単語を入力するたびに、単語の複数のバージョンにはランダムなジャンクが続きます。ここには example of the problem があります。これはコードの大部分です(それ以上のものが必要な場合は、さらに追加します)。他の問題がある場合は、教えてください。 ありがとうございます。ヌル文字であること '\ 0' で['s', 't', 'r', 'i', 'n', 'g', '\0']
:複数のプログラムをCで実行すると、複数の単語にはランダムなものがたくさんあります。
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");
ヌルターミネーターはありません。 Googleは、それは本当にCの文字列を読む。 –