【问题标题】:In C, use a while loop to count the number of characters a user inputs在 C 中,使用 while 循环来计算用户输入的字符数
【发布时间】:2011-12-29 02:01:48
【问题描述】:

我是一个完全的编程新手,在我知道的一些非常基本的东西上遇到了很多麻烦。我创建了一个程序,用户输入一个单词,我必须使用 while 循环来计算单词中的字符数并在屏幕上显示结果。

我可以让用户输入单词,但我的问题在于 while 循环。我只是不明白如何编码。我真的很感激这方面的帮助。

谢谢

编辑:

这是我到目前为止所做的:

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

int main(void)
{
char input[30]; 
int wordlen;

printf("please enter a word: \n");
scanf("%29c", input);

while (input < 30);
{
/*Not sure what to put in here*/
printf("Number of letters in input is %s", /*???*/);
}

return 0;
}

另一个编辑:这是作业,但我的讲师很垃圾,没有很好地解释事情。我正在努力学习并想了解它是如何工作的,我不一定期待直接的答案。甚至一些关于如何自己解决它的提示也会很棒。谢谢


好的,经过多次试验和错误,这是我想出的。我认为这是正确的,但希望您对此发表意见。请记住,我做 C 的时间还不到 3 周,所以我的技术可能很差。谢谢大家的意见。

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

int main(void)
{
char input[30]; 
int i;
int x;
x=0;

printf("please enter a word: \n");
scanf("%29s", input);
i=strlen(input);

while (x < i)
{
 x++;
}
 printf("Number of letters in input is %d", x);


return 0;
}

【问题讨论】:

  • 展示解决问题的一些努力/尝试。
  • 尝试更具体地说明您不了解的内容,以便我们为您提供帮助(显示您编写的代码总是好的)。
  • 我怀疑这应该被标记为作业。

标签: c loops count while-loop


【解决方案1】:

本着homework 标记帖子的精神,我删除了我的实现,我将提供提示:

C 中的字符串是 NULL 终止的,这意味着如果你有一个字符串,例如由 char[30] 指向的“qW3rTy”,在内存中它实际上看起来像这样:

input[0] = 'q'
input[1] = 'w'
input[2] = '3'
input[3] = 'r'
input[4] = 'T'
input[5] = 'y'
input[6] = '\0'
... // It doesn't matter what's after here, because the NULL above marks the end.

'\0' 是另一种说法,即该字节的值实际上为零,即 NULL。因此,要计算字符串中的字符数,您需要遍历字符串以查找第一个空字符,并增加一个计数器。确保您不计算 NULL。

【讨论】:

    【解决方案2】:
    i = strlen(input);
    while (x < i){
        x++;
    }
    

    你最多可以数 x 与 strlen 一起计数是没有意义的。 只有 x = i。 如果计数本身的目的是,在没有strlen的情况下计算实际字符。

    例如)

    while (input[x] != '\0'){
        x++;
    }
    

    【讨论】:

      【解决方案3】:
      #include <stdio.h>
      #include <string.h>
      
      int main(void){
          char input[30]; 
          int wordlen;
      
          printf("please enter a word: \n");
          // %29c is request 29 chars include newline
          // %29s is max 29 chars input exclusive space chars(space, tab, newline..)
          // %*c  is drop out char for newline ('\n' remain buffer)
          // %s is request of C string
          // C string is char sequence , and last char is '\0'
          scanf("%29s%*c", input);
          // strlen is counting C string chars until last '\0' char
          wordlen = strlen(input);
      
          //while(condition); //this while does not has  block to execute
          //input is constant address , The address comparison(input < 30) is meaningless
          //while break condition as 0==strlen(input) is NG (why reason scanf wait until input)
          //exit loop when input special string "-quit"  
          while (0!=strcmp("-quit", input)){
              printf("Number of letters in input is %d.\n", wordlen);
              printf("please enter a word: \n");
              scanf("%29s%*c", input);
              wordlen = strlen(input);
          }
      
          return 0;
      }
      

      【讨论】:

      • 如果您用解释分解代码块而不是将其隐藏在 cmets 中,这将更具可读性。此外,当问题被标记为homework 时,提示比仅仅提供代码更可取。
      【解决方案4】:

      如果你将字符串存储在一个字符数组中,比如 s,最简单的解决方案是使用名为 strlen 的内置 C 函数,它可以计算字符串中的字符数

      printf("%d\n",strlen(str));
      

      使用 while 循环,您需要从数组的开头开始检查,直到到达 NULL('\0') 字符,类似于:

      len = 0;
      while(str[len] != '\0')
        len++;
      printf("%d\n",len);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-06-08
        • 1970-01-01
        • 2021-09-14
        • 1970-01-01
        • 2022-01-23
        • 2013-02-27
        • 2011-09-29
        相关资源
        最近更新 更多