Hello friends,

Please tell me how to reverse string in c like taht.

My name is jacob

jacob is name my

Recommended Answers

All 9 Replies

First question. Do you want to display it reversed or do you what to reverse the order in the c string?

The way this forum works is that YOU are responsible for posting up your try to solve the assignment/exercise.

So post your code (and do paste it between the code tags you get by clicking on icon in the editor), and tell us what has you stumped.

Regarding reversing a word, first, you need to ID the word - where does it start, and where does it stop, if there are other words next to it.

Then, practice it by hand yourself, and try to recognize the basic steps you use to do this task. Those simple step by step instructions, will be the backbone of your programs logic, if they're small enough steps.[code ] icon in the editor), and tell us what has you stumped.

Regarding reversing a word, first, you need to ID the word - where does it start, and where does it stop, if there are other words next to it.

Then, practice it by hand yourself, and try to recognize the basic steps you use to do this task. Those simple step by step instructions, will be the backbone of your programs logic, if they're small enough steps.

Hello

I want to reverse the order of my complete string.

I am Learning c
c learning am I

Like that.

Hello

I want to reverse the order of my complete string.

I am Learning c
c learning am I

Like that.

O.K. could you post what you've attempted.

So post your code (and do paste it between the code tags you get by clicking on the icon in the editor), and tell us what has you stumped. [/B][code ] icon in the editor), and tell us what has you stumped.

here is the code for reversibg string

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



char * reverString( char *str)
{
 int strsize, endword, wordReadPos writPos = 0;

  strsize = strlen(str) -1;
  
 char * buffer = (char*) malloc( strsize + 2);

 while(strsize > 0)
 {
  if(str[strsize] == ' ')
    {
     buffer[strsize++] = str[strsize--];
     }
  
  else
    {
      endword = strsize; // store the end of word
     }
   
   // now scan for the beging of a word

  while(strsize >= 0 && str[strsize] != ' ') strsize--;
 
    wordReadPos = strsize + 1;
   
   while(wordReadPos != endword)
      {
       buffer[writPos++] = str[wordReadPos++];
      }  // copy word
   }

  buffer[writePos]  = '\0';

  strcpy(str, buffer);
  
  free(buffer);

 return 0;
}

here is the code for reversibg string

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



char * reverString( char *str)
{
 int strsize, endword, wordReadPos writPos = 0;

  strsize = strlen(str) -1;
  
 char * buffer = (char*) malloc( strsize + 2);

 while(strsize > 0)
 {
  if(str[strsize] == ' ')
    {
     buffer[strsize++] = str[strsize--];
     }
  
  else
    {
      endword = strsize; // store the end of word
     }
   
   // now scan for the beging of a word

  while(strsize >= 0 && str[strsize] != ' ') strsize--;
 
    wordReadPos = strsize + 1;
   
   while(wordReadPos != endword)
      {
       buffer[writPos++] = str[wordReadPos++];
      }  // copy word
   }

  buffer[writePos]  = '\0';

  strcpy(str, buffer);
  
  free(buffer);

 return 0;
}

I don't see why the function has to be so complicated..

void rev_str(char *s)
{
	int str_size = strlen(s);
	char *end = s + str_size - 1;
	
	while ( s < end )
	{
		char temp = *s;
		*s++ = *end;
		*end-- = temp;
	}
}

Which assumes the string is writable.

> I don't see why the function has to be so complicated..

Mostly because it reverses characters in the string, not the words. But it is a good start. Once the characters are reversed, you can apply the same function to each individual word, and reach the goal.

> I don't see why the function has to be so complicated..

Mostly because it reverses characters in the string, not the words. But it is a good start. Once the characters are reversed, you can apply the same function to each individual word, and reach the goal.

This is funny, the OP had a line 'Please tell me how to reverse string in c like taht.' and I assume he meant reverse the string like taht. I didn't realize it was a typo.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.