良い一日!私たちの先生は、単語または一連の数字が回文であるか、スタックを使用していないかを判断するように私たちに要求しました。私はすでにそれを終えました。しかし、今はもっと練習したいので、空白やその他の無関係な文字を削除して、文が回文であるかどうかを判断しようとしています(注:宿題の一部ではありません)私のコードはすでに機能しています(うまくいけば)が、散らかっています。だから私はそれを改善したいと思います。先生から使用しないように言われたので、goto関数を削除したいと思います。goto関数を使用せずにifステートメントを終了するにはどうすればよいですか?前もって感謝します。また、私のコードはブルートフォース方式で行われているため、文が回文であるかどうかを確認する他の方法もあります。私のコードは次のとおりです。注(ここには構造体とポップアンドプッシュ関数を含めたり貼り付けたりしませんでした)
int main(){
char word[11];
char temp[11];
char value;
int i=0, x=0, n=0, length=0;
Stack*head = NULL;
printf("Please type the word: ");
gets(word);
length = strlen(word);
while(i<length){
if(isspace(word[i]) || !isalpha(word[i])) {
if(isdigit(word[i])) goto NEXT; // i used the goto function here
i++;
continue;
}
NEXT:
temp[n]=word[i];
push(&head, word[i]);
i++;
n++;
}
temp[n]='\0';
while(x<n){
value = pop(&head);
if (value==temp[x]){
x++;
continue;
}
break;
}
if(x==n) printf("Yehey! It is a palindrome.");
else printf("Sorry, It is not a palindrome.");
getch();
}
あなたの提案に基づいています。これが私の改善されたコードです:
int main(){
char word[11];
char temp[11];
int i=0, n=0;
int flag = 1;
Stack*head = NULL;
printf("Please type the word: ");
fgets(word, 11, stdin);
for(i = 0; word[i]!='\0' ; i++){
if(isalnum(word[i])) {
temp[n]=word[i];
push(&head, word[i]);
n++;
}
}
temp[n]='\0';
for(i=0; temp[i]!='\0'; i++){
if (pop(&head)!=temp[i]){
flag = 0;
break;
}
}
if (flag==1) printf("Yehey! It is a palindrome.");
else printf("Sorry, It is not a palindrome.");
getch();
}