I've created a program that behaves in a strange way -- I am not sure why it is giving improper values. For example when I use input: ("a chatterbox", "hat"), it works properly:
The starting position is 3.
String 'hat' occured for 1 time(s).
But if I remove the space ("achatterbox", "hat") it shows:
String 'hat' occured for 0 time(s).
Exercise explanation:
/* Write a function called findString() to determine if one character string exists inside another string. The first argument to the function should be the character string that is to be searched and the second argument is the string you are interested in finding. If the function finds the specified string, have it return the location in the source string where the string was found. If the function does not find the string, have it return −1. So, for example, the call Click here to view code image index = findString ("a chatterbox", "hat"); searches the string "a chatterbox" for the string "hat". Because "hat" does exist inside the source string, the function returns 3 to indicate the starting position inside the source string where "hat" was found. */
My code:
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
int findString (char motherArgument[], char childArgument[]);
int main(int argc, char const *argv[])
{
printf("%s",findString ("a chatterbox", "hat")); //why if I remove space ' ' this does not work, why?
return 0;
}
int findString (char motherArgument[], char childArgument[]) // is child inside the mother?
{
int i, flag = false, freq = 0;
for (i = 0; i < strlen(motherArgument); i++) // going through mother
{
if(motherArgument[i] == childArgument[i]) // chars equal?
{
flag = true;
printf("The starting position is %d.", i - 1); // i -1 to get 3 as in the example
freq++; // frequency
}
}
if (flag = true)
{
printf("\nString '%s' occured for %d time(s).\n", childArgument, freq);
}
else
{
printf("None\n"); // false = none
return -1;
}
}
What am I doing wrong? Should I input the data in a different way?
findStringdoes not return a pointer suitable forprintf's%sformat, and the code only finds the substring if it starts at the beginning. It was luck, or bad luck. My attempt crashed whether or not there is a space in the string. You are probably overflowing the child argument anyway.findString()returns an index, not a string. You should print the result with%d, not%s.findString()doesn't match a substring. It checks whether corresponding characters in the two strings are identical, and counts them. E.g.abcdefandxbc1e3will count 3 matches forb,c, ande. Also, it will access outside the child if it's shorter than the mother.