String palindrom check

429 views Asked by At

I'm currently trying to write a function that will check how many palindroms words are in a given string and return that number to main. My approach was to check the length of a string and copying into another in reverse and then comparing both. While checking each time I reach a blank space

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

int palindrom(char str1[81]);

int main(){

    char str[81];
    gets(str);
    printf("%d pali\n", palindrom(str));


}


int palindrom(char str[81]) {

    int i, j=0, k = 0, pali_count = 0, length = 0, flag=0;
    char rev[81], c;
    for (i = 0; str[i] != '\0'; i++){}//getting the string length

    while (i >= 0) {//revrsing the string 
        rev[j++] = str[--i];
    }
    rev[j-1] = '\0';
    printf("%s", rev);


    length = j - 2;
    k = length;
    j = 0;
    while (k >= 0) {
        k--;
        j++;
        c = rev[k];
        if (rev[k] != str[j])
            flag++;
        if (c == ' ') {
            if (flag == 0)
                pali_count++;
            flag = 0;
            continue;
        }

        return pali_count;

    }





    return 0;

}
3

There are 3 answers

4
Mahedi Sabuj On BEST ANSWER

There are plenty of mistakes in your code. Let me explain you one by one:

  • In while loop (where you check reverse string with current string) you return pali_count after one iteration which always be zero.
  • If your last word is a palindrome word it never give you as palindrome because there is no space in your last word.
  • And at last your whole algorithm is wrong. Check the below example:

    Suppose, str = 'sabuj'

    after reverse the word it will be: rev = 'jubas'

    now if you check last char with first char then all char will be same hence it give you wrong result. you need to check first char with first char not with first with last as you already reverse the string.

    Here you will also face another problem in case of sentence like mahedi sabuj will be checked into jubas ideham which is also wrong because here we match first word with second word and vice versa.

Now Here is the solution:

  • Split the sentence with space
  • take each word is that palindrome
  • if yes increase the count

Here is the code:

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

int palindrom(char str1[81]);

int main(){

    char str[81];
    gets(str);

    int result = 0;

    char *p = strtok(str, " "); // split word by space

    while(p != NULL)
    {
        result += palindrom(p);
        p = strtok(NULL, " ");
    }


    printf("%d pali\n", result);
}


int palindrom(char str[81])
{

    int i, j=0, k = 0, pali_count = 0, length = 0, flag=0;
    char rev[81], c;

    for (i = 0; str[i] != '\0'; i++){} //getting the string length

    while (i >= 0)  //revrsing the string
    {
        rev[j++] = str[--i];
    }

    rev[j-1] = '\0';

    length = j - 2;
    k = length;
    j = length;

    while (k >= 0)
    {
       if (str[j] != rev[k])
         return 0;

       k--;
       j--;
    }

    return 1;
}

Sample I/O:

madam mm sabuj --> 2 pali

sabuj --> 0 pali

0
RoadRunner On

Don't use gets, use fgets instead.

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

#define STRSIZE 81

int is_palindrome(char *word);

int
main(void) {
    char str[STRSIZE];
    size_t slen;
    char *word;
    const char *delim = " ";
    int ispal = 0;

    printf("Enter some text: ");
    if (fgets(str, STRSIZE, stdin) == NULL) {
        printf("Cannot read text into buffer.\n");
        exit(EXIT_FAILURE);
    }

    slen = strlen(str);
    if (slen > 0) {
        if (str[slen-1] == '\n') {
            str[slen-1] = '\0';
        } else {
            printf("Too many characters entered.\n");
            exit(EXIT_FAILURE);
        }
    }

    if (!*str) {
        printf("No text entered.\n");
        exit(EXIT_FAILURE);
    }

    word = strtok(str, delim);
    while (word != NULL) {
        if (is_palindrome(word)) {
            ispal++;
        }
        word = strtok(NULL, delim);
    }

    printf("%d palindromic words found.\n", ispal);
    return 0;
}

int
is_palindrome(char *word) {
    int start, end;

    if (!word) {
        return 0;
    }

    if (strlen(word) == 1) {
        return 1;
    }

    start = 0;
    end = strlen(word) - 1;
    while (start < end) {
        if (word[start++] != word[end--]) {
            return 0;
        }
    }

    return 1;
}
0
Florin On
import java.util.Scanner;

public class Javatips {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String x = in.next();
        int n = x.length();
        boolean isPalindrom = true;
        for (int i = 0; i < n / 2 - 1; i++) {
            if (x.charAt(i) == x.charAt(n - 1 - i)) {
                isPalindrom = true;
            } else {
                isPalindrom = false;
            }
            if (isPalindrom) {
                System.out.println("the String " + x + " is palindrom");
            } else {
                System.out.println("the String " + x + " is not palindrom");
            }
        }
    }
}