3

I'm trying to set up a code that counts the whole string and doesn't stop after the first space that it finds. How do I do that?

I tried this kind of code but it just counts the first word then goes to display the number of letters in that first word.

So far this is what I have tried.

int main(){
    char get[100];
    int i, space=0, len=0, tot;
    scanf("%s", get);

    for (i=0; get[i]!='\0'; i++)
    {
        if (get[i] == ' ')
            space++;
        else 
            len++;
    }

tot = space + len;
printf("%i", tot);
}

And

int main(){
    char get[100];
    int len;
    scanf("%s", &get);
    len = strlen(get);
    printf("%i", len);
}

But would still get the same answer as the first one.

I expected that if the input: The fox is gorgeous. output: 19

But all I get is input: The fox is gorgeous. output: 3

HaxCkzersssxz
  • 63
  • 1
  • 8

3 Answers3

5

strlen already includes spaces, since it counts the length of the string up to the terminating NUL character (zero, '\0').

Your problem is that that the %s conversion of scanf stops reading when it encounters whitespace, so your string never included it in the first place (you can verify this easily by printing out the string). (You could fix it by using different scanf conversions, but in general it's easier to get things right by reading with fgets – it also forces you to specify the buffer size, fixing the potential buffer overflow in your current code.)

Arkku
  • 41,011
  • 10
  • 62
  • 84
2

The Answer by Arkku is correct in its diagnose. However, if you wish to use scanf, you could do this:

scanf("%99[^\n]", get);

The 99 tells scanf not to read more than 99 characters, so your get buffer won't overflow. The [^\n] tells scanf to read any character until it encounters the newline character (when you hit enter).

As Chux pointed out, the code still has 2 issues.

When using scanf, it is always a good idea to check its return value, which is the number of items it could read. Also, indeed the \n remains in the input buffer when using the above syntax. So, you could do this:

if(scanf("%99[^\n]", get) == 0){
    get[0] = 0; //Put in a NUL terminator if scanf read nothing
}

getchar();      //Remove the newline character from the input buffer
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
GermanNerd
  • 643
  • 5
  • 12
  • 3
    When the input is `'\n'`, this code reads nothing into `get` and the `'\n'` remains in `stdin`. `get[]` remains unassigned and then the following `get[i]!='\0'` is UB. – chux - Reinstate Monica Jan 11 '19 at 16:24
  • Ever watchful...Thanks. – GermanNerd Jan 11 '19 at 16:51
  • 1
    Better, UV, yet `getchar()` may read a non-newline due to long input. Pedantically code can use `scanf("%*1[^\n]");` return the _potential_ trailing newline. IMO, its these corners cases that make `fgets()` easier to use, but `fgetc()` has its troubles too. – chux - Reinstate Monica Jan 11 '19 at 16:58
  • @chux Interesting, I didn't know that syntax. Yeah, scanf usually is more trouble than it's worth, and I hardly ever use it in my code. But a lot of questions are asked involving it, often by students where it is a requirement in assignments. – GermanNerd Jan 11 '19 at 17:04
  • 1
    If reading with `scanf` exclusively and there is no need to capture leading whitespace, one option is to just prepend a single space to the format string to consume any existing whitespace (e.g., `" %99[^\n]"`). However, if the input format is more complex than here, IMO all `scanf` solutions become hairy (e.g., need to consume erroneous non-whitespace for different types and numbers of fields), hence I did not even wish to include a `scanf` option in my answer… Even `fgets` followed by `sscanf` is far easier to get right. – Arkku Jan 11 '19 at 17:37
  • @Arkku Agree about `scanf()` short-comings. Note that with `" %99[^\n]"`, `scanf()` will not even return on a `"\n"` input as it waits for more input. – chux - Reinstate Monica Jan 11 '19 at 18:28
  • `scanf` is great for converting tables of numbers that are piped into your programme, not so great with terminal user input. – Neil Jan 11 '19 at 23:00
1

I will take one example to explain the concept.

main()
{
  char s[20], i;

  scanf("%[^\n]", &s);
    while(s[i] != '\0') {
       i++;
   }
  printf("%d", i);

  return 0;
}

i have used c language and u can loop through the ending the part of the string and you will get the length. here i have used "EDIT SET CONVESRION METHOD" to read string, you can also gets to read.

Gururaj
  • 157
  • 9