字符串回文检查

String palindrom check

我目前正在尝试编写一个函数,该函数将检查给定字符串中有多少个回文词,并将该数字 return 传给 main。 我的方法是检查字符串的长度并反向复制到另一个字符串,然后比较两者。 每次检查时我都会出现空白 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] != '[=10=]'; i++){}//getting the string length

    while (i >= 0) {//revrsing the string 
        rev[j++] = str[--i];
    }
    rev[j-1] = '[=10=]';
    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;

}

您的代码中有很多错误。让我一一为您解释:

  • while loop 中(用当前字符串检查反向字符串)您 return pali_count 经过一次迭代后始终为零。
  • 如果你的最后一个词是回文词,它永远不会给你回文,因为你的最后一个词中没有 space。
  • 最后你的整个算法都错了。检查以下示例:

    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.

现在这是解决方案:

  • space
  • 拆分句子
  • 取每个字就是那个回文
  • 如果是,增加计数

代码如下:

#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] != '[=10=]'; i++){} //getting the string length

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

    rev[j-1] = '[=10=]';

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

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

       k--;
       j--;
    }

    return 1;
}

样本I/O:

mm sabuj 女士 --> 2 pali

sabuj --> 0 巴里语

不要使用 gets,请改用 fgets

#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] = '[=10=]';
        } 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;
}
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");
            }
        }
    }
}