gpt4 book ai didi

c - 我正在尝试反转字符串,以便它可以将字母 y 读为元音

转载 作者:太空宇宙 更新时间:2023-11-04 02:53:27 24 4
gpt4 key购买 nike

我正在反转字符串,例如,如果我输入单词“try”,它会将 y 读作元音并打印出该单词是合法的。

reverse 函数有效,但它不会传递给 switch 语句值。

#include //#include

void rev(char *);
void legal(char string);

int main()
{

char string[50];
char temp;
printf("Enter any string:");
scanf(" %s",&string);
//printf("Before reversing the string %s\t",string);

rev(string);
printf("\nReverse String is %s",string);
temp = legal(string);
printf("\nReverse String is %s",temp);

return 0;
}

void legal(char string)

{


switch(string)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
printf("\nWord is legal");
break;
default:
printf("\nWord is not legal");
break;
}

return 0;

}


//reversing string using pointers
void rev(char *str)

{
char *str1,temp;
str1=str;
while(*str1!='\0')
str1++;
str1--;
while(str<str1)
{
temp=*str;
*str=*str1;
*str1=temp;
str++;
str1--;
}
}

最佳答案

我发现您将 char * 误认为是 char

相反,您必须逐个字符地循环遍历字符串,并用标志判断该词是否合法..

这是我的做法,没有提前测试代码:

void rev(char *);
void legal(char *string);

int main()
{

char string[50];
int temp;
printf("Enter any string:");
scanf(" %s",&string);
//printf("Before reversing the string %s\t",string);

rev(string);
printf("\nReverse String is %s",string);
temp = legal(string);

printf("\nLegal? %d",temp);

return 0;
}

int legal(char *string)
{
char *ch = string;
int flag = 0;
while(*ch != '\0'){
switch(*ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'Y':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
flag = 1;
break;
default:
//nothing
break;
}
}
if(flag == 1){
printf("word is legal!");
}else{
printf("word is illegal!");
}
return flag;
}

关于c - 我正在尝试反转字符串,以便它可以将字母 y 读为元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19802636/

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