Find the missing character in the String palette

Below is the code I wrote to find the missing character in the string palindrome, but I don’t know how to find which character should be printed while checking the string on both sides.

#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

+4
source share
2 answers

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

the 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 included mch comments and then chqrlie and chux
  • I added a lowercase to all input to prevent "m"! = "M" in "Malayama"
  • , [mcve] , .
  • C
    (, i ).
  • ,
+2

, , , , '\n', .

#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);

- "" , , . 2 .

  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");
  }
}

, , 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;
}
+2

Source: https://habr.com/ru/post/1684535/


All Articles