判断字符串是否回文

    xiaoxiao2021-04-18  59

    回文字符串,就是一个字符串,从左到右读和从右到左读是完全一样的,比如”aba”。

    循环实现:

    #include <iostream> using namespace std; bool IsPalindereme(char* array, size_t size) { for(int i=0; i<size; i++) { if(array[i] != array[size-1-i]) return false; } return true; } void FunTest() { char* array = "1234321"; int ret = IsPalindereme(array, strlen(array)); cout<<ret<<endl; } int main() { FunTest(); system("pause"); return 0; } #include <iostream> using namespace std; bool IsPalindereme(char* array, size_t size) { char* begin= array; char* end = array+size-1; while(begin < end) { if(*begin != *end) return false; begin++; end--; } return true; } void FunTest() { char* array = "1234321"; int ret = IsPalindereme(array, strlen(array)); cout<<ret<<endl; } int main() { FunTest(); system("pause"); return 0; }

    递归实现:

    #include <iostream> using namespace std; bool IsPalindereme(char* array, size_t size) { if(array == NULL) return false; if(size == 1 || size == 0) //size为1是元素为奇数个,size为0时是元素为偶数个 return true; if(*array != array[size-1]) return false; return IsPalindereme(array, size-2); } void FunTest() { char* array = "123421"; int ret = IsPalindereme(array, strlen(array)); cout<<ret<<endl; } int main() { FunTest(); system("pause"); return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-674662.html

    最新回复(0)