【发布时间】:2021-07-06 11:44:07
【问题描述】:
分段错误:有人可以帮助我理解我的错误吗?
目标:创建一个只有大写字母的新字符串。
另外,我试图通过参考 ASCII 表来识别我不想要的字母,希望这是正确的方法。
CS50 IDE,学习哈佛的 CS50 课程
#include <ctype.h>
#include <cs50.h>
#include <stdio.h>
#include <string.h>
// Points assigned to each letter of the alphabet
int POINTS[] = {1, 3, 3, 2, 1, 4, 2, 4, 1, 8, 5, 1, 3, 1, 1, 3, 10, 1, 1, 1, 1, 4, 4, 8, 4, 10};
// int compute_score(string word);
int put_down_caps_only(string word);
int main(void)
{
string word1 = get_string("Player 1: ");
string word1CapsOnly="";
char chr;
for(int i=0; i<strlen(word1);i++)
{
if(word1[i]<123&&word1[i]>96)
{
// use to upper function
// is lower would work here nicely
chr = toupper(word1[i]);
strncat(word1CapsOnly, &chr, 1);
}
else if(word1[i]<65 || word1[i]>122)
{
//ignore
}
else
{
//just add, upper alredy
strncat(word1CapsOnly, &word1[i], 1);
}
}
printf("%s", word1CapsOnly);
// int score1 = compute_score(word1);
// TODO: Print the winner
}
/* int compute_score(string word)
{
// TODO: Compute and return score for string
} */
【问题讨论】:
-
AFAIK C 中没有
string... 是typedef到char *吗? -
请显示minimal reproducible example。如果这是您的第一篇文章,您应该阅读我们的 tour 并阅读 How to Ask。也许看看reference for
strncat,我认为你不明白它的作用。 -
@kiner_shah,照你说的做,谢谢
-
@selvin 欢迎来到 CS50,为了避免混淆,他们假装 C 确实 有
string类型。 (不用说,这种虚构实际上并没有避免混淆。) -
请不要编辑此问题来提出新问题。如果您有新问题,请ask a new question。