0

i wrote this code:

    printf("enter a string: ");
    scanf("%s",C); // C= "Hello world this is a string"

    printf("%d\n",strlen(C));

    temp=com0(C);

this code shows that the length of the string is 5 which also mean that is the length of the first word only i have to get the full length but that's not the point the important thing is i have to pass the whole string to the function which also print the length of the first word only and it should print the whole length instead

this is the code of the function:

bool com0(char k[]){
    printf("%d\n",strlen(k));
    if(k[0]>='a' && k[0] <= 'z'){

                    return com1(nextchar(k+1));
    }
    else{
        return false;
    }
}

it prints 5 also !

and this the result of execution:

enter image description here

  • 2
    Did you read up on `scanf()`, it `"%s"` part and what happens at white space? – Yunnosch Dec 19 '22 at 21:37
  • 1
    use `fgets` scanf is full of traps – 0___________ Dec 19 '22 at 21:38
  • @Yunnosch i didn't understand, can you explain more please ? – Houssam Eddine Dec 19 '22 at 21:39
  • 1
    Sure. I recommend to read the specification/documentation of the function `scanf()` which you are apparently using without having done so. In order to make it easier for you, I point out the paragraph on the `"%s"` part and to look for any description of the effect of white space. – Yunnosch Dec 19 '22 at 21:41
  • 1
    If the string you try to enter is (e.g.) `Hello world this is a string`, then don't use `scanf` The `%s` will only capture `Hello` [up to the first whitespace char]. Use (e.g): `char buf[1000]; fgets(buf,sizeof(buf),stdin); buf[strcspn(buf,"\n")] = 0;` to get the full string (with the newline stripped). Also, for prompting a user, see my answer: [Check if all values entered into char array are numerical](https://stackoverflow.com/a/65013419/5382650) – Craig Estey Dec 19 '22 at 21:42
  • `gets` function fixed the problem – Houssam Eddine Dec 19 '22 at 21:46
  • 1
    Never use `gets`--it is a blatant security hole. The [linux] man page explains why. It has been removed from the modern ISO/C standard [for good reason]. See: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/q/1694036/5382650) – Craig Estey Dec 19 '22 at 21:48
  • The line `printf("%d\n",strlen(k));` invokes undefined behavior. The function `strlen` returns a value of type `size_t`, so the correct format specifier is `%zu`, not `%d`. Even if it works on your platform, it may fail on other platforms. – Andreas Wenzel Dec 19 '22 at 21:52

1 Answers1

1

using fgets instead of scanf fixed the problem!

printf("enter a string: ");
fgets(C,sizeof(C),stdin);
C[strcspn(C,"\n")] = 0; // C = "Hello world this is a string"
printf("%d\n",strlen(C)); // print 28

    temp=com0(C);
  • 1
    The line `printf("%d\n",strlen(k));` invokes undefined behavior, which may cause the program to crash. The function `strlen` returns a value of type `size_t`, not `int`, so the correct format specifier is `%zu`, not `%d`. Even if it works on your platform, it may fail on other platforms. See the documentation of [`strlen`](https://en.cppreference.com/w/c/string/byte/strlen) and [`printf`](https://en.cppreference.com/w/c/io/fprintf) for further information. – Andreas Wenzel Dec 19 '22 at 22:09