Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Strings in C In general, a string is a sequence of characters that we index from

ID: 3869936 • Letter: S

Question

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.

Explanation / Answer

Here is the program as per your requirements.

The output is also attached.

#include <stdio.h>
#include <string.h>
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";
//int ss_loc ;
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);
ss_loc = index_of_from(sent, 0,word1);
if ( ss_loc != -1 )
printf(""%s" found starting from the given index 0 is : %d ", word1, ss_loc);
ss_loc = index_of_from(sent,2, word2);
if ( ss_loc != -1 )
printf(""%s" found starting from the given index 2 is : %d ", word2, ss_loc);
ss_loc = index_of_from(sent, 0,word3);
if ( ss_loc != -1 )
printf(""%s" found starting from the given index 0 is : %d ", word3, ss_loc);
ss_loc = index_of_from(sent,0, word4);
if ( ss_loc != -1 )
printf(""%s" found starting from the given index 0 is : %d ", word4, ss_loc);
else
printf(""%s" was not found ", word4);
ss_loc = index_of_from(sent, 0,word5);
if ( ss_loc != -1 )
printf(""%s" found starting from the given index 0 is : %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
int i,j,k,l1,l2,r;
r = -1;
j =0;
l1 = strlen(str);
l2 = strlen(sub_str);
for(i=0; i < l1 ; i++)
{
   if( l2 >= i)
   {
   if(str[i] == sub_str[j])
       {
       for(k=1; k < l2; k++)
           {
           if(str[i+k] == sub_str[k])
               r = i+1;
           else
               r = -1;
           }
       }
       }
}
return r;
}
// 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
int i,j,k,l1,l2,r;
r = -1;
j =0;
l1 = strlen(str);
l2 = strlen(sub_str);
if((start_index + l2) <= l1)
{
for(i=start_index; i < l1 ; i++)
{
   if( l2 >= i)
   {
   if(str[i] == sub_str[j])
       {
       for(k=1; k < l2; k++)
           {
           if(str[i+k] == sub_str[k])
               r = i-start_index;
           else
               r = -1;
           }
       }
       }
}
}
return r;
}

Output:

Sentence to search : This lab is fun

"This" found starting at index : 1

"is" found starting at index : 3

"bun" was not found

"funny" was not found

"This" found starting from the given index 0 is : 0

"is" found starting from the given index 2 is : 0

"bun" was not found

"funny" was not found