Try to search your question here, if you can't find : Ask Any Question Now ?

In C, my code to find the palindrome is either not printing out all the chars, missing a single char, or repeating chars?

HomeCategory: stackoverflowIn C, my code to find the palindrome is either not printing out all the chars, missing a single char, or repeating chars?
willson asked 3 weeks ago

I am working on homework and my assignment is to find the palindrome in the string. My code is not printing out all the chars or missing a char, or repeatedly printing chars. I tried to post as many details as possible on my code. I hope someone can help me through this code.
*I have posted this question before but this question has new updated code which is very different from what I had before.

#include <stdio.h>
#include <string.h> 
int palindromelength(char *str, int i, int j);
char str[100];  
int main()
{
    int i,j,len,n;
    printf("Enter a string ");      
    fgets(str,sizeof(str),stdin);//takes user input
    str[strcspn(str, "n")] = 0;
    len=strlen(str);
    palindromelength(str, 0, len-1);//function call
    return 0;       
}
int palindromelength(char *str, int i, int j) 
{       
    int len=strlen(str),n;
    printf("length is %dn", len);
    while(i<len-1)
    {       
    printf("i=%dn",i);
        for(j=len-1;j>=(len/2)-1 ;j--)
        {           
            printf("%c%cn",str[i],str[j]);           
            if(str[i]==str[j])//if letters aren't same
            {
               printf("if: i=%d j=%d str[%d]=%c str[%d]=%cn",i,j,i,str[i],j,str[j]);
            // printf("%c%c",str[i]);/***in my final code I will print this statement 
           //and all the other printfs will be removed. So there won't be new line.***/
            }                               
        }
        i++;    
    }
    return 0;   
}
***only str[i] will be printed in final code***
Output:        expected:abcba
Enter a string abcbae
length is 6
i=0
ae
aa
if: i=0 j=4 str[0]=a str[4]=a
ab
ac
i=1
be
ba
bb
if: i=1 j=3 str[1]=b str[3]=b
bc
i=2
ce
ca
cb
cc
if: i=2 j=2 str[2]=c str[2]=c
i=3
be
ba
bb
if: i=3 j=3 str[3]=b str[3]=b
bc
i=4
ae
aa
if: i=4 j=4 str[4]=a str[4]=a
ab
ac
output 2:                       expected:aba 
Enter a string dabae
length is 5
i=0
de
da
db
da
i=1
ae
aa
if: i=1 j=3 str[1]=a str[3]=a
ab
aa
if: i=1 j=1 str[1]=a str[1]=a
i=2
be
ba
bb
if: i=2 j=2 str[2]=b str[2]=b
ba
i=3
ae
aa
if: i=3 j=3 str[3]=a str[3]=a
ab
aa
if: i=3 j=1 str[3]=a str[1]=a
output 3:                    expected:racecar
Enter a string racecar
length is 7
i=0
rr
if: i=0 j=6 str[0]=r str[6]=r
ra
rc
re
rc
i=1
ar
aa
if: i=1 j=5 str[1]=a str[5]=a
ac
ae
ac
i=2
cr
ca
cc
if: i=2 j=4 str[2]=c str[4]=c
ce
cc
if: i=2 j=2 str[2]=c str[2]=c
i=3
er
ea
ec
ee
if: i=3 j=3 str[3]=e str[3]=e
ec
i=4
cr
ca
cc
if: i=4 j=4 str[4]=c str[4]=c
ce
cc
if: i=4 j=2 str[4]=c str[2]=c
i=5
ar
aa
if: i=5 j=5 str[5]=a str[5]=a
ac
ae
ac
1 Answers
Best Answer
Mikhail answered 3 weeks ago
Your Answer

6 + 16 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com