1. 程式人生 > >判斷一個字串是不是迴文字串

判斷一個字串是不是迴文字串

編寫了一個程式,功能是判斷一個輸入的字串是否是迴文字串,程式碼如下
int is_plal(char str[100])
{
    char *str1 = str;
    char *str_s = str1;
    char *str_m = str1;
    while (*str_s)
    {
        str_s++;
    }
    str_s--;
    while (str_m < str_s)
    {
        if (*str_m != *str_s)
        {
            return 0;
        }
        str_m++;
        str_s--;
    }
    return
1; } int main() { char str[100] = { 0 }; printf("please enter a string :"); gets(str); if (is_plal(str)) { printf("%s is a plalindrome!\n",str); } else { printf("%s isn't a plalindrome!\n",str); } system("pause"); return 0; }

這是在vs2013平臺下的一個程式。如果輸入一個不是迴文字串的字串,則判斷函式返回0,螢幕上列印不是迴文,如果是迴文字串,則返回1,並在螢幕上列印該字串是迴文字串。程式執行結果如下:
這裡寫圖片描述


這裡寫圖片描述
總體來說,這個程式就是利用指標去進行操作的,關於指標的用途還有其他很多很實用的方法,需要去好好努力學習!