gpt4 book ai didi

c - 陷入无限循环,不知道我做错了什么

转载 作者:行者123 更新时间:2023-11-30 20:08:43 26 4
gpt4 key购买 nike

程序可以编译,但是当我尝试使用它时,出现无限循环。我哪里做错了。我将评论添加到我想要完成的任务中。

我尝试将其更改为 for 循环,但仍然遇到问题。我想我应该坚持使用 while 循环来完成我想要完成的任务。

  //Declare the arrays to hold the strings
char str[21], vowels[21], consonants[21];
int i=0;

//Declare the pointers
char *strPointer, *vowelPointer, *consonantPointer;

//Print out the prompt to the user
printf("Enter a string (20 characters maximum): ");

//Scan the user input into str
//Only allow 20 characters
scanf("%s", str);

//Set strPointer to the beginning of the user's string
strPointer = str;

//Set vowelPointer to the beginning of the vowels string
vowelPointer = vowels;

//Set consonantPointer to the beginning of tht consonant string
consonantPointer = consonants;

//Loop through the user's string until the end of the string
while(*strPointer !='\0')
{
//Check if what strPointer is pointing to is a vowel
if(strPointer[i]=='A'||strPointer[i]=='a'||strPointer[i]=='E'||strPointer[i]=='e'||strPointer[i]=='I'||strPointer[i]=='i'||strPointer[i]=='O'||strPointer[i]=='o'||strPointer[i]=='U'||strPointer[i]=='u')
{

//Move the letter from strPointer to vowelPointer
strPointer=vowelPointer
;

//Move the vowelPointer
vowelPointer=vowels
;

}
else
{

//Move the letter from strPointer to consonantPointer
strPointer=consonantPointer
;

//Move the consonantPointer
consonantPointer=consonants
;

}

//Move the strPointer
strPointer=str;
}

//Add null terminators where appropriate
strPointer[21]='\0';
str[21]='\0';

//Set the vowel and consonant pointers back to the beginning of their strings
vowelPointer[0];
consonantPointer[0];

//Print the original string and the resulting vowel and consonant strings
printf("Original string: %s\n", str);
printf("%s\n", vowelPointer);
printf("%s\n", consonantPointer);

最后的 printf 语句对输出进行了解释。输入字符串,重新打印,元音和辅音分开并列出。

最佳答案

我想提出一些建议来帮助您。

首先,当您扫描用户输入时,明智的做法是使用缓冲区。然后,将缓冲区的内容 strcpy 到 char 数组中。这将有助于防止溢出。有关此主题的更多详细信息,请参阅下面的代码。

其次,您可以使用 for 循环来迭代每个字母。我希望我的回答能帮助您理解我的意思。

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int main(void)
{

//Declare the arrays to hold the strings
char c, str[21], vowels[21], consonants[21], buffer[21];
int i = 0, j = 0, h = 0;

//Declare the pointers
char *strPointer, *vowelPointer, *consonantPointer;

//Print out the prompt to the user
printf("Enter a string (20 characters maximum): ");

//Scan the user input into str
scanf("%s", buffer);

// Copy the buffer into the str
strcpy(str, buffer);

// go letter by letter checking if it is a vowel or consonant
for (int i = 0; str[i]; i++)
{
// make the character uppercase
c = toupper(str[i]);

// if the letter is a vowel add the letter to the vowel array,
// then increase the position
if (c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U')
vowels[j++] = str[i];
else
// add the letter to the consonants array, then increase the position
consonants[h++] = str[i];
}

// properly terminate the strings
vowels[j] = '\0';
consonants[h] = '\0';

//Print the original string and the resulting vowel and consonant strings
printf("Original string: %s\n", str);
printf("%s\n", vowels);
printf("%s\n", consonants);

return 0;
}

关于c - 陷入无限循环,不知道我做错了什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55696986/

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