在C中,使用while循环来计算用户输入的字符数
我是一个完全的编程新手,并且在我知道的非常基本的事情上遇到了很多麻烦。我创建一个程序,用户输入一个单词,我必须使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
本着标记帖子的精神,我删除了我的实现,并提供提示:
C 中的字符串以 NULL 结尾,这意味着如果您有一个像“qW3rTy”这样的字符串指向通过 char[30] 来表示,在内存中,这实际上看起来像这样:
'\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:
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.如果您将字符串存储在字符数组中,例如 s,最简单的解决方案是使用内置的 C 函数 strlen,它可以计算字符串中的字符数。
使用 while 循环,您需要从头开始检查直到到达 NULL('\0') 字符,如下所示:
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
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:
你可以算出 x 的计数,与 strlen 结合起来是没有意义的。
只有 x = i。
如果计数本身的目的是计算实际字符而不使用strlen。
前任。)
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.)