2016-12-06 8 views
-2
#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 
#define FALSE 0 
#define TRUE 1 

................................チェック回文とデバッグ

if (strcmp(strrev(inputarray),temp) == 0) 

printf("Palindrome\n"); 

else 
printf("Not palindrome\n"); 

} 

しようこれはまだ入力が回文であるときに '回文ではない'となります。それは、私がプログラムを実行すると、 'stackar inputarray corrupted'と言います。どのように修正するかについてのアイデアは、回文を読み取り、入力配列が破損するのを止めます。

+1

あなたは 'strcpy(temp、inputarray)'で何を達成しようとしていますか?実行時には、 'temp'と' inputarray'の両方が初期化されていないことに注意してください。どちらも良い終了文字列ではない可能性があります。 –

+0

'strcpy(temp、inputarray)'が間違った位置にあると、inputarrayがいっぱいになってから来なければなりません。これで、初期化されていないデータがコピーされます。 –

+0

1. [デバッガ](http://stackoverflow.com/questions/2069367/how-to-debug-using-gdb)の使い方を学びます。 2.コードをインデントしてください。 3. 'stdbool.h'を使い、' true or false'にはMACROを使わないでください。最後に、http://stackoverflow.com/help/how-to-ask – Stargateur

答えて

0

これは1つの可能な実装です。私はできる限り多くのコメントで説明しようとしましたが、明確でないものがあればコメントを自由に残してください。

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

int alphabetic(char *string) 
{ 
    int i, valid; 
    valid = true; 
    for (i = 0; i < strlen(string); i++) 
    { 
     if (toupper(string[i]) < 'A' || toupper(string[i]) > 'Z') 
     { 
      valid = false; 
      // break here we are done; 
      break; 
     } 

    } 
    return valid; 
} 

void printArray(char* str) 
{ 
    printf("Array = "); 
    for (int i = 0; i < strlen(str); ++i) 
    { 
     printf("%c", str[i]); 
    } 
    printf("\n"); 
} 

bool isPalindrome(char* str1, char* str2) 
{ 
    bool isValidPalindrome = true; 
    int length = strlen(str1); 
    if (length != strlen(str2)) 
    { 
     printf("Strings must be the same lenth"); 
     isValidPalindrome = false; 
    } 
    else 
    { 
     --length; 
     for (int i = length; i >= 0; --i) 
     { 
      if (str1[i] != str2[length - i]) 
      { 
       isValidPalindrome = false; 
       break; 
      } 
     } 
    } 
    return isPalindrome; 
} 

int main() 
{ 
    const int length = 10; 
    char c, inputarray[length], temp[length]; 
    int i = 0; 
    // Comparing strings that have not been initialized 
    // produces undefined behavior. Imagine inputArray is equal to: 
    // inputArray: "my String ... some other unknown stuff"... where does 
    // the string ends? there is no '\n' in the horizon. 
    // The stack error you are getting is produced by the statement 
    // below. I've pusehd this statement below right after inputArray 
    // has been initialized 
    // strcpy(temp, inputarray); 

    // You don't need the format specifier %s unless you 
    // rewrite your printf statement as printf("%s", "Please enter string"); 
    // for simplicity you can write it as follows 
    printf("Please enter string: "); 

    while ((c = getchar()) != '\n') 
    { 
     if (i < length - 1) 
      inputarray[i] = c; 
     i++; 
    } 

    // Pulled the code inside the if to avoid multiple returns // just preference... not needed 
    if (i < length) 
    { 
     inputarray[i] = '\0'; 
     // helper function to print array 
     printArray(inputarray); 

     if (!alphabetic(inputarray)) 
     { 
      printf("Invalid input"); 
     } 
     // copy the strings here 
     strcpy(temp, inputarray); 
     // reverse the string here 
     strrev(inputarray); 
     // you will have to roll out your own isPalindrome 
     // implementation since reversing a string and comparing it 
     // with itself will always return false e.g. 
     // inputArray = "hello"; 
     // copy inputArray into temp 
     // temp = "hello"; 
     // reverse inputArray 
     // compare strings: "olleh" == "hello" -> false 
     if (isPalindrome(inputarray, temp) == true) 
      printf("Palindrome\n"); 
     else 
      printf("Not palindrome\n"); 
    } 
    else 
    { 
     inputarray[9] = '\0'; 
     printf("String too long\n"); 
    } 
    return 0; 
} 
関連する問題