gpt4 book ai didi

c - C中的复数程序,当我运行程序时,复数单词后面有一堆随机的东西

转载 作者:行者123 更新时间:2023-11-30 16:38:41 25 4
gpt4 key购买 nike

所以问题是,每当我输入一个单词时,该单词的复数版本后面都会跟着一堆随机垃圾。这是一个 example of the problem。这是大部分代码(如果您需要更多,我会添加更多),如果您发现任何其他问题,可以告诉我吗?非常感谢。

char * getWord()
{
//make char array of size 20 using malloc
char *word;
word = malloc(20);
//give the msg to user
printf("Enter a word ");
//take input from user
scanf("%s", word);
printf("\n");
//return word
return word;
}
//change word to uppercase
void toUpperCase(char *word)
{
int i = 0;
while (word[i] != '\0')
{
if (word[i] >= 65 && word[i] <= 90)
{
//it is upper case latter so do nothing
}
else if (word[i] >= 97 && word[i] <= 122)
{
word[i] = word[i] - 32;
}
i++;
}
}
//function which will determine which rule applies to given word
int findRule(char *word)
{
int n;//length of word
n = strlen(word);

//last char is Y then rule 1
if (word[n - 1] == 'Y')
return 1;
//end is S,CH,SH then rule 2
else if ((word[n - 1] == 'S') || (n >= 2 && (word[n - 1] == 'H' && (word[n - 2] == 'C' || word[n - 2] == 'S'))))
{
return 2;
}
//else rule 3
else return 3;
}
//rule one
char *ruleOne(char *word)
{
int n;//length of word
n = strlen(word);
//make char array of size 20 using malloc
char *ans;
ans = malloc(n + 2);
//copy all char except last
for (int i = 0; i<n - 1; i++)
{
ans[i] = word[i];
}
//add ies in place of y
ans[n - 1] = 'I';
ans[n] = 'E';
ans[n + 1] = 'S';
return ans;
}

//rule 2
char *ruleTwo(char *word)
{

int n;//length of word
n = strlen(word);
//make char array of size 20 using malloc
char *ans;
ans = malloc(n + 2);
//copy all char
for (int i = 0; i<n; i++)
{
ans[i] = word[i];
}
//add two char E and S
ans[n] = 'E';
ans[n + 1] = 'S';
return ans;
}
//rule 3
char *ruleThree(char *word)
{
int n;//length of word
n = strlen(word);
//make char array of size 20 using malloc
char *ans;
ans = malloc(n + 1);
//copy all char
for (int i = 0; i<n; i++)
{
ans[i] = word[i];
}
//add S
ans[n] = 'S';
return ans;
}

int main()
{
//geets the user
greets();
//open file to write

fptr = fopen("pluralWords.txt", "w");
//if file not open
if (fptr == NULL)
{
printf("Error to open file!\n");
exit(1);
}
else
{
//file opened
printf("%s\n", "The output file pluralWords.txt is open\n");
printf("%s\n", "---------------------------------------\n");
while (1)
{

//ask the msg that user want enter a number or not y/Y(YES), n/N (NO)
printf("%s", "Would you like to enter a word Y (YES) or N (NO)?");
//it is for check yes or no
char c[2];
scanf("%s", c);

if (c[0] == 'n' || c[0] == 'N')
{
printf("Thank you for trying out the Pluralizer!");
printf("\nClosing the file pointer");
fclose(fptr);
break;
}
else if (c[0] == 'y' || c[0] == 'Y') {
printf("\n");
//make cahr array using pointer and malloc function
char *word = malloc(20);
//read input
word = getWord();
//convert to upper case
toUpperCase(word);
printf("%s\n", word);

//find rule
int rule = findRule(word);

printf("Rule is %d\n", rule);

char *ans = malloc(22);
//call coresponding function with rule
if (rule == 1)
{

//call ruleOne
ans = ruleOne(word);

}
else if (rule == 2)
{

//call ruleTwo
ans = ruleTwo(word);
}
else
{

//call ruleThree
ans = ruleThree(word);

}
//output to screen
printf("Word is %s and plural is %s\n\n", word, ans);
//msg to user
printf("Adding the words to the file\n");
//wrtie to file
fprintf(fptr, "%s %s\n", word, ans);
printf("\n\n");

最佳答案

C 字符串要求末尾有一个空字符,即:['s', 't', 'r', 'i', 'n', 'g', '\0'] 其中 '\0' 为空字符。

这个空字符是操作知道字符串长度的方式。字符串函数(例如 strlenstrcpy 等)会查找此空字符,并会继续在内存中查找,直到找到它。在打印出来的情况下,这意味着程序将打印出“垃圾”内存,直到找到设置为\0 的位置。

在您的规则...函数中,您将覆盖此空字符而不是替换它:

ans[n] = 'E';     //this was NULL (/0)
ans[n + 1] = 'S';
return ans; //ans is now missing a NULL terminator

顺便说一句,您还分配了内存但从未释放它,这会导致内存泄漏。在 C 中,将缓冲区传递给函数通常比让其 malloc 内存并返回它更干净(例如 char*ruleOne(char*word) 会更好,因为 voidruleOne(char * in_word, char* out_word, int out_len)) 然后您可以简单地在 main() 中创建一个缓冲区作为 char wordbuf[128] 而不必担心自己管理内存。

关于c - C中的复数程序,当我运行程序时,复数单词后面有一堆随机的东西,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47403929/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com