As mentioned in one of the comments, scanf("%s", str)
reads until it finds a trailing white space. In your input "My name is Steve" scanf
will read up to My
since there is a space after My
.
Assuming that your input only contains numbers,letters, and spaces you could try the following:
int main()
{
char str[1000] = "";
char ch = 'M';
char *findM;
printf("Enter a line of text\n");
scanf("%999[0-9a-zA-Z ]", str); // Get all alphanumerics and spaces until \n is found
findM = strchr(str, ch);
findM++; // Increment the pointer to point to the next char after M
printf("string after %c is %s ", ch, findM);
return 0;
}
If you are not required to use scanf()
, I will recommend staying away from scanf()
and using fgets()
instead:
int main()
{
char str[1000] = "";
char ch = 'M';
char *findM;
printf("Enter a line of text\n");
fgets(str, sizeof(str), stdin); // Get the whole string
findM = strchr(str, ch);
findM++; // Increase the counter to move to the next char after M
printf("string after %c is %s ", ch, findM);
return 0;
}