1

The following code does not work:

char text[10];
    scanf(" %s", &text);

    if (strcmp(text, "some text") == 0)
        puts("some text");

But when I change 'if part' thus, then everything is fine:

if (strcmp(text, "sometext") == 0)

why does it work this way and how to fix it?

Argentum
  • 41
  • 5

4 Answers4

3

The problem is not with your strcmp call but, rather, in your scanf call! When you type some text as the input, scanf will stop reading at the first whitespace, so your text string will be "some" - and, thus, the strcmp call will (correctly) return non-zero, as this is not the same as "some text".

For more information on this feature of scanf, spaces and the %s format, see this topic: How do you allow spaces to be entered using scanf?

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
1

call fgets to read the white space and compare the strings with strncmp :

fgets (text, 100, stdin);
puts(text);
if (strncmp(text, "some text",9) == 0)
    puts("some text");
Marwen Jaffel
  • 703
  • 1
  • 6
  • 14
0

My answer will be about sorting such a problems without asking the forum qwuestions. I would take 2-3 minutes to see what is going there:

int main()
{
    char text[10];
    int result;
    result = scanf(" %s", &text);

    printf("scanf result: %d, scanned text: \"%s\"\n", result, text);

    if (strcmp(text, "some text") == 0)
        puts("some text");

}

Debugging - ie finding the source of the problems is same important as programming knowledge. If you add this single line yourself, you would sort this problem yourself in no time.

Try yourself: https://godbolt.org/z/Tmy7PA

0___________
  • 60,014
  • 4
  • 34
  • 74
-1

Off-topic, but too long for a comment.

Your scanf line is actually wrong (not referring to the whitespace), and you're just getting lucky that it works here. You need to remove the ampersand & in front of text. What you have passes the address of text to scanf, which is actually a pointer to a char[10]. For scanf(" %s", text);, text "decays" to a pointer to its first element (char*) which what you actually want. Here, it just so happens that the address of the array (&text) is the same as the address to the first element in the array (&(text[0]), or *(text + 0)), since they're both in automatic storage at the same place. If, for example, you had something like

char* text = malloc(10);
scanf(" %s", &text);

you would invoke undefined behavior by overwriting memory starting at the address of the pointer to your malloced memory. Here, the address of text is an address to the pointer in automatic storage, whereas the memory it actually points to is somewhere else. You can see this illustrated in the following program.

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

int main(void)
{
    char text[10];
    char* text2 = malloc(10);

    printf("address of text = %p\n", (void*)(&text));   // same address as below
    printf("address of text[0] = %p\n", (void*)(&(text[0])));  // same address as above
    printf("address of text2 = %p\n", (void*)&text2);   // different than below
    printf("address of text2[0] = %p\n", (void*)(&(text2[0])));  // different than above

    return 0;
}

&text == &(text[0]), showing the address of the array and the address of the first element of the array are equivalent, however &text2 != &(text2[0]), showing the address of the pointer is not equal to the address of the first element of the memory you've malloced.

Illustrated here: https://godbolt.org/z/N2TKvK

yano
  • 4,827
  • 2
  • 23
  • 35
  • DV because it doesn't answer the question or because I've said something wrong? What I've shown is something OP needs to understand, don't think that's worthy of a DV. – yano Jan 23 '20 at 19:16