gpt4 book ai didi

c - do while 循环中的字符串

转载 作者:行者123 更新时间:2023-11-30 19:44:46 29 4
gpt4 key购买 nike

// PigLatinFinal.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>

void Heading (void)
{
printf("************************************************************\n");
printf("* Pig Latin *\n");
printf("* *\n");
printf("* *\n");
printf("* Welcome *\n");
printf("* *\n");
printf("* In this program, the user will enter a word in English *\n");
printf("* The program will then return the word in Pig Latin. *\n");
printf("* The word cannot be longer than 7 letters. You can *\n");
printf("* translate as many words as you wish. Enter 'END' without *\n");
printf("* the quotation marks to end the program. 'END is case *\n");
printf("* sensitive *\n");
printf("* *\n");
printf("************************************************************\n");
printf("\n\n\n\n");
}

int main(void)
{
Heading();
char str1[20];
int i =0;
do
{

int count =0;
do
{
printf("Enter a word in English for the program to return in Pig Latin: ");
scanf("%s", str1);
while(str1[count] != '\0')
++count;
if (count>7)
{
printf("\n\nInvalid Entry. Please enter a word with no more than 7 letters\n\n");
count=0;
}
}while (count>7);//loop to perform if invalid word is entered

//我输入大于 7 个字母的单词,它仍然以 Pig Latin 给出,而不是提示输入另一个单词。我试图放入一个 while 循环,但它只是给出了一个无限循环

         //while (count<7)
//
printf("\n%s in Pig Latin: ",str1);
for (i=1;str1[i];++i)
{
printf("%c",str1[i]);
}
printf("%c",str1[0]);
printf("ay");
printf("\n\n\n");
//}


}while(str1!= "END");

//这里也不是输入END结束。我究竟做错了什么?

return 0;
}

最佳答案

改变

}while (count>7);//loop to perform if invalid word is entered

}while (count==0);//loop to perform if invalid word is entered

还有

}while(str1!= "END");

}while(strcmp(str1,"END")!=0);

另外,不要忘记包含 string.h

关于c - do while 循环中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27392352/

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