繁体   English   中英

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

[英]how to reverse order of words in string using C?

我试图颠倒字符串中单词的顺序,但我的 output 是一堆毫无意义的垃圾。 我不知道是什么问题,也许循环被打破了。

如果有人能解释我下面的代码有什么问题,我将不胜感激。 我还是 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];

此代码可以帮助您:

#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);
  }

}

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

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

int main()

{
   //declare variable
   char string[100], rev_string[100];
   
   //declare number of loop 
   int i, j =0 , 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--){
      rev_string[j] = string[i];
      j++;
   }
    //check the rev_string if you want
    /*for (i = 0; i < len; i++){
       printf("%c\n",rev_string[i]);
    }*/
   
   if(strcmp(rev_string,string) == 0){
           printf("Is Palindrome\n");
           return(0);
       }else{
           printf("Is not Palindrome\n");
           return(1);
       }
       
       
}

暂无
暂无

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

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