Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/55.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
isspace()将不计算C中的空格_C_Space - Fatal编程技术网

isspace()将不计算C中的空格

isspace()将不计算C中的空格,c,space,C,Space,我这里有这个练习。当我输入一个有许多空格的句子时,我希望它告诉我那个数字。但是,它将只显示0个空格。我是不是犯了一个愚蠢的小错误 #include <stdio.h> #include <ctype.h> #define COUNT 40 int main() { char input[COUNT]; printf("Enter a sentence: "); scanf("%s", input); int loopCount; int spac

我这里有这个练习。当我输入一个有许多空格的句子时,我希望它告诉我那个数字。但是,它将只显示0个空格。我是不是犯了一个愚蠢的小错误

#include <stdio.h>
#include <ctype.h>

#define COUNT 40

int main()
{
  char input[COUNT];
  printf("Enter a sentence: ");
  scanf("%s", input);

  int loopCount;
  int spaceCount = 0;

  for(loopCount = 0; loopCount < COUNT; loopCount++)
  {
    if (isspace(input[loopCount]))
    {
      spaceCount++;
      printf("SO MUCH STUFF");
    }
    else{}
  }

  printf("That sentence has %d spaces.", spaceCount);
  return(0);
}
#包括
#包括
#定义计数40
int main()
{
字符输入[计数];
printf(“输入一个句子:”);
扫描频率(“%s”,输入);
整数循环计数;
int spaceCount=0;
对于(loopCount=0;loopCount
带有%s修饰符的scanf将读取一个字符串(使用空格标记 完)

修理,使用

代码

#include <stdio.h>
#include <ctype.h>

#define COUNT 40

int main()
{
  char input[COUNT];
  printf("Enter a sentence: ");

  // read line of string ( or first COUNT characters )
  // this is also a safer alternative for buffer overflow
  fgets(input, COUNT, stdin);

  int loopCount;
  int spaceCount = 0;

  for(loopCount = 0; loopCount < COUNT; loopCount++)
  {
    if (isspace(input[loopCount]))
    {   
      spaceCount++;
    }
  }

  printf("That sentence has %d spaces.\n", spaceCount);
  return 0;
}
带有%s修饰符的scanf将读取一个字符串(使用空格标记 完)

修理,使用

代码

#include <stdio.h>
#include <ctype.h>

#define COUNT 40

int main()
{
  char input[COUNT];
  printf("Enter a sentence: ");

  // read line of string ( or first COUNT characters )
  // this is also a safer alternative for buffer overflow
  fgets(input, COUNT, stdin);

  int loopCount;
  int spaceCount = 0;

  for(loopCount = 0; loopCount < COUNT; loopCount++)
  {
    if (isspace(input[loopCount]))
    {   
      spaceCount++;
    }
  }

  printf("That sentence has %d spaces.\n", spaceCount);
  return 0;
}

@amdixon说得很对,scanf只能读取到空格,所以通常使用scanf执行类似操作的方式是使用while循环,但是FGET和读取到静态分配的数组都是非常不确定的事情。这里最美妙的事情是你可以懒散到很好的效果。像这样的方法会很有效:

char next = 0;
while (next != '\n')
{
    scanf("%c", &next);
    if (next == ' ')
        spaceCount++;
}

@amdixon说得很对,scanf只能读取到空格,所以通常使用scanf执行类似操作的方式是使用while循环,但是FGET和读取到静态分配的数组都是非常不确定的事情。这里最美妙的事情是你可以懒散到很好的效果。像这样的方法会很有效:

char next = 0;
while (next != '\n')
{
    scanf("%c", &next);
    if (next == ' ')
        spaceCount++;
}

scanf将只给您第一个字。.可能的重复顺便说一句,您应该稍微更改循环条件,可能
for(loopCount=0;input[loopCount]&&loopCount
,这样,如果读取的字符串比缓冲区短,循环就不会继续超过字符串的结尾。关于这一行:''scanf(“%s”,输入;1)scanf将在遇到的第一个空格(通常是空格、制表符、换行符)字符处停止。2)始终检查scanf返回的值(而不是参数值)为确保操作成功。3)在%s上放置“最大长度”修饰符,否则用户可能会溢出输入缓冲区,导致未定义的行为,并可能/将导致seg故障事件。定义“数值”时,请始终将值包装在括号中(“,”)'为避免任何“文本替换”错误,SANK将只给您第一个单词。。顺便说一句,您应该稍微更改循环条件,可能
for(loopCount=0;input[loopCount]&&loopCount
,这样循环将不会继续超过您读取的字符串的结尾(如果该字符串比缓冲区短)。关于此行:“scanf”(“%s”,input);1)scanf将在遇到的第一个空格(通常是空格、制表符、换行符)字符处停止。2)始终检查scanf返回的值(而不是参数值)为确保操作成功。3)在%s上放置“最大长度”修饰符,否则用户可能会溢出输入缓冲区,导致未定义的行为,并可能/将导致seg错误事件。定义“数值”时,请始终将该值包装在“括号”(“,”)中,以避免任何“文本替换”错误