试图用 C 写一个字母计数器
trying to write a letter counter in C
我正在尝试制作一个程序来计算用户输入的字母数。当我 运行 我的程序时,它在 count_letters(){ // writing the function to count number of letters
行显示 error: expected ';' after expression
?函数末尾需要分号吗?我也觉得我没有正确地解决这个问题,有人能赐教吗:(
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(); //function to count letters
int number_of_letters; //length of the string input
int letterCount; // counter for number of letters in the string input
int main(void) {
string text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
count_letters() { // writing the function to count number of letters
for (int i = 0; i < number_of_letters; i++) {
if (isalpha(text[i])) {
letterCount++;
}
}
}
}
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(); //function to count letters
int number_of_letters; //length of the string input
//initialize letterCount to 0 so that doing letterCount++ does not add 1 to a garbage value
int letterCount = 0; // counter for number of letters in the string input
//define string as a global value if you don't want to refer to it again and again by pass it as parameter or reference
string text;
void count_letters() { //since it's not returning anything, it's void not integer
for(int i = 0; i < number_of_letters; i++) {
if(isalpha(text[i])
letterCount++;
}
}
int main(void){
text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
}
希望这对您有所帮助。修改的地方都有评论
我可能会认为你的知识太基础了,如果是这样还请见谅。
我相信您犯了一个小错误,这可能是由另一种语言的工作方式引起的。问题在于您声明 count_letters().
的方式
要在 C 中正确声明一个函数,首先退出任何现有函数,然后输入任何变量类型作为函数的 return 类型、函数名称,然后在括号中输入您的函数的参数。之后,您的函数代码可以插入括号之间,就像您所做的那样。
注意你也可以在没有代码的情况下声明函数,然后将函数的代码放在下面。
您可能还想在外部声明您的字符串以避免处理指针。
下面是一个函数声明的例子:
int foo(int amount)
因此您的代码应该看起来像这样:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
void count_letters(); //function to count letters
int number_of_letters; //length of the string input
int letterCount; // counter for number of letters in the string input
string text;
int main(void) {
text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
count_letters();
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
}
void count_letters() { // writing the function to count number of letters
for (int i = 0; i < number_of_letters; i++) {
if (isalpha(text[i])) {
letterCount++;
}
}
}
如果我没有理解或正确回答您的问题,请原谅,如果我能得到更多细节,我将很高兴改进自己。缺少 cs50.h 库,我无法确定我的代码是否有效。
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(char *); //function to count letters
int main(void)
{
char* text = get_string("Text: "); // getting string input
printf("Number of letters: %i\n", count_letters(text)); //printing the number of letters in the string input
}
int count_letters(char *text)
{ // writing the function to count number of letters
int letterCount,number_of_letters; //length of the string input
number_of_letters = strlen(text); //storing the length of string here
for(int i = 0; i < number_of_letters; i++){
if(isalpha(text[i]))
{
letterCount++;
}
return letterCount;
}
虽然您可以随意使用 strlen
来获取 text
中的初始字符数,但没有必要。在 C 中,"string" 中的最后一个字符是 nul-terminating 字符 '[=21=]'
(其 ASCII 值为 0
)。这就是普通字符数组与字符串的区别。这是所有字符串函数知道何时停止扫描字符的方式。
所以你不需要事先知道一个字符串中有多少个字符。例如,在您声明的 "Text: "
提示符处输入字符串 "hello"
。
string text = get_string("Text: ");
当你在提示符下输入"hello"
时:
Text: hello
字符串在内存中存储为:
+---+---+---+---+---+---+
| h | e | l | l | o |[=12=] |
+---+---+---+---+---+---+
^
|
text
其中指针text
指向字符串第一个字符在内存中的地址。使用字符串以 nul-terminating 字符结尾的事实,您可以简单地从头开始向前扫描,直到到达 '[=21=]'
(相当于普通的旧 0
).
您可以使用 for
循环并使用索引进行迭代:
for (int i = 0; text[i]; i++)
// do whatever with the character text[i]
或者您可以使用指针并简单地增加指针,使其指向字符串中的下一个字符,直到达到 '[=21=]'
:
string p = text;
while (*p) {
// do whatever with *p (the character at that address)
p++;
}
将最后一个版本放入您的 int count_letters (string s)
函数(将指向您的字符串的指针作为参数传递给函数)和 returns 一个 int
代表字母的数量(仅包括 [a-zA-Z]
),您的函数将缩减为:
int count_letters (string s)
{
int n = 0;
while (*s) /* while not the nul-character */
if (isalpha (*s++)) /* check if current is letter, advance ptr */
n++; /* increment letter count */
return n; /* return letter count */
}
你 main()
函数,不需要调用 strlen()
然后减少到:
int main (void) {
string text = get_string ("Text: ");
printf ("Number of letters: %d\n", count_letters(text));
}
把它放在一起并包括所需的 headers,你将有:
#include <stdio.h>
#include <ctype.h>
#include "cs50.h"
int count_letters (string s)
{
int n = 0;
while (*s) /* while not the nul-character */
if (isalpha (*s++)) /* check if current is letter, advance ptr */
n++; /* increment letter count */
return n; /* return letter count */
}
int main (void) {
string text = get_string ("Text: ");
printf ("Number of letters: %d\n", count_letters(text));
}
例子Use/Output
用 libcs50.so
编译和 link 然后,例如你会:
$ ./bin/ltrcountcs50
Text: hello world
Number of letters: 10
了解 C 中的 "string" 是什么(CS50 使用的 typedef char* string;
的不幸选择除外),让您无需知道如何扫描字符串中的字符预先包含许多字符。 (并且还可以帮助您理解为什么未能向任何期望 nul-terminated[=72 的 C 字符串函数提供 nul-terminated 字符串=] 字符串作为参数导致 未定义的行为 -- 他们无法知道何时停止扫描字符)。
检查一下,如果您还有其他问题,请告诉我。
我正在尝试制作一个程序来计算用户输入的字母数。当我 运行 我的程序时,它在 count_letters(){ // writing the function to count number of letters
行显示 error: expected ';' after expression
?函数末尾需要分号吗?我也觉得我没有正确地解决这个问题,有人能赐教吗:(
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(); //function to count letters
int number_of_letters; //length of the string input
int letterCount; // counter for number of letters in the string input
int main(void) {
string text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
count_letters() { // writing the function to count number of letters
for (int i = 0; i < number_of_letters; i++) {
if (isalpha(text[i])) {
letterCount++;
}
}
}
}
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(); //function to count letters
int number_of_letters; //length of the string input
//initialize letterCount to 0 so that doing letterCount++ does not add 1 to a garbage value
int letterCount = 0; // counter for number of letters in the string input
//define string as a global value if you don't want to refer to it again and again by pass it as parameter or reference
string text;
void count_letters() { //since it's not returning anything, it's void not integer
for(int i = 0; i < number_of_letters; i++) {
if(isalpha(text[i])
letterCount++;
}
}
int main(void){
text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
}
希望这对您有所帮助。修改的地方都有评论
我可能会认为你的知识太基础了,如果是这样还请见谅。
我相信您犯了一个小错误,这可能是由另一种语言的工作方式引起的。问题在于您声明 count_letters().
的方式要在 C 中正确声明一个函数,首先退出任何现有函数,然后输入任何变量类型作为函数的 return 类型、函数名称,然后在括号中输入您的函数的参数。之后,您的函数代码可以插入括号之间,就像您所做的那样。
注意你也可以在没有代码的情况下声明函数,然后将函数的代码放在下面。
您可能还想在外部声明您的字符串以避免处理指针。
下面是一个函数声明的例子:
int foo(int amount)
因此您的代码应该看起来像这样:
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
void count_letters(); //function to count letters
int number_of_letters; //length of the string input
int letterCount; // counter for number of letters in the string input
string text;
int main(void) {
text = get_string("Text: "); // getting string input
number_of_letters = strlen(text); //storing the length of string here
count_letters();
printf("Number of letters: %i\n", letterCount); //printing the number of letters in the string input
}
void count_letters() { // writing the function to count number of letters
for (int i = 0; i < number_of_letters; i++) {
if (isalpha(text[i])) {
letterCount++;
}
}
}
如果我没有理解或正确回答您的问题,请原谅,如果我能得到更多细节,我将很高兴改进自己。缺少 cs50.h 库,我无法确定我的代码是否有效。
#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <ctype.h>
int count_letters(char *); //function to count letters
int main(void)
{
char* text = get_string("Text: "); // getting string input
printf("Number of letters: %i\n", count_letters(text)); //printing the number of letters in the string input
}
int count_letters(char *text)
{ // writing the function to count number of letters
int letterCount,number_of_letters; //length of the string input
number_of_letters = strlen(text); //storing the length of string here
for(int i = 0; i < number_of_letters; i++){
if(isalpha(text[i]))
{
letterCount++;
}
return letterCount;
}
虽然您可以随意使用 strlen
来获取 text
中的初始字符数,但没有必要。在 C 中,"string" 中的最后一个字符是 nul-terminating 字符 '[=21=]'
(其 ASCII 值为 0
)。这就是普通字符数组与字符串的区别。这是所有字符串函数知道何时停止扫描字符的方式。
所以你不需要事先知道一个字符串中有多少个字符。例如,在您声明的 "Text: "
提示符处输入字符串 "hello"
。
string text = get_string("Text: ");
当你在提示符下输入"hello"
时:
Text: hello
字符串在内存中存储为:
+---+---+---+---+---+---+
| h | e | l | l | o |[=12=] |
+---+---+---+---+---+---+
^
|
text
其中指针text
指向字符串第一个字符在内存中的地址。使用字符串以 nul-terminating 字符结尾的事实,您可以简单地从头开始向前扫描,直到到达 '[=21=]'
(相当于普通的旧 0
).
您可以使用 for
循环并使用索引进行迭代:
for (int i = 0; text[i]; i++)
// do whatever with the character text[i]
或者您可以使用指针并简单地增加指针,使其指向字符串中的下一个字符,直到达到 '[=21=]'
:
string p = text;
while (*p) {
// do whatever with *p (the character at that address)
p++;
}
将最后一个版本放入您的 int count_letters (string s)
函数(将指向您的字符串的指针作为参数传递给函数)和 returns 一个 int
代表字母的数量(仅包括 [a-zA-Z]
),您的函数将缩减为:
int count_letters (string s)
{
int n = 0;
while (*s) /* while not the nul-character */
if (isalpha (*s++)) /* check if current is letter, advance ptr */
n++; /* increment letter count */
return n; /* return letter count */
}
你 main()
函数,不需要调用 strlen()
然后减少到:
int main (void) {
string text = get_string ("Text: ");
printf ("Number of letters: %d\n", count_letters(text));
}
把它放在一起并包括所需的 headers,你将有:
#include <stdio.h>
#include <ctype.h>
#include "cs50.h"
int count_letters (string s)
{
int n = 0;
while (*s) /* while not the nul-character */
if (isalpha (*s++)) /* check if current is letter, advance ptr */
n++; /* increment letter count */
return n; /* return letter count */
}
int main (void) {
string text = get_string ("Text: ");
printf ("Number of letters: %d\n", count_letters(text));
}
例子Use/Output
用 libcs50.so
编译和 link 然后,例如你会:
$ ./bin/ltrcountcs50
Text: hello world
Number of letters: 10
了解 C 中的 "string" 是什么(CS50 使用的 typedef char* string;
的不幸选择除外),让您无需知道如何扫描字符串中的字符预先包含许多字符。 (并且还可以帮助您理解为什么未能向任何期望 nul-terminated[=72 的 C 字符串函数提供 nul-terminated 字符串=] 字符串作为参数导致 未定义的行为 -- 他们无法知道何时停止扫描字符)。
检查一下,如果您还有其他问题,请告诉我。