Why can't I get the length of the whole string?

给你一囗甜甜゛ 提交于 2019-12-23 02:06:12

问题


I tried strlen(string). And I tried writing the code for finding the length of the whole string. However it still tends to stop counting the length when the first 'space' is reached. Why does it happen? What is the solution? I'm trying to stop at '\0'. Shouldn't this work? Here is the part of the code.

int main(void) {
char phrase[256];
char phrase2[256];
int i,j,size;
scanf("%255s", phrase);
for(i=0;phrase[i]!='\0';i++){
    size++;
}
...

回答1:


The reason it stops counting is because scanf reads one word at a time. It's reading the first word and then halting, ignoring the rest of the line.

Instead, use fgets:

Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the end-of-file is reached, whichever happens first.

You can read from standard input like so:

fgets(phrase, 256, stdin);

Have you considered using strlen to measure the length of your input?

Finally, you should get into the habit of initialising variables when you declare them:

int i = 0,j = 0, size = 0;

When a variable is declared its value is 'whatever was in memory', until you set its value. In your example 'length' is never initialised, which will lead to unexpected behaviour. Initialising variables is a good way to avoid unexpected behaviour and time wasted tracking down bugs.




回答2:


Scanf() function 

does not take spaces. whenever space is occured, it considers it as end of input and puts '\0' at that position.

you can do one thing:

scanf("%[^\n]s",phrase);

this scanf will take input till newline is occured i.e. you press the enter :)

Secondly important thing in your code is, you have not initialized size to 0.

Here is the solution:

int main(void) 
{
    char phrase[256];
    char phrase2[256];
    int i,j,size=0;    //initialize size
    scanf("%[^\n]s",phrase);   //input taken till '\n'
    for(i=0;phrase[i]!='\0';i++)
    {
        size++;
    }
}



回答3:


However it still tends to stop counting the length when the first 'space' is reached.

Well, that is the default behaviour of scanf. You can use fgets to fix your problem. Also, to add a note size is not initialized




回答4:


sscanf stops at the first white space character. Here's a snippet from man scanf about the field width specifier.

An optional decimal integer which specifies the maximum field width. Reading of characters stops either when this maximum is reached or when a nonmatching character is found, whichever happens first.

If you would like to read the entire line, use fgets

fgets(phrase, 255, stdin);



回答5:


%s consumes words (separated by one or more spaces), You could use %c which consumes characters.

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

int main(void) {
  char phrase[256];
  int size;
  scanf("%255c", phrase);
  size = strlen(phrase) - 1; /* -1 is for EOL */
  printf("length is %d\n", size);
}



回答6:


The scanf() function can only read one word at a time. You can change the code to

scanf("%255[\n]s",phrase);


来源:https://stackoverflow.com/questions/22433309/why-cant-i-get-the-length-of-the-whole-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!