2016-12-11 17 views
-5

したがって、関数プロトタイプを使用して、ユーザーが入力した単語が回文かどうかを確認できるようにしています。しかし、最後に「Segment Fault:11」というエラーが表示されます。私は関数プロトタイプを使用することにかなり新しいので、関数定義の本体で何が起こっているのかを誰かが助けてくれたら、私にそれを指摘してください。エラー:Palindromeのセグメンテーションフォルト11

#include <stdio.h> 

void palindrome_check(int ch_length, char text) 

int main(void) 
{ 
    int length; 
    printf("Enter how many characters are in the message: "); 
    scanf("%d", &length); 

    char m; 
    printf("Enter the message: "); 
    scanf("%c", &text); 

    palindrome_check(l, m); 

    return 0; 
} 

void palindrome_check(int ch_length, char text) 
{ 
    char msg[ch_length]; 
    text = msg[ch_length]; 

    int count = 0; 

    while (count < ch_length) 
    { 
     count++; 
     scanf("%c", &msg[count]); 
    } 

    int i, j; 
    for (i = 0; i < ch_length; i++) 
    { 
     msg[j] = msg[ch_length - i]; 
    } 

    if (text[i] == text[j]) 
    { 
     printf("The message you entered is a palindrome!\n"); 
    } 
    else 
    { 
     printf("It's not a palindrome.\n"); 
    } 
} 
+0

メインのテキストは何ですか?文字列の代わりに*文字を*チェックしてもよろしいですか?そして、頂上近くの 'palindrome_check'宣言の後にセミコロンを追加してください。リストされたコードにはあなたを助けることができないほど多すぎます。これはおそらくコンパイルされず、最初に解決する必要のある大量の警告が表示されます。 – Evert

+0

ファイルの先頭に 'void palindrome_check(int ch_length、char text)'の後にセミコロンがありません。 –

+1

このコードはコンパイルされません。 –

答えて

0

いくつかの不要なことをしているようなコードを理解できませんでした。 msgは何ですか?あなたの問題を正しく理解していれば、これはうまくいくはずです:

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

void palindrome_check(int ch_length, char text []); 

int main(void) 
{ 
    char text [100]; 
    /* 
    int length; 
    printf("Enter how many characters are in the message: "); 
    scanf("%d", &length); 
    Not necessary if you use strlen()*/ 

    printf("Enter the message: "); 
    fgets(text,100,stdin); /*Using fgets() to allow spaces input*/ 
    /*Strip the newline*/ 
    text [strlen(text)-1]='\0'; 

    palindrome_check(strlen(text),text); 

    return 0; 
} 

void palindrome_check(int ch_length, char text []) 
{ 
    int i; 
    for (i = 0; i < ch_length; i++) 
    { 
     if(text [i] != text [ch_length-i-1]) 
     { 
     printf("It's not a palindrome.\n"); 
     return; 
     } 
    } 
    printf("It is a palindrome!\n"); 
} 
関連する問題