计数字符和字符串n c
这是我班级的任务之一,这是作业的目的:
编写一个程序,其输入是字符和字符串,并且其输出表示字符中出现在字符串中的次数。输出应包括输入字符并使用复数形式, n's ,如果字符出现的次数为不是 完全
1
。您可以假设字符串不包含空格,并且始终包含少于50个字符。
这是我到目前为止的代码,我是C编程的新手,因此我还不知道如何正确声明字符串。到目前为止,我了解到C中没有字符串,就像Java中的字符串
#include <stdio.h>
#include <string.h>
int main(void) {
char userChar;
char userString[50];
int count = 0;
for (int i = 0; i < userChar; i++) {
if (userString[i] == userChar)
count++;
}
printf("%d", count);
if (count != 1)
printf("'s");
return 0;
}
一样
我需要更改代码,从n星期一
1 n
这是我唯一获得的输出,它只输出了一个正确的事情:
0's
This is one of the assignments for my class and this is the objective of the assignment:
Write a program whose input is a character and a string, and whose output indicates the number of times the character appears in the string. The output should include the input character and use the plural form, n's, if the number of times the characters appears is not exactly
1
. You may assume that the string does not contain spaces and will always contain less than 50 characters.
This is the code I have so far and I am new to C programming so I don't know how to declare Strings correctly just yet. So far I learned there are no strings in C like there is in Java and you have to do them as a character array:
#include <stdio.h>
#include <string.h>
int main(void) {
char userChar;
char userString[50];
int count = 0;
for (int i = 0; i < userChar; i++) {
if (userString[i] == userChar)
count++;
}
printf("%d", count);
if (count != 1)
printf("'s");
return 0;
}
For example, if I wanted to input n Monday
and output 1 n
What would I need to change in my code to go from n Monday
to 1 n
This is the only output I am getting, and it only has outputted one thing correctly:
0's
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,我希望这不被视为作弊:-)
第二,您需要将用户查看和用户串定为主要参数,然后在运行时传递它们。他们什么也没分配,所以这就是为什么您获得
0的
第三,您的 条件是错误的。您需要它,因此它仅在字符串的长度上迭代:
for(int i = 0; i&lt; strlen(userString); i ++)
最后,您没有在以前打印用户char的值回报
First, I hope this is not considered cheating :-)
Second, you need to define userChar and userString as arguments for main, and pass them in at run time. They are assigned nothing, so that is why you get
0's
Third, your for condition is wrong. You need this so it only iterates through the length of the string:
for (int i = 0; i < strlen(userString); i++)
Finally, You are not printing the value of userChar prior to the return
首先,您需要输入字符串和一个字符。要计算字符串中字符的出现数量,您可以使用标准字符串函数
strchr
。该程序看起来像以下内容
At first you need to input a string and a character. To count the number of occurrences of the character in the string you can use standard string function
strchr
.The program can look something like the following
预期的输出不是
0的
,它应包括计数字符:例如,如果字符为n
和字符串 nourtay ,则输出应应BE,如果字符串为
eeny-Miny-Miny-Miny-Moe
,则输出将在这里是一个修改版本:
The expected output is not
0's
, it should include the counted character: for example if the character isn
and the stringMonday
, the output should beand if the string is
Eeny-meeny-miny-moe
, the output would beHere is a modified version: