简体   繁体   English

计算整个字符串,包括 C 中的空格

[英]Counting the Whole String Including The Spaces in C

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输出:19

But all I get is input: The fox is gorgeous.但我得到的只是输入:狐狸很漂亮。 output: 3输出:3

strlen already includes spaces, since it counts the length of the string up to the terminating NUL character (zero, '\\0' ). strlen已经包含空格,因为它计算字符串的长度,直到终止NUL字符(零, '\\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). 您的问题是,当scanf%s转换遇到空格时,它会停止读取,因此您的字符串永远不会一开始就包含它(您可以通过打印出字符串来轻松地进行验证)。 (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.) (您可以通过使用不同的scanf转换来修复它,但通常,通过使用fgets读取可以更轻松地完成操作-它还会强制您指定缓冲区大小,从而修复当前代码中潜在的缓冲区溢出。)

The Answer by Arkku is correct in its diagnose. Arkku答案在诊断中是正确的。 However, if you wish to use scanf, you could do this: 但是,如果您希望使用scanf,则可以执行以下操作:

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

The 99 tells scanf not to read more than 99 characters, so your get buffer won't overflow. 99告诉scanf读取的字符不能超过99个,因此您的get缓冲区不会溢出。 The [^\\n] tells scanf to read any character until it encounters the newline character (when you hit enter). [^\\n]告诉scanf读取任何字符,直到遇到换行符为止(按回车键时)。

As Chux pointed out, the code still has 2 issues. 正如Chux指出的那样,该代码仍然存在2个问题。

When using scanf, it is always a good idea to check its return value, which is the number of items it could read. 使用scanf时,最好检查它的返回值,这是它可以读取的项目数。 Also, indeed the \\n remains in the input buffer when using the above syntax. 同样,使用上述语法时, \\n实际上仍保留在输入缓冲区中。 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

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.我用过 c 语言,你可以遍历字符串的结尾部分,你会得到长度。 here i have used "EDIT SET CONVESRION METHOD" to read string, you can also gets to read.这里我使用了“EDIT SET CONVESRION METHOD”来读取字符串,您也可以读取。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM