0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main(){

    char str [1000] = "";
    char ch     = 'M';
    char *findM;
        printf("Enter a line of text\n");
        scanf("%s", str);
            findM = strchr(str, ch);
        printf("string after %c is %s ", ch, findM);

    return 0;
}

The input to the program is "My name is Steve", and the output of this program becomes, string after M is (null) Why is this occurring?

StaticBeagle
  • 5,070
  • 2
  • 23
  • 34

4 Answers4

1

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;
}
StaticBeagle
  • 5,070
  • 2
  • 23
  • 34
0

You would get null if the method does not find any matches, it is possible that your input does not have 'M'

for more details : http://www.cplusplus.com/reference/cstring/strchr/

bnm
  • 108
  • 6
0

Probably your str variable doesn't contain the char 'M' in the first word - The strchr(string, char) function returns null if no match is found and cuts off the input string after the first whitespace (excluding leading whitespaces). As user3121023 mentioned in their comment, use fgets instead to capture multi-word input.

Jonas
  • 1,473
  • 2
  • 13
  • 28
  • it gives me a (null) regardless, I think its assuming theres nothing being into str, but i dont know why –  Oct 22 '16 at 23:18
  • @MuhammedBamne try setting char *str = "Muhammed"; instead. This shouldn't return null. – Jonas Oct 22 '16 at 23:24
0

strchr() is considering ' ' (space )as a delimiter so give you input withiut a space it works fine ..

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main()
{

    char str [1000] = "";
    char ch     = 'M';
    char *findM;
    printf("Enter a line of text\n");
        scanf("%s", str);
        findM = strchr(str, ch);
    if(findM)
            printf("string after %c is %s ", ch, findM);

    else
        printf("Character not found ...\n");
    return 0;
}
Anjaneyulu
  • 434
  • 5
  • 21