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”.
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;
}
Summary of ReverseWordsInString()
ReverseWordsInString() 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.
For example
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()
void ReverseWord( char str[] , int start , int end )
{
for ( int i = 0 , j = end; i <= ( end - start ) /
2; i++ )
Swap(
&str[start++] , &str[j--] );
}
Summary of ReverseWord()
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.
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,
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()
void ReverseString( char str[] )
{
for ( int i = 0 ,j = strlen( str ) - 1; i <
strlen(str) / 2; i++ , j-- )
Swap(
&str[i] , &str[j] );
}
Summary of ReverseString()
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 first iteration, first and the last element is 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()
void Swap( char * value1 , char * value2 )
{
char temp = *value2;
*value2
= *value1;
*value1
= temp;
}
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++)
#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;
}
Output
I love my country
country my love I
0 comments:
Post a Comment