1

フレーズが回文 (左から右まで同じ単語) であるかどうかを判断しようとしていますが、うまくいきません。どうしたの?、ポインタも再帰も文字列型変数も使えない

#include <stdio.h>

#include <string.h>

int main()

{

 int i,j = 0,length;
 char space = ' ';
 char phrase [80],phrase2[80],phrase3[80];

 printf("Give me the phrase: ");
 gets(phrase);
 length = strlen(phrase);

 for(i =0; i <= length - 1; i++)
 {
  if(phrase[i] != space)    //Makes the phrase without spaces
  {
   phrase2[i] = phrase[i];
   j++;
  }
 }

 for(i = length -1; i >= 0;i--)
 {
  if(phrase[i] != space)    //Makes the phrase backwards an without spaces
  {
   phrase3[j] = phrase[i];
   j++;
  }
 }

 length = strlen(phrase2);

 for(i =0; i <= length -1;i++)      //Compare the phrases to know if they are the same
 {
  if(phrase2[i] != phrase3[i])
  {
   printf("It's not a palindrome\n"); 
   return 0;
  }
 }
 printf("It's a palindrome\n");
 return 0; 
}
4

5 に答える 5

2

これを試して:

 for(i =0, j=0; i <= length - 1; i++)
 {
  if(phrase[i] != space)    //Makes the phrase without spaces
  {
   phrase2[j] = phrase[i];
   j++;
  } 
 }

 for(i = length -1, j = 0; i >= 0;i--)
 {
  if(phrase[i] != space)    //Makes the phrase backwards an without spaces
  {
   phrase3[j] = phrase[i];
   j++;
  }
 }

 length = j;

アップデート

Praetorian の投稿に応えて、文字列をコピーせずにそれを行うコードを次に示します。

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

int main()
{
  int i, j, length;
  char space = ' ';
  char phrase[80];

  printf("Give me the phrase: ");
  gets(phrase);
  length      = strlen(phrase);

  for( i = 0, j = length - 1; i < j; i++, j-- ) {
    while (phrase[i] == space) i++;
    while (phrase[j] == space) j--;
    if( phrase[i] != phrase[j] ) {
      printf("It's not a palindrome\n");
      return 0;
    }
  }

  printf("It's a palindrome\n");
  return 0; 
}
于 2010-10-14T01:11:46.117 に答える
1

あなたの質問はすでに他の人から回答されていますがphrase3、逆の文字列を保持するためにコピーを作成する必要がないことを示すために、このコードを投稿しています。

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

int main()
{

  int i, j, length, halfLength;
  char space = ' ';
  char phrase1[80], phrase2[80];

  printf("Give me the phrase: ");
  gets(phrase1);
  length      = strlen(phrase1);

  for( i = 0, j = 0; i <= length; ++i ) {
    if( phrase1[i] != space ) {    //Makes the phrase1 without spaces
      phrase2[j++] = phrase1[i];
    }
  }

  length      = strlen(phrase2);
  halfLength  = length / 2;

  for( i = 0, j = length - 1; i < halfLength; ++i, --j ) {
    if( phrase2[i] != phrase2[j] ) {
      printf("It's not a palindrome\n");
      return 0;
    }
  }

  printf("It's a palindrome\n");
  return 0; 
}
于 2010-10-14T01:58:44.380 に答える
1

2 番目のループの前に、j=0 を設定します。その後、動作するはずです。

PS: 3 つの文字列を出力してデバッグした場合は、数分で解決できます。何が問題なのかわからない場合は、途中のステップで変数の値を出力してください。そうすれば、問題がどこで発生し、それが何であるかを知ることができます。

于 2010-10-14T01:31:02.080 に答える
0

これは私が思いついたものです:

#include <stdio.h>
void main() {
char a[50],b[50];
int i=0,j,ele,test=0,x;
while((a[i]=getchar())!='\n') {
if(a[i]!=' ' && a[i]!=',') //do not read whitespaces and commas(for palindromes like "Ah, Satan sees Natasha")
i++;
}
a[i]='\0';
ele=strlen(a);
// Convert string to lower case (like reverse of Ava is avA and they're not equal)
for(i=0; i<ele; i++)
if(a[i]>='A'&&a[i]<='Z')
a[i] = a[i]+('a'-'A');
x = ele-1;
for(j=0; j<ele; j++) {
b[j] = a[x];
x--;
}
for(i=0; i<ele; i++)
if(a[i]==b[i])
test++;
if(test==ele)
printf("You entered a palindrome!");
else
printf("That's not a palindrome!");
}

回文にはおそらく最善の方法ではありませんが、自分でこれを作ったのは1時間かかったことを誇りに思います:(笑

于 2013-01-22T19:14:44.967 に答える
-1

なぜ使用しないのstd::stackですか?それぞれが入力文字列の長さを反復する 2 つのループが必要になります。最初のループでは、入力文字列を 1 回調べて、各文字をスタックにプッシュします。2 番目のループでは、スタックから文字をポップし、インデックスの文字と比較します。ループが終了する前に不一致が発生した場合、回文はありません。これの良いところは、偶数/奇数の長さのコーナーケースについて心配する必要がないことです。それはうまくいくでしょう。

(もしあなたがそうしたいと思っているなら、1 つのスタック (LIFO) と 1 つのキュー (FIFO) を使用できますが、それはアルゴリズムを実質的に変更しません)。

実装は次のとおりです。

bool palindrome(const char *s)
{
    std::stack<char> p; // be sure to #include <stack>

    for(int i = 0; s[i] != 0; i++)
        p.push(s[i]);

    for(int i = 0; s[i] != 0; i++)
    {
        if(p.top() != s[i])
            return false; // not a palindrome!

        p.pop();
    }    

    return true;
}

スペースのスキップは、読者の演習として残されています ;)

于 2013-01-22T19:37:34.650 に答える