C中的字符计数器
Character counter in C
这段 C 语言的代码应该计算 table 中所有等于 A 的字符 ..但是它不起作用我需要帮助来找到问题!! ~~
我开始使用 for 循环读取用户提供的 table 字符,然后我使用另一个 for 循环来计算等于 A~~
的字符数
请提出任何建议
#include <stdio.h>
#include <stdlib.h>
int main()
{
char T[100],A;
int i,N,b=0;
printf("give the number of your table's columns \n");
scanf("%d", &N);
if (N > 0 && N <= 100) {
for (i; i < N; i++) {
scanf("%c", &T[i]);
printf("give the character of the column number %d \n", i);
scanf("%c",&T[i]);
}
for (i; i < N; i++) {
if (T[i] = 'A') b++;
printf("the number of characters equal to A is %d",b);
}
return 0;
}
}
你必须明确解释它为什么不起作用:什么错误信息,你是如何编译的,你需要数字 100 等吗?
在 MikeCAT 评论之上,您必须查看 if (N > 0 && N <= 100) :您会遇到一些问题,因为您尝试在索引 100 处写入数组,但您的数组以索引 99(大小 - 1)结束。
你可能不需要扫描两次,所以你可以删除第一个:scanf("%c",&T[i])
你应该用gcc -Wall -Werror -Wextra
编译
另外 printf("the number of characters equal to...")
应该在你的循环之外(在 }
之后)
你的return 0;
也应该在你的if条件之外并且在最后的}
之前
让我知道它是否有帮助,然后从修改您的代码开始。
"supposed to count all characters equal to A in a table ..but it doesn't work I need help to find the Problem !!"
编译器警告将帮助您找到问题。
如果您在编译时没有看到警告,请将您的编译器设置为显示它们。 (in GCC 例如使用 -Wall
)
在打开警告的情况下编译时你应该在编译时看到类似这样的信息:
Build Status (so.prj - Debug)
s0_15.c - 3 warnings
22, 22 warning: using the result of an assignment as a condition without parentheses
22, 22 note: place parentheses around the assignment to silence this warning
22, 22 note: use '==' to turn this assignment into an equality comparison
7, 17 warning: unused variable 'A'
14, 14 warning: variable 'i' is uninitialized when used here
8, 10 note: initialize the variable 'i' to silence this warning
此外,当进入第二个 for()
循环时,i
不会再次初始化,而是保留完成第一个循环时的值。它需要重新初始化为零才能使第二个循环工作...(请注意,我的编译器 没有标记此 ,幸运的是我曾一度被告知 learn how to set and use breakpoints 我在这里做的,让我在 运行 时间内发现 i
的价值。
按照上述警告提供的指导,进行了修改以允许代码 运行,并按照您的描述执行。
请参阅在线评论以了解对上述警告的回应:
int main(void)//added void to match accepted prototype of main
{
char T[100] = {0},A;//A is not used, should be deleted
//initialize array to all zeros.
int i=0,N=0,b=0;//initialized i (and N)
printf("give the number of your table's columns \n");
scanf("%d", &N);
if (N > 0 && N <= 100) {
for (i; i < N; i++) {
scanf("%c", &T[i]);
printf("give the character of the column number %d \n", i);
scanf("%c",&T[i]);
}
// `i` is already equal to N here. it will never enter the loop
//for (i; i < N; i++) {
for (i = 0; i < N; i++) {//initialized i to zero
//if (T[i] = 'A') b++;//this is not what you want
if (T[i] == 'A') b++; //replace assignment '='
//with comparison '==' operator
//Note, optionally move the following printf() statement to below
//the loop so it is called only once after counting is done.
printf("the number of characters equal to A is %d\n",b);//added '\n' for readability of output.
}
//return 0;// not here...
}
return 0;// ...but here
}
C 有一个语言特性(或缺陷)。您可以在通常不需要的上下文中使用赋值运算符。
if (T[i] = 'A') b++;
正在更改 T[i]
而不是将其划分为 A
。这意味着如果更改的值不是 0
.
,则 if
语句正在注册
使用==
,您将再次进行比较。
这段 C 语言的代码应该计算 table 中所有等于 A 的字符 ..但是它不起作用我需要帮助来找到问题!! ~~ 我开始使用 for 循环读取用户提供的 table 字符,然后我使用另一个 for 循环来计算等于 A~~
的字符数请提出任何建议
#include <stdio.h>
#include <stdlib.h>
int main()
{
char T[100],A;
int i,N,b=0;
printf("give the number of your table's columns \n");
scanf("%d", &N);
if (N > 0 && N <= 100) {
for (i; i < N; i++) {
scanf("%c", &T[i]);
printf("give the character of the column number %d \n", i);
scanf("%c",&T[i]);
}
for (i; i < N; i++) {
if (T[i] = 'A') b++;
printf("the number of characters equal to A is %d",b);
}
return 0;
}
}
你必须明确解释它为什么不起作用:什么错误信息,你是如何编译的,你需要数字 100 等吗?
在 MikeCAT 评论之上,您必须查看 if (N > 0 && N <= 100) :您会遇到一些问题,因为您尝试在索引 100 处写入数组,但您的数组以索引 99(大小 - 1)结束。
你可能不需要扫描两次,所以你可以删除第一个:scanf("%c",&T[i])
你应该用gcc -Wall -Werror -Wextra
另外 printf("the number of characters equal to...")
应该在你的循环之外(在 }
之后)
你的return 0;
也应该在你的if条件之外并且在最后的}
让我知道它是否有帮助,然后从修改您的代码开始。
"supposed to count all characters equal to A in a table ..but it doesn't work I need help to find the Problem !!"
编译器警告将帮助您找到问题。
如果您在编译时没有看到警告,请将您的编译器设置为显示它们。 (in GCC 例如使用 -Wall
)
在打开警告的情况下编译时你应该在编译时看到类似这样的信息:
Build Status (so.prj - Debug)
s0_15.c - 3 warnings
22, 22 warning: using the result of an assignment as a condition without parentheses
22, 22 note: place parentheses around the assignment to silence this warning
22, 22 note: use '==' to turn this assignment into an equality comparison
7, 17 warning: unused variable 'A'
14, 14 warning: variable 'i' is uninitialized when used here
8, 10 note: initialize the variable 'i' to silence this warning
此外,当进入第二个 for()
循环时,i
不会再次初始化,而是保留完成第一个循环时的值。它需要重新初始化为零才能使第二个循环工作...(请注意,我的编译器 没有标记此 ,幸运的是我曾一度被告知 learn how to set and use breakpoints 我在这里做的,让我在 运行 时间内发现 i
的价值。
按照上述警告提供的指导,进行了修改以允许代码 运行,并按照您的描述执行。
请参阅在线评论以了解对上述警告的回应:
int main(void)//added void to match accepted prototype of main
{
char T[100] = {0},A;//A is not used, should be deleted
//initialize array to all zeros.
int i=0,N=0,b=0;//initialized i (and N)
printf("give the number of your table's columns \n");
scanf("%d", &N);
if (N > 0 && N <= 100) {
for (i; i < N; i++) {
scanf("%c", &T[i]);
printf("give the character of the column number %d \n", i);
scanf("%c",&T[i]);
}
// `i` is already equal to N here. it will never enter the loop
//for (i; i < N; i++) {
for (i = 0; i < N; i++) {//initialized i to zero
//if (T[i] = 'A') b++;//this is not what you want
if (T[i] == 'A') b++; //replace assignment '='
//with comparison '==' operator
//Note, optionally move the following printf() statement to below
//the loop so it is called only once after counting is done.
printf("the number of characters equal to A is %d\n",b);//added '\n' for readability of output.
}
//return 0;// not here...
}
return 0;// ...but here
}
C 有一个语言特性(或缺陷)。您可以在通常不需要的上下文中使用赋值运算符。
if (T[i] = 'A') b++;
正在更改 T[i]
而不是将其划分为 A
。这意味着如果更改的值不是 0
.
if
语句正在注册
使用==
,您将再次进行比较。