c。中的字符串回文

时间:2015-06-23 17:53:28

标签: c string

我正在编写一个字符串pallindrome的程序,代码正在成功编译,但在运行它接受字符串但之后没有任何内容,输出窗口保持不变,光标闪烁,帮助我这个代码有什么问题。 我正在使用dev-c ++

Net::SSH.start(@host, user, {:password => pass, :non_interactive => true})

2 个答案:

答案 0 :(得分:1)

问题在于:

    while(ch!='\0')

ch是一个char数组,您将它与单个char进行比较。

此外,size未初始化。

我会建议这样的事情:

size=0;
while(ch[size]!='\0')
   {    p++;
      size++;
   }

或者,使用指针方法:

 while(*p!=0)
 {
      p++;
      size++;
 }

此外,不要在for loop内打印(这会使其多次打印),而是使用标志变量。

答案 1 :(得分:0)

您只需要一个循环,例如while (i < i) 看看这个将完成这项工作的例子:

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

/* in c99 use <stdbool.h> instead*/
typedef int bool;
#define true 1;
#define false 0;

int main(void)
{
    char ch[20];

    puts("enter the string: ");
    gets(ch);

    size_t size = strlen(ch);

    bool pallindrome = true;
    int j = size-1;
    int i = 0;
    while (i < j)
    {
        if(ch[i] != ch[j]) {
            pallindrome = false;
            break;
        }
        ++i;
        --j;
    }

    if (pallindrome)
        printf("\"%s\" is pallindrome\n", ch);
    else
        printf("\"%s\" is not pallindrome\n", ch);

    getch();
    return 0;
}
相关问题