简体   繁体   中英

how to reverse order of words in string using C?

I am trying to reverse the order of words in a string, but my output is a bunch of junk that makes no sense. I don't know what is the problem, maybe the loops are broken.

Appreciate it if someone can explain what is wrong with my code below. I still new to C programming and this kind of problem is kind of frustrating.

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

}

Your title is a bit confusing because your code seems to be a palindrome check and it should reverse the string, not the order of the words.

To reverse the string you can simply do:

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

This code can help you:

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

}

you can use a loop first to reverse the string first and then use 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);
       }
       
       
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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