简体   繁体   English

将字符串重新排列为回文C

[英]re-arrange string to a palindrome C

void MakeItPOLI(char Input[99])
{
    char poli[99]="", ch[2],chV2[2]; // chV2- will be the middle letter(if there is odd number of letters)
    int arr[26]={0},i,j,flag=0; // flag=1 if there is need for middle letter. 
    ch[1]='\0'; chV2[1]='\0';
    for(i=0;i<99;i++) arr[Input[i] - 'a']++;
    for(i=0; i<26;i++)
    {
        if(arr[i]%2!=0){arr[i]-=1;chV2[0]=i+'a'; flag=1;}
        for(j=0;j<arr[i]/2;j++){ch[0]=i+'a'; strcat(poli,ch);}
    }
    strrevV2(poli,chV2, flag); 
}
void strrevV2(char poli[99], char chV2[2], int flag){
    int e=strlen(poli),s=0; char revpoli[99]="",temppoli[99]="";
    strcpy(temppoli,poli);
    while (e!=0) revpoli[s++]=temppoli[--e];
    if(flag==1) strcat(poli,chV2); strcat(poli,revpoli);
    printf("This is not a palindrom but it could be\n---> %s\n",poli);

}

EDIT- 编辑-

thanks for the help. 谢谢您的帮助。 it was an assignment from university C class (Im an ee student) the code should take a string and re arrange it to a palindrome. 这是大学C类(我是电子学生)的作业,代码应采用字符串并将其重新排列成回文。 with out adding letters, as some comments here did, [test could never be a palindrome , but zccbbaa should be abczcba] but they helped me learn, so thanks! 不加字母,就像这里的一些评论一样,[测试永远不可能是回文,但zccbbaa应该是abczcba],但是它们帮助我学习了,所以谢谢!

i did not had time to fully explain the q. 我没有时间充分解释问题。 the other day. 另一天。 this is a very small part of a big and annoying assignment. 这是一项艰巨而艰巨的任务的一小部分。 [500 lines of codes, some did closer to 1000, least iv seen is around 500]. [500行代码,有些接近1000行,至少iv见到的大约是500行]。 above is my code, with the same idea i had, but working! 上面是我的代码,具有与我相同的想法,但是可以工作! (there is another codes that make sure "input[99]") can be a palindrome- if it has max of one letter thats appeared odd number of times (还有其他代码可确保“ input [99]”)可以是回文式-如果它最多包含一个出现奇数次的字母

Letters[Input[i]] is plain wrong, and it invokes undefined behavior accessing array out of bounds. Letters[Input[i]]是绝对错误的,它会调用未定义的行为来访问数组。

As you can see on a ASCII table values of letters are not from 0 to 25 , as you supposed. 如您所见,在ASCII表中 ,字母的值不像您想象的那样从025

You can do, for example: 您可以这样做,例如:

int index;
for(i=0;i<size;i++)
{
    index = toupper(Input[i]) - 'A';
    if ((index >= 0) && (index < 26))
    {
       Letters[index]++;
    }
}

EDIT If you can assume all input letters and are lower case just 编辑如果可以假设所有输入字母都是小写

Letters[Input[i] - 'a']++;

EDIT2 编辑2

Moreover your loop 而且你的循环

for (i = Letters[j]; Letters[j] != 0; i = i - 2)

does not take care of i>0: fixed 不照顾i> 0:固定

for (i = Letters[j]; (Letters[j] != 0) && (i>=0); i = i - 2)

EDIT3 编辑3

trying to correct your code without using "too complicated" matters, your code can simply be 尝试在不使用“过于复杂”的问题的情况下更正您的代码,您的代码可能只是

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

void MakeItPOLI(char Input[99])
{
    int i, size, j;
    size = strlen(Input);

    char Poli[99] = {0};

    for (i=0; i<size; i++)
    {
        Poli[i] = Input[i];
    }

    if ((size%2) == 0)
    {
        j=size-1;
    }
    else
    {
        j=size-2;
    }

    do
    {
        Poli[i++] = Input[j];
    }
    while (j-- > 0);

    Poli[i] = '\0';

    printf("This is not a palindrom but it could be: %s\n", Poli);
}

int main()
{
    char test[99] = "test";
    char test2[99] = "testc";
    MakeItPOLI(test);
    MakeItPOLI(test2);
}

OUTPUT 输出值

This is not a palindrom but it could be: testtset
This is not a palindrom but it could be: testctset
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

char *makeItPal(char *str)
{
  if (!str || strlen(str) < 2)
    return NULL;

  char *dest = malloc(sizeof(char) * ((strlen(str) * 2) + 1));
  if (!dest)
    exit(0);

  int i = 0, j = 0;
  for (i = 0; str[i]; i++)
    dest[j++] = str[i];

  i -= 2;

  while (i >= 0)
    {
      dest[j] = str[i];
      i--;
      j++;
    }

  dest[j] = 0;
  return dest;
}

int main()
{
  char str[] = "stack";
  char str2[] = "foo";
  char str3[] = "str3";

  char *dest = makeItPal(str);
  char *dest2 = makeItPal(str2);
  char *dest3 = makeItPal(str3);

  printf("%s\n", dest);
  printf("%s\n", dest2);
  printf("%s\n", dest3);
}

here is i think a better solution 这是我认为更好的解决方案

Output : rifzy@rifzy-HP-EliteBook-840-G1:~$ gcc test.c && ./a.out
         stackcats
         fooof
         str3rts

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

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