2012-02-29 75 views
1

正在处理一个小程序,该程序将从用户处取得一个字符串并查看它是否是回文。 (一个词组以相同的方式向后拼写,如“从不奇怪或甚至”)我已经构建了从字符串中删除空格和任何非字母字符的函数,然后也创建了字符串的副本。 (所有这三个函数都经过了充分的测试,没有问题。)现在我正在研究一个函数,它需要比较两个字符串以查看字符串是否以相同的方式向前和向后拼写。布尔函数将无法正确地比较数组

函数应该通过向前循环主字符串并向后复制并比较每个元素来决定它是否是回文。然而我一直得到一个错误的答案,我做错了什么?

_Bool isPalindrome(char str[], char copy[]) 
{ 
    int i = 0; 
    int count = 0, j = 0; 

    // loop through the main string to find the number of elements 
    while(str[j] != '\0') 
    { 
    count++; 
    j++; 
    } 

    //loop through the main string until the null character. 
    while(str[i] != '\0') 
    { 
    // to loop through the copy backwards, 
    // use the size of the first and subtract i 
    size = count - i; 
    if(str[i] != copy[size]) 
     return FALSE; 
    i++; 
    } 

    return TRUE; 
} 
+0

试运行调试器?你的第一个循环也相当于'strlen'。 – cnicutar 2012-02-29 16:55:49

+0

用笔和纸以及两个长度为一个字符的输入字符串运行您的代码。走着瞧吧。 – Mat 2012-02-29 16:58:03

回答

0

它应该是:

size = count - i - 1; 
+0

嘿它工作,谢谢! – Ryan 2012-02-29 17:22:00