如何使用C反转字符串中的单词顺序?

如何使用C反转字符串中的单词顺序?,c,string,C,String,我试图颠倒字符串中单词的顺序,但我的输出是一堆毫无意义的垃圾。我不知道是什么问题,可能是线圈断了 如果有人能解释我下面的代码有什么问题,我将不胜感激。我还不熟悉C编程,这种问题有点令人沮丧 #include<stdio.h> int main() { //declare variable char string[100], rev_string[100]; //declare number of loop int i, j, len; p

我试图颠倒字符串中单词的顺序,但我的输出是一堆毫无意义的垃圾。我不知道是什么问题,可能是线圈断了

如果有人能解释我下面的代码有什么问题,我将不胜感激。我还不熟悉C编程,这种问题有点令人沮丧

#include<stdio.h>

int main()

{
   //declare variable
   char string[100], rev_string[100];
   
   //declare number of loop 
   int i, j, len;

   printf("enter the string: ");
   scanf("%s",string);
   
   //finding the length
   len = strlen(string);
   printf("strings length: %d\n", len);

   for (i = len - 1; i >= 0; i--)
       for (j = 0; j < len - 1; j++)
           rev_string[j] = string[i];

   rev_string[j] = '\0';

   if (strcmp(string, rev_string) == 0)
       printf("rev_string: %s is a palindrome", rev_string);

   else
       printf("rev_string : %s is not a palindrome words",rev_string);

return(0);

}


您的标题有点混乱,因为您的代码似乎是回文检查,它应该反转字符串,而不是单词的顺序

要反转字符串,只需执行以下操作:

for (i = 0; i < len; i++)
    rev_string[i] = string[len - i - 1];

可以先使用循环反转字符串,然后使用strcomp


此代码可以帮助您:

#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define NCHARS 256      /* constant to count array size, covers ASCII + extended ASCII */

int ispalindrom (const char *s1, const char *s2)
{
    int count[NCHARS] = {0};            /* counting array, covers all extended ASCII     */

for (; *s1; s1++)                   /* loop over chars in string 1 */
    if (!isspace(*s1))              /* if not whitespace */
        count[(int)*s1]++;          /* add 1 to index corresponding to char */

for (; *s2; s2++)                   /* loop over chars in string 2 */
    if (!isspace(*s2))              /* if not whitespace */
        count[(int)*s2]--;          /* subtract 1 from index corresponding to char */

for (int i = 0; i < NCHARS; i++)    /* loop over counting array */
    if (count[i])                   /* if any index non-zero, not anagram */
        return 0;

return 1;       /* all chars used same number of times -> anagram */

}


void main()
{
int i, j = 0, k = 0, x, len;
char str[100], str1[10][20], temp;
char str2[100];
printf("enter the string :");
scanf("%[^\n]s", str);

for (int i = 0;str[i] != '\0'; i++)
{
      str2[i]=str[i];
}

/* reads into 2d character array */
for (int i = 0;str[i] != '\0'; i++)
{
    if (str[i] == ' ')
    {
        str1[k][j]='\0';
        k++;
        j=0;
    }
    else
    {
        str1[k][j]=str[i];
        j++;
    }
}
str1[k][j] = '\0';

/* reverses each word of a given string */

for (int i = 0;i <= k;i++)
{
    len = strlen(str1[i]);
    for (int j = 0, x = len - 1;j < x;j++,x--)
    {
        temp = str1[i][j];
        str1[i][j] = str1[i][x];
        str1[i][x] = temp;
    }
}

for (int i = 0;i <= k;i++)
{
    printf("%s ", str1[i]);
}

printf("\n\n");

if (ispalindrom(str1, str2)==0)
{
    printf("The word is Palindrom !\n");
}
else
{
    printf("The word is not Palindrom !\n");
}

}

1把绳子倒过来。2将单独的单词3颠倒过来!这回答了你的问题吗?您确定只想实现一种回文类型,即反转单词而不是字符串中的字母?嵌套for循环看起来更接近于实现单个单词的回文。有多种类型的。感谢代码。但是你知道为什么我的“for”循环不起作用吗?rev_字符串返回一个垃圾输出您的代码可以工作,但对我来说有点进步。我需要时间来消化它。哈哈。不过无论如何,谢谢你的回答。非常感谢。例如:ELLE,BOOB,你的代码不起作用correctly@MEDLDN你确定?我现在测试过了,效果很好,你想同时插入两个单词吗?不,我只写了一个单词,但他打印的单词不是回文,通常是回文
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#define NCHARS 256      /* constant to count array size, covers ASCII + extended ASCII */

int ispalindrom (const char *s1, const char *s2)
{
    int count[NCHARS] = {0};            /* counting array, covers all extended ASCII     */

for (; *s1; s1++)                   /* loop over chars in string 1 */
    if (!isspace(*s1))              /* if not whitespace */
        count[(int)*s1]++;          /* add 1 to index corresponding to char */

for (; *s2; s2++)                   /* loop over chars in string 2 */
    if (!isspace(*s2))              /* if not whitespace */
        count[(int)*s2]--;          /* subtract 1 from index corresponding to char */

for (int i = 0; i < NCHARS; i++)    /* loop over counting array */
    if (count[i])                   /* if any index non-zero, not anagram */
        return 0;

return 1;       /* all chars used same number of times -> anagram */

}


void main()
{
int i, j = 0, k = 0, x, len;
char str[100], str1[10][20], temp;
char str2[100];
printf("enter the string :");
scanf("%[^\n]s", str);

for (int i = 0;str[i] != '\0'; i++)
{
      str2[i]=str[i];
}

/* reads into 2d character array */
for (int i = 0;str[i] != '\0'; i++)
{
    if (str[i] == ' ')
    {
        str1[k][j]='\0';
        k++;
        j=0;
    }
    else
    {
        str1[k][j]=str[i];
        j++;
    }
}
str1[k][j] = '\0';

/* reverses each word of a given string */

for (int i = 0;i <= k;i++)
{
    len = strlen(str1[i]);
    for (int j = 0, x = len - 1;j < x;j++,x--)
    {
        temp = str1[i][j];
        str1[i][j] = str1[i][x];
        str1[i][x] = temp;
    }
}

for (int i = 0;i <= k;i++)
{
    printf("%s ", str1[i]);
}

printf("\n\n");

if (ispalindrom(str1, str2)==0)
{
    printf("The word is Palindrom !\n");
}
else
{
    printf("The word is not Palindrom !\n");
}

}
#include <stdbool.h>
#include <stdio.h>

bool ispalindrom(char *str,int k)
{
   for(int i=0;i<k;i++)
   {
      if(str[i]!=str[k-i-1])
      {
            return false;
      }
   }
   return true;
}

int main()
{
  char string[100];
  printf("enter the string: ");
  scanf("%s",string);
  if (ispalindrom(string,strlen(string)))
  {
     printf("\nrev_string: %s is a palindrome\n", string);
  }
  else
  {
      printf("\nrev_string : %s is not a palindrome words\n",string);
  }

}