ashwanth s
ashwanth s

Reputation: 115

Find the missing character in a String palindrome

The following is the code I had written to find the missing character in a string palindrome but I don't know how to find which character must be printed while checking the string from both the sides simultaneously.

#include <stdio.h>
#include <stdlib.h>

int main() {
    char s[100], m[100];
    fgets(s, 100, stdin);
    int len = strlen(s);
    int a, b;

    for (int i = 0; i < strlen(s); i++) {
        m[i] = s[len - i - 1];
    }

    for (int i = 0, j = 0; i <= strlen(s), j <= strlen(s); i++, j++) {
        if (isspace(m[j])) {
            j++;
        }
        if (s[i] != m[j]) {
            if (len % 2 == 0) {
                printf("%c", m[j]);
                break;
            } else {
                printf("%c", s[i]);
                break;
            }
        }  
    }
}

Input: Malayaam
Output: l

Input: abcddcb
Output: a

Upvotes: 4

Views: 12992

Answers (4)

ashwin2125
ashwin2125

Reputation: 11

Hope this helps you..

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

int main(void)
{
char ar[102];
int ai=0;
int aj,ak;
scanf("%s",&ar);
ak = strlen(ar)-1;
ai = 0;
while(ai<ak){
    if(ar[ai]!=ar[ak]){
        if(ar[ai] == ar[ak-1] && ai!=ak-1){
            printf("%c",ar[ak]);
            break;
        }else{
            printf("%c",ar[ai]);
            break;
        }
    }
    ak--;
    ai++;
}
}

Upvotes: 0

user11114045
user11114045

Reputation:

int i;
char s[100];
scanf("%[^\n]s",s);
int l=strlen(s);
for(i=0,j=l-1;i<l,j>=0;i++,j--)
{
     if(s[i]!=s[j])
     {
          if(s[i+1]==s[j])
          {
              printf("%c",s[i]);
              break;
          }
          else
          {
              printf("%c",s[j]);
              break;
          }
     }
}

/*find the missing letter in palindrome:

This code checks the first i and last character j of string and if it is equal then i is incremented and j is decremented.if it is not equal it checks for the character next to i or j to be equal with the character, if i+1 is equal to j then i is missing and when j-1 is equal to i then j is the missing character in the string palindrome.*/

Upvotes: 0

Yunnosch
Yunnosch

Reputation: 26753

Assuming that there is always exactly one mistake in the palindrome, I recommend to look at the next letter on each side, for deciding which of two non-identical letters is the missing one.

Code:

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

int main(void)
{
    char s[100],m[100];
    fgets(s,100,stdin);
    int len=strlen(s)-1;
    int i,j;


    for(i=0;i<len;i++)
    {
        m[i]=tolower((unsigned char)s[len-i-1]);
        s[i]=tolower((unsigned char)s[i]);
    }

    for(i=0,j=0;i<len;i++,j++)
    {
        if(s[i]!=m[j])
        {
            printf("%c != %c, ", s[i], m[j]);
            if(s[i+1]==m[j])
            { printf("but %c == %c, %c should be inserted on the right.\n", s[i+1], m[j], s[i]);
            } else if(s[i]==m[j+1])
            { printf("but %c == %c, %c should be inserted on the left.\n", s[i], m[j+1], m[j]);
            } else
            { printf("giving up.\n");
            }
            break;
        }
    }

    return 0;
}

Note:

  • I incorporated the comments by mch and later chqrlie and chux
  • I added a lowercasing of all input, to prevent "m"!="M" in "Malayaam"
  • I think your [mcve] was not complete, so I edited a few things in.
  • I edited a little for backward compatibility to older C
    (e.g. no declaration of i inside the for loop head).
  • the assumption of exactly one mistake protects from some edge cases

Upvotes: 2

chux
chux

Reputation: 154174

Reduce problems, by recognizing that input likely has a trailing '\n' that need not participate in the test.

#include <ctype.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
  char s[100];
  fgets(s, sizeof s, stdin);
  s[strcspn(s, "\r\n")] = '\0';
  size_t len = strlen(s);

When the string is not a palindrome due to 1 "extra" character, to remove the left or right character may not be discernible until a fair amount of processing of the remaining string. Code needs to consider 2 paths.

  size_t mismatch;
  if (is_palindrome(s, len, &mismatch)) {
    puts("Palindrome");
  } else if (is_palindrome(s + mismatch + 1, len - mismatch*2 - 1, NULL)) { // skip left
    printf("left <%c>\n", s[mismatch]);
  } else if (is_palindrome(s + mismatch, len - mismatch*2 - 1, NULL)) { // skip right
    printf("right <%c>\n", s[len - mismatch-1]);
  } else {
    puts("Not palindrome nor off by 1 palindrome");
  }
}

All that is left is to make is_palindrome()

bool is_palindrome(const char *s, size_t len, size_t *mismatch) {
  printf("<%s> %zu\n", s, len);
  size_t i = 0;
  while (i + 1 < len) {
    if (tolower((unsigned char)s[i]) != //
        tolower((unsigned char )s[len - 1])) {
      if (mismatch) {
        *mismatch = i;
      }
      return false;
    }
    i++;
    len--;
  }
  return true;
}

Upvotes: 2

Related Questions