在C中,使用while循环来计算用户输入的字符数

发布于 2024-12-15 06:17:39 字数 999 浏览 1 评论 0原文

我是一个完全的编程新手,并且在我知道的非常基本的事情上遇到了很多麻烦。我创建一个程序,用户输入一个单词,我必须使用 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;
}

I'm a complete newbie to programming and am having a lot of trouble with something I know is very basic. I create a program where the user inputs a word and I must use a while loop to calculate the number of characters in the word and show the result on screen.

I'm fine with having the user input the word but my problem is with the while loop. I just can't understand how to code it. I really would appreciate some help with this.

Thanks

Edit:

Here's what I've done so far:

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

Another edit: This is homework but my lecturer is rubbish and doesn't explain things well. I'm trying to learn and would like to understand how it works I'm not necessarily expecting a direct answer. Even some hints as to how to solve it myself would be great. Thanks


Ok after much trial and error here is what I have come up with. I think it is correct but would like your opinions on it. Please bear in mind I have been doing C for less than 3 weeks so my technique might be poor. Thanks for your input everyone.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我要还你自由 2024-12-22 06:17:39

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

C 中的字符串以 NULL 结尾,这意味着如果您有一个像“qW3rTy”这样的字符串指向通过 char[30] 来表示,在内存中,这实际上看起来像这样:

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。

In the spirit of a homework tagged post, I've deleted my implementation and I'll offer hints instead:

Strings in C are NULL terminated, that means that if you have a string like "qW3rTy" pointed to by a char[30], in memory this actually looks like this:

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.

That '\0' is another way of saying that the value at that byte is literally zero, aka NULL. So, to count the number of characters in a string, you loop over the string looking for the first null character, incrementing a counter. Be sure you don't count the NULL.

时间你老了 2024-12-22 06:17:39

如果您将字符串存储在字符数组中,例如 s,最简单的解决方案是使用内置的 C 函数 strlen,它可以计算字符串中的字符数。

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

使用 while 循环,您需要从头开始检查直到到达 NULL('\0') 字符,如下所示:

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

If you are storing the string in a character array, say s, the easiest solution is to use built in C-function called strlen, which can compute the number of characters in a string

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

Using a while loop, you need to check from the start of the array until you reach a NULL('\0') character, which goes something like:

len = 0;
while(str[len] != '\0')
  len++;
printf("%d\n",len);
不即不离 2024-12-22 06:17:39
i = strlen(input);
while (x < i){
    x++;
}

你可以算出 x 的计数,与 strlen 结合起来是没有意义的。
只有 x = i。
如果计数本身的目的是计算实际字符而不使用strlen。

前任。)

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

You can count up to a count of x in conjunction with strlen does not make sense.
Only just x = i.
If the purpose of counting itself is, to count the actual characters without strlen.

ex.)

while (input[x] != '\0'){
    x++;
}
芯好空 2024-12-22 06:17:39
#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;
}
#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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文