-1

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 %d\n", len);
    while(i<len-1)
    {       
    printf("i=%d\n",i);
        for(j=len-1;j>=(len/2)-1 ;j--)
        {           
            printf("%c%c\n",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]=%c\n",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
serene
  • 19
  • 3
  • What is the input for the first example? – Spikatrix Apr 03 '19 at 16:12
  • Presumably you need the longest palindrome in the string? And does the middle of the palindrome need to be in the middle of the string? I'm also not convinced as to the reason why you feel the need to ask a new question. – Bathsheba Apr 03 '19 at 16:17
  • @Spikatrix sorry. It was abcbae – serene Apr 03 '19 at 16:20
  • @Bathsheba because the old code was heavily edited so if I tried to edit there it would look really messy. I'm also not convinced as to the reason why you feel the need to ask this when I already explained why. I cut of the string in the middle because the for a palindrome there's no point in comparing the chars after the middle because they will be the same. I do need the longest palindrome in string but first I need to find the palindrome first which is what I am checking right now. – serene Apr 03 '19 at 16:29
  • serene not sure to be (a little) aggressive with @Bathsheba is a good way to receive help ... – bruno Apr 03 '19 at 16:34
  • Why do you pass in values for `i` and `j` and then overwrite them? – Chris Turner Apr 03 '19 at 16:37
  • @bruno I am just really frustrated right now and I need help with my code. Not get judged about my questions. I spend a lot of time editing my questions and code before posting it. – serene Apr 03 '19 at 16:38
  • @ChrisTurner where? – serene Apr 03 '19 at 16:39
  • @serene ok but understand we spend a lot of time to help, and we do not need to help, while you need to receive help ;-) – bruno Apr 03 '19 at 16:39
  • @serene well they're part of the function call, but they get set again (admittedly to the same values) about 3 lines into the function – Chris Turner Apr 03 '19 at 16:41
  • @bruno just asking why doesn't really help – serene Apr 03 '19 at 16:42
  • @ChrisTurner I removed it but it didn't change the output. – serene Apr 03 '19 at 16:46
  • @serene I put some remarks and a proposal to find all the (sub)palindromes in the input string, see my answer. As you can see Bathsheba was right about the middle ... – bruno Apr 03 '19 at 17:36

2 Answers2

-1

Your code is doing exactly what you've asked of it.

You're scanning through the string and then scanning backwards from the other end to find any matching characters and not yet checking for palindromes. The reason why your code is giving you odd results when doing this is because of the inner loop.

for(j=len-1;j>=(len/2)-1 ;j--)

You stop j from going beyond the middle of the string, but allow i to traverse all of it. This means that you get points where i is more than j like in this example bit of output:

if: i=4 j=2 str[4]=c str[2]=c

Or sometimes the matching end character could be before the middle of the string.

You want to constrain j so that it is always more than i like so...

for(j=len-1;j>i ;j--)

...or maybe make it j>=i if you're happy to match the same character with itself.

Chris Turner
  • 8,082
  • 1
  • 14
  • 18
-1

You made a simple program into complex code. As far as i understand you need to check whether the input string is palindrome or not.Here is the simple sample code for that in c

#include <stdio.h>

int main()
{
   char s[1000], r[1000];
   int begin, end, count = 0;

   printf("Input a string\n");
   fgets(s,sizeof(s),stdin);

   // Calculating string length

   while (s[count] != '\0')
      count++;

   end = count - 1;

   for (begin = 0; begin < count; begin++) {
      r[begin] = s[end];
      end--;
   }

   r[begin] = '\0';

   printf("%s\n", r);
 if(strcmp(s,r)==0)
{
printf("panlindrome");
}
else
{
printf("not palindrome");
}
   return 0;
}
Vaibhav
  • 198
  • 1
  • 7
  • sorry but you do not answer to the question, you check if all the string is a palindrome, you do not search for them, and you do than in an expensive way making the reverse of the initial string ... – bruno Apr 03 '19 at 17:39
  • @bruno I did what I undestand about the problem statement. Other thing is check the output console he provided. If the input string is “racecar”, than that string is palindrome. Other there is function called “strrev() “ in c that will reverse the string. If you are looking for better answer please provide clarity on what you are looking for thanks – Vaibhav Apr 03 '19 at 19:13
  • the questions says _my assignment is to find the palindrome in the string_ and if you look at the examples there are also _dabae_ for which the expected result is _aba_ etc. (I am not the downvoter ;-) ) – bruno Apr 03 '19 at 19:17
  • That does make sense – Vaibhav Apr 03 '19 at 19:20
  • it is not always easy to understand the request, we all make mistakes from time to time, we only human, have fun – bruno Apr 03 '19 at 19:23
  • @serene did you find what you are looking for? – Vaibhav Apr 04 '19 at 05:34