简体   繁体   English

在一个字符串中找到一个回文词然后重写它们,在 C

[英]Find a palindrome words in a string and then rewrite them, in C

Hi, how can i write a code in C that checks a string for palindromes, and then rewrites them?嗨,我怎样才能在 C 中编写代码来检查回文字符串,然后重写它们? For example: string> "awbiue abdba aebto leoel", should return "abdba leoel".例如:string>“awbiue abdba aebto leoel”,应该返回“abdba leoel”。

I wrote this code, but it can only find that the string is palindrome or not:我写了这段代码,但它只能找到字符串是否为回文:

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

int main()
{
char str[100];

printf("Enter string: ");
gets(str);
 int f=1;

{

for(int i=0;i<strlen(str); i++)
{
    if(str[i]!=str[strlen(str)-i-1])
    {
        f=0;  break;
    }
}
if(f==1)
    printf("Palindrom");
else
    printf("Not Palindrom");}
return 0;
}

You only need to read string by string, making sure whether they are palindrome, and if so, print them out -- that is what I did in the following code:您只需要逐个字符串读取,确保它们是否是回文,如果是,则将它们打印出来——这就是我在以下代码中所做的:

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

int main()
{
    char str[100];
    printf("Enter string: ");
    while(scanf("%s", str) == 1) { // read strings one by one in the str variable
        //your code part
        int f=1;

        for(int i=0;i<strlen(str); i++)
        {
            if(str[i]!=str[strlen(str)-i-1])
            {
                f=0;  break;
            }
        }
        if(f==1) // that means that string is palindrome
            printf("%s ", str); // print the string and a space

    }
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM