Reversing words in a string
ReverseWordsInString() reverses the words in a a string, it takes the string as its input parameter and returns the string with words reversed.
For example
If the string is “I love my countryâ€, then this function will return “country my love Iâ€.
Code of ReverseWordsInString()
Summary of ReverseWordsInString()
The string is “I love my countryâ€, then after the end of the for loop, the string would be;
“I evol ym yrtnuocâ€
After the end of the for loop, this sting is passed to the ReverseString(), which returns the reversed string.Now this time the string “I evol ym yrtnuoc†will become “country my love Iâ€.
Code of ReverseWord()
Summary of ReverseWord()
In the first iteration, first and the last element of the word is swapped,
In the second iteration, second and the second last element of the word is swapped,
And it continues till the below mentioned condition:
Summary of ReverseString()
In the second iteration, second and the second last element is swapped,
And it continues till the below mentioned condition:
Summary of Swap()
Swap() takes 2 input parameters which are the pointers to the elements to be swapped.
First a temporary pointer temp is declared,
char temp;
which holds the data during swapping,
then the second element value2 is stored in temp
temp = *value2;
then the first element value1 is placed in the second element value2
*value2 = *value1;
and at the last the value of temp is assigned to value1
*value1 = temp;
Example (C++)
Output
I love my country
country my love I
ReverseWordsInString() reverses the words in a a string, it takes the string as its input parameter and returns the string with words reversed.
For example
If the string is “I love my countryâ€, then this function will return “country my love Iâ€.
Code of ReverseWordsInString()
Code:
char * ReverseWordsInString( char str[] )
{
int start = 0;
int end = 0;
for ( int i = 0; i < strlen( str ); i++ )
{
if( str[i] == ' ' || i == strlen( str ) - 1 )
{
if ( i == strlen( str ) - 1 )
i = i + 1;
end = i - 1;
ReverseWord( str , start , end );
start = i + 1;
}
}
ReverseString( str );
return str;
}
For exampleReverseWordsInString() takes the string and returns the string with words reversed.
There are 2 variables; start and end, both these variables are initialized by 0.
There is a for loop which starts from the 0th index of the string and iterates till the length of the string.
The pupose of this loop is to reverse each word in the string.
The string is “I love my countryâ€, then after the end of the for loop, the string would be;
“I evol ym yrtnuocâ€
After the end of the for loop, this sting is passed to the ReverseString(), which returns the reversed string.Now this time the string “I evol ym yrtnuoc†will become “country my love Iâ€.
Code of ReverseWord()
Code:
void ReverseWord( char str[] , int start , int end )
{
for ( int i = 0 , j = end; i <= ( end - start ) / 2; i++ )
Swap( &str[start++] , &str[j--] );
}
There are 3 variables; i, start and end in the for loop, i controls the loop – how many times till the loop executes, start starts from the 0th index of the word and increments by 1 in every iteration of the for loop, and end starts from the last index of the word and decrements by 1 in every iteration. The loop continues half times the length of the word. At every iteration two elements are swapped,ReverseWord() reverses the word, it takes 3 parameters, first is the string, second is the staring index of the word from the whole string and third parameter is the ending index of the word.
In the first iteration, first and the last element of the word is swapped,
In the second iteration, second and the second last element of the word is swapped,
And it continues till the below mentioned condition:
i <= ( end - start ) / 2
Code of ReverseString()
Code:
void ReverseString( char str[] )
{
for ( int i = 0 ,j = strlen( str ) - 1; i < strlen(str) / 2; i++ , j-- )
Swap( &str[i] , &str[j] );
}
In the first iteration, first and the last element is swapped,ReverseString() reverses the string, it takes the string to be reversed.
There are 2 variables; i and j in the for loop, i starts from the 0th index of the string and increments by 1 in every iteration of the for loop, and j starts from the second last index of the string and decrements by 1 in every iteration. The loop continues half times the length of the string. At every iteration two elements are swapped,
In the second iteration, second and the second last element is swapped,
And it continues till the below mentioned condition:
i < strlen(str) / 2
Code of Swap()
Code:
void Swap( char * value1 , char * value2 )
{
char temp = *value2;
*value2 = *value1;
*value1 = temp;
}
Swap() takes 2 input parameters which are the pointers to the elements to be swapped.
First a temporary pointer temp is declared,
char temp;
which holds the data during swapping,
then the second element value2 is stored in temp
temp = *value2;
then the first element value1 is placed in the second element value2
*value2 = *value1;
and at the last the value of temp is assigned to value1
*value1 = temp;
Example (C++)
Code:
#include "stdafx.h"
#include "iostream"
#include "conio.h"
using namespace std;
void Swap( char * value1 , char * value2 )
{
char temp = *value2;
*value2 = *value1;
*value1 = temp;
}
void ReverseString( char str[] )
{
for ( int i = 0 ,j = strlen( str ) - 1; i < strlen(str) / 2; i++ , j-- )
Swap( &str[i] , &str[j] );
}
void ReverseWord( char str[] , int start , int end )
{
for ( int i = 0 , j = end; i <= ( end - start ) / 2; i++ )
Swap( &str[start++] , &str[j--] );
}
char * ReverseWordsInString( char str[] )
{
int start = 0;
int end = 0;
for ( int i = 0; i < strlen( str ); i++ )
{
if( str[i] == ' ' || i == strlen( str ) - 1 )
{
if ( i == strlen( str ) - 1 )
i = i + 1;
end = i - 1;
ReverseWord( str , start , end );
start = i + 1;
}
}
ReverseString( str );
return str;
}
int main()
{
char str[]="I love my country";
cout << str << endl;
cout << ReverseWordsInString( str ) << endl;
_getche();
return 0;
}
I love my country
country my love I