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

Computing 1 C source file, Assgn6.c, on Blackboard: http: //uml.umassonline.net

ID: 3846671 • Letter: C

Question

Computing 1 C source file, Assgn6.c, on Blackboard: http: //uml.umassonline.net Description: Create a project called Assgn6. Add a C source file to the project named assign6.c. A string is nothing but a list of characters. The first character of a string can be pointed by a pointer, say sp, and the 2^nd character of the string can be accessed by *(sp + 1), for example. Also, it may be useful to use a function that returns the number of characters in a string: int strlen (char *string); Write a function int find string char *source, char substring) that searches for the first occurrence of substring in source and returns the index of the first character of substring where it is found in the source. If substring does not occur in source, a value of -1 is returned. For example, if original = "software catalog" and portion = "cat", find string(original, portion) will return 9, since the c of "cat" occurs starting at 9^th position of "software catalog" when the first character s is counted at the 0^th position. The main() function asks for the source string and the substring to search for in the source string, calls the find string() function, and prints the index value returned from the find string() function.

Explanation / Answer

Here is the code and output. Please don't forget to rate the answer if it helped. Thank you very much.

#include <stdio.h>
#include <string.h>
int find_string(char *source, char *substr)
{
int i,j,k, end1= strlen(source),end2 = strlen(substr);
int found = 1;
for(i = 0; i< end1; i++)
{
found = 1;
/*for each index in the source string, search all chars of the search string and
break out of loop if any char did not match*/
for(j = 0, k = i; j < end2; j++, k++)
{
if(*(source+k) != *(substr+j))
{
found = 0;
break;
}
}
if(found)
return i;
}
return -1;
}
int main()
{
char source[50], search[50];
printf(" Enter a source string: ");
/*use fgets as there can be spaces in user input. can use scanf if no spaces will be provided*/
fgets(source,50, stdin);
printf(" Enter a search string: ");
fgets(search,50, stdin);

/*fgets will also store the char at end, remove the newline from inputs. If newline is not
removed, we might not be able to match the search string in the source anywhere except if it
was in the end of source...*/
source[strlen(source) - 1] = '';
search[strlen(search) - 1] = '';

/*find the index of source in search*/
int idx = find_string(source, search);
printf("index of %s in %s is %d ",search, source, idx );
}

output

Enter a source string: software catalog

Enter a search string: cat
index of cat in software catalog is 9

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote