Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Strings in C In general, a string is a sequence of characters that we index from [0, length -1], where length is the number of

Strings in C

In general, a string is a sequence of characters that we index from [0, length-1], where length is the number of characters in the sequence. A sub-string is then the sequence of characters in found between a given start index and a given end index, where 0 <= start <= end and end <= length-1. Given two strings S1 and S2, one may wish to determine is say S2 is a sub-string of S1, and if so where it starts in S1.

First, you are given the contents of the file main.c (which is incomplete):

#include

#include

typedef unsigned int index_t;

int index_of(const char* str, const char* sub_str);

int index_of_from(const char* str, index_t start_index,

const char* sub_str);

int main()

{

char sent[] = "This lab is fun";

char word1[] = "This";

char word2[] = "is";

char word3[] = "fun";

char word4[] = "bun";

char word5[] = "funny";

printf(" Sentence to search : %s ", sent);

int ss_loc = index_of(sent, word1);

if ( ss_loc != -1 )

printf("\"%s\" found starting at index : %d ", word1, ss_loc);

ss_loc = index_of(sent, word2);

if ( ss_loc != -1 )

printf("\"%s\" found starting at index : %d ", word2, ss_loc);

ss_loc = index_of(sent, word3);

if ( ss_loc != -1 )

printf("\"%s\" found starting at index : %d ", word3, ss_loc);

ss_loc = index_of(sent, word4);

if ( ss_loc != -1 )

printf("\"%s\" found starting at index : %d ", word4, ss_loc);

else

printf("\"%s\" was not found ", word4);

ss_loc = index_of(sent, word5);

if ( ss_loc != -1 )

printf("\"%s\" found starting at index : %d ", word5, ss_loc);

else

printf("\"%s\" was not found ", word5);

return 0;

}

// if sub_str exists as a sub-string in the string str

// return the starting index of the left-most occurrence;

// otherwise return -1

int index_of(const char* str, const char* sub_str)

{

// your code here

}

// if sub_str exists as a sub-string in the string (str + start_index)

// return the starting index of the left-most occurrence;

// otherwise return -1

int index_of_from(const char* str, index_t start_index,

const char* sub_str)

{

// your code here

}

First, you will need to write the body for the function :

index_of

Such that when all the files are in one directory (in UNIX) typing

gcc -Wall main.c

followed by

./main

Will produce the following output:

$ gcc -Wall main.c

$ ./main

Sentence to search : This lab is fun

"This" found starting at index : 0

"is" found starting at index : 2

"fun" found starting at index : 12

"bun" was not found

"funny" was not found

Second, copy the body of your index_of function into the body of the index_of_from function and think about what lines of code will need to be added to meet its specifications. Note: you can do it by adding a single new first line of code, but also worry about an obvious error that then may occur and how to avoid it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

please dont use chat gpt or other AI 4 2 5 . .

Answered: 1 week ago