Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
#include <stdio.h>
#include <string.h>

int main()
{
    int n,flag=0,p;
    scanf("%d",&n);
    while(n--){
        int j=0;
        char s[10000];
        scanf("%s",&s);
        j=strlen(s);

        for(int i=1;i<=j/2;i++){

            if(s[i]==s[j-i+1])
            flag++;
            p=i;
            }
        if(flag==p)
            printf("palindrome");
            else
        printf("not");

    }
}
[edit]Code block added - OriginalGriff[/edit]

What I have tried:

i m trying to check palindrome string.
Posted
Updated 26-Aug-17 3:38am
v2
Comments
Richard MacCutchan 26-Aug-17 7:18am    
What is the question?

Well, it's pretty simple - and if you had used the debugger, you would have spotted it pretty quickly.

Start with a couple of hints:
1) What is the index of the first element of an array?
2) What is operator precedence going to do to this:
C++
j-i+1
Will it be evaluated as
C++
j-(i+1)
Or as
C++
(j-i)+1
Does it matter?
3) Does (1) have any effect on the value of p?
4) What is the value of flag the second time you go round the outer loop?

Instead of checking like that, add a variable called isPalindrome, and set it to 1 (this is C for "true") outside the inner loop.
Inside the loop, if you find a single non-match, set it to 0 (this is C for "false")

After the loop, check it, and it tells you if it's a palindrome...
 
Share this answer
 
Comments
jatinp510 27-Aug-17 1:21am    
thnk u soo much
OriginalGriff 27-Aug-17 1:40am    
You're welcome!
jatinp510 27-Aug-17 2:18am    
//in this code i have checked for whether this is palindrome and if its palindrome it will check whwther its odd or even
//suggest me a better code to improve my skills

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

int main()
{
int n,p,i;
scanf("%d",&n);
while(n--){
int flag=0,type=0;
int j=0;
char s[100000];
scanf("%s",&s);
j=strlen(s);
if(j==1){
printf("YES ODD\n");
}
else{

if(j%2==0){
type=1;
}

for(int i=0;i<j/2;i++){
if(s[i]==s[j-i-1])
flag++;
p=i;}
if(flag==(p+1)&&type==1)
printf("YES EVEN\n");
else if(flag==(p+1)&&type==0)
printf("YES ODD\n");
else
printf("NO\n");

}}
}
OriginalGriff 27-Aug-17 2:30am    
Perhaps you should have spent longer thinking about the task rather than rushing into code? That's ... um ... pretty poor, if I'm honest.
jatinp510 27-Aug-17 3:15am    
sir i m a begineer...
plzz.. give me some precious suggestions.. :)
First of all, use proper indentation, it help you reading the code, it helps strangers reading your code.
C++
#include <stdio.h>
#include <string.h>

int main()
{
    int n,flag=0,p;
    scanf("%d",&n);
    while(n--){
        int j=0;
        char s[10000];
        scanf("%s",&s);
        j=strlen(s);

        for(int i=1;i<=j/2;i++){

            if(s[i]==s[j-i+1])
                flag++;
            p=i;
        }
        if(flag==p)
            printf("palindrome");
        else
            printf("not");

    }
}

C arrays are 0 based, which means that the first char in s is s[0]. You start checking on second char.

There is a tool that allow you to see what your code is doing, its name is debugger. It is also a great learning tool because it show you reality and you can see which expectation match reality.
When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.
 
Share this answer
 
v2
Comments
jatinp510 27-Aug-17 1:21am    
thnk u.. sir
jatinp510 27-Aug-17 2:18am    
//in this code i have checked for whether this is palindrome and if its palindrome it will check whwther its odd or even
//suggest me a better code to improve my skills

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

int main()
{
int n,p,i;
scanf("%d",&n);
while(n--){
int flag=0,type=0;
int j=0;
char s[100000];
scanf("%s",&s);
j=strlen(s);
if(j==1){
printf("YES ODD\n");
}
else{

if(j%2==0){
type=1;
}

for(int i=0;i<j/2;i++){
if(s[i]==s[j-i-1])
flag++;
p=i;}
if(flag==(p+1)&&type==1)
printf("YES EVEN\n");
else if(flag==(p+1)&&type==0)
printf("YES ODD\n");
else
printf("NO\n");

}}
}
Patrice T 27-Aug-17 2:22am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900