简体   繁体   English

C 中的回文程序

[英]Palindrome program in C

My program in C which is Palindrome has an error in its function.我在 C 中的程序是回文,它的 function 有错误。 My function is not comparing the 2 characters in my string.我的 function 没有比较字符串中的 2 个字符。 When I type a single character it answers palindrome but if it is two or more always not palindrome.当我输入单个字符时,它会回答回文,但如果是两个或更多,则始终不是回文。

Code:代码:

          int IntStrlength=strlen(StrWord);
          int IntCtr2=0;
          int IntCtr=1, IntAnswer;
          while(IntCtr<=(IntStrlength/2)){
                printf(" %d %d\n", IntCtr2,IntStrlength);
               if(StrWord[IntStrlength] != StrWord[IntCtr2]){
                  IntAnswer=0;
                  printf(" %d=Not Palindrome", IntAnswer);
                  exit (0);
                 }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
               else{
                  IntCtr2++;
                  IntStrlength--;
                  }// else <--------
               IntCtr++;
          }//while(IntCtr<IntStrlength/2) <-----------
          IntAnswer=1;
          printf(" %d=Palindrome", IntAnswer);
          return ;

} }

Single character:单字:

Two or more characters:两个或更多字符:

My program in C which is Palindrome has an error in its function.我的C语言程序是回文式,它的功能有错误。 My function is not comparing the 2 characters in my string.我的功能不是比较字符串中的2个字符。 When I type a single character it answers palindrome but if it is two or more always not palindrome.当我键入一个字符时,它会回答回文,但如果是两个或更多,则始终不是回文。

Code:代码:

          int IntStrlength=strlen(StrWord);
          int IntCtr2=0;
          int IntCtr=1, IntAnswer;
          while(IntCtr<=(IntStrlength/2)){
                printf(" %d %d\n", IntCtr2,IntStrlength);
               if(StrWord[IntStrlength] != StrWord[IntCtr2]){
                  IntAnswer=0;
                  printf(" %d=Not Palindrome", IntAnswer);
                  exit (0);
                 }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
               else{
                  IntCtr2++;
                  IntStrlength--;
                  }// else <--------
               IntCtr++;
          }//while(IntCtr<IntStrlength/2) <-----------
          IntAnswer=1;
          printf(" %d=Palindrome", IntAnswer);
          return ;

} }

Single character:单个字符:

Two or more characters:两个或两个以上字符:

My program in C which is Palindrome has an error in its function.我的C语言程序是回文式,它的功能有错误。 My function is not comparing the 2 characters in my string.我的功能不是比较字符串中的2个字符。 When I type a single character it answers palindrome but if it is two or more always not palindrome.当我键入一个字符时,它会回答回文,但如果是两个或更多,则始终不是回文。

Code:代码:

          int IntStrlength=strlen(StrWord);
          int IntCtr2=0;
          int IntCtr=1, IntAnswer;
          while(IntCtr<=(IntStrlength/2)){
                printf(" %d %d\n", IntCtr2,IntStrlength);
               if(StrWord[IntStrlength] != StrWord[IntCtr2]){
                  IntAnswer=0;
                  printf(" %d=Not Palindrome", IntAnswer);
                  exit (0);
                 }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
               else{
                  IntCtr2++;
                  IntStrlength--;
                  }// else <--------
               IntCtr++;
          }//while(IntCtr<IntStrlength/2) <-----------
          IntAnswer=1;
          printf(" %d=Palindrome", IntAnswer);
          return ;

} }

Single character:单个字符:

Two or more characters:两个或两个以上字符:

My program in C which is Palindrome has an error in its function.我的C语言程序是回文式,它的功能有错误。 My function is not comparing the 2 characters in my string.我的功能不是比较字符串中的2个字符。 When I type a single character it answers palindrome but if it is two or more always not palindrome.当我键入一个字符时,它会回答回文,但如果是两个或更多,则始终不是回文。

Code:代码:

          int IntStrlength=strlen(StrWord);
          int IntCtr2=0;
          int IntCtr=1, IntAnswer;
          while(IntCtr<=(IntStrlength/2)){
                printf(" %d %d\n", IntCtr2,IntStrlength);
               if(StrWord[IntStrlength] != StrWord[IntCtr2]){
                  IntAnswer=0;
                  printf(" %d=Not Palindrome", IntAnswer);
                  exit (0);
                 }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
               else{
                  IntCtr2++;
                  IntStrlength--;
                  }// else <--------
               IntCtr++;
          }//while(IntCtr<IntStrlength/2) <-----------
          IntAnswer=1;
          printf(" %d=Palindrome", IntAnswer);
          return ;

} }

Single character:单个字符:

Two or more characters:两个或两个以上字符:

My program in C which is Palindrome has an error in its function.我的C语言程序是回文式,它的功能有错误。 My function is not comparing the 2 characters in my string.我的功能不是比较字符串中的2个字符。 When I type a single character it answers palindrome but if it is two or more always not palindrome.当我键入一个字符时,它会回答回文,但如果是两个或更多,则始终不是回文。

Code:代码:

          int IntStrlength=strlen(StrWord);
          int IntCtr2=0;
          int IntCtr=1, IntAnswer;
          while(IntCtr<=(IntStrlength/2)){
                printf(" %d %d\n", IntCtr2,IntStrlength);
               if(StrWord[IntStrlength] != StrWord[IntCtr2]){
                  IntAnswer=0;
                  printf(" %d=Not Palindrome", IntAnswer);
                  exit (0);
                 }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
               else{
                  IntCtr2++;
                  IntStrlength--;
                  }// else <--------
               IntCtr++;
          }//while(IntCtr<IntStrlength/2) <-----------
          IntAnswer=1;
          printf(" %d=Palindrome", IntAnswer);
          return ;

} }

Single character:单个字符:

Two or more characters:两个或两个以上字符:

My program in C which is Palindrome has an error in its function.我的C语言程序是回文式,它的功能有错误。 My function is not comparing the 2 characters in my string.我的功能不是比较字符串中的2个字符。 When I type a single character it answers palindrome but if it is two or more always not palindrome.当我键入一个字符时,它会回答回文,但如果是两个或更多,则始终不是回文。

Code:代码:

          int IntStrlength=strlen(StrWord);
          int IntCtr2=0;
          int IntCtr=1, IntAnswer;
          while(IntCtr<=(IntStrlength/2)){
                printf(" %d %d\n", IntCtr2,IntStrlength);
               if(StrWord[IntStrlength] != StrWord[IntCtr2]){
                  IntAnswer=0;
                  printf(" %d=Not Palindrome", IntAnswer);
                  exit (0);
                 }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
               else{
                  IntCtr2++;
                  IntStrlength--;
                  }// else <--------
               IntCtr++;
          }//while(IntCtr<IntStrlength/2) <-----------
          IntAnswer=1;
          printf(" %d=Palindrome", IntAnswer);
          return ;

} }

Single character:单个字符:

Two or more characters:两个或两个以上字符:

My program in C which is Palindrome has an error in its function.我的C语言程序是回文式,它的功能有错误。 My function is not comparing the 2 characters in my string.我的功能不是比较字符串中的2个字符。 When I type a single character it answers palindrome but if it is two or more always not palindrome.当我键入一个字符时,它会回答回文,但如果是两个或更多,则始终不是回文。

Code:代码:

          int IntStrlength=strlen(StrWord);
          int IntCtr2=0;
          int IntCtr=1, IntAnswer;
          while(IntCtr<=(IntStrlength/2)){
                printf(" %d %d\n", IntCtr2,IntStrlength);
               if(StrWord[IntStrlength] != StrWord[IntCtr2]){
                  IntAnswer=0;
                  printf(" %d=Not Palindrome", IntAnswer);
                  exit (0);
                 }//if(StrWord[IntCtr2]!=StrWord[IntStrlength]) <---------
               else{
                  IntCtr2++;
                  IntStrlength--;
                  }// else <--------
               IntCtr++;
          }//while(IntCtr<IntStrlength/2) <-----------
          IntAnswer=1;
          printf(" %d=Palindrome", IntAnswer);
          return ;

} }

Single character:单个字符:

Two or more characters:两个或两个以上字符:

This may works for you这可能对你有用

 #include <stdio.h>
    #include <stdlib.h>
    
    int main(void) {
        setbuf(stdout,NULL);
        int i,limit;
        char string1[10];
        int flag=0;
    
    
        printf("enter a string");
        scanf("%s",string1);
        limit=strlen(string1);
        for(i=0;i<limit;i++){
            if(string1[i]!=string1[limit-i-1]){
                flag=1;
                break;
            }
    
        }   if(flag==1){
            printf("entered string is not  palindrome");
        }else{
            printf("entered string is  palindrome");
        }
    
    
    
    
        return EXIT_SUCCESS;
    }
I have solution for this    
char a[]="abbba";
int i,j,b=strlen(a),flag=0;
          for(i=0,j=0; i<b; i++,j++)
          {
              if(a[i]!=a[b-j-1])
              {
               flag=1;
               break;
              } 
          }
        if(flag)
        {

                      printf("the string is not palindrum");
        }
        else
              {
                      printf("the string is  palindrum");
        }

You can visit my github link for the code. 您可以访问我的github链接获取代码。 You can download the code from there. 您可以从此处下载代码。 I guess it will help you to understand the code. 我想它将帮助您理解代码。 Code contains many comments you can remove and experiment with it. 代码包含许多注释,您可以删除它们并进行试验。 Palindrome number If something is missing please let me know. 回文数如果有什么遗漏,请告诉我。 If you like the code and explanation upvote my comment. 如果您喜欢该代码和解释,请反对我的评论。 To understand the code and run it in your system visit the link (Palindrome Code), download the code and play with it. 要了解代码并在系统中运行,请访问链接(回文代码),下载代码并进行操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM