سؤال

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++;
}
...
هل كانت مفيدة؟

المحلول

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.

نصائح أخرى

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++;
    }
}

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

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);

%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);
}

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

scanf("%255[\n]s",phrase);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top