Reverse each word in a string c++
RELATED POSTS
- Blogger Comment
- Facebook Comment
Subscribe to:
Post Comments
(
Atom
)
C/C++,java,python c++ keywords, identifiers, data types,variables,modifiers,storage classes,operators,for loop ,while loop,do while loop,if else statement,nested loops,break continue goto statement,functions ,call by value call by reference ,arrays ,strings,pointers,data structures ,classes and objects,inheritance,polymorphism,operator overloading,function overloading,data abstraction,data encapsulation,dynamic memory allocation for array and objects,function templates,class templates,
Reverse each word in a string c++
Input Sentence: "Hello, I am Aadil!"
The expected output will print, ",olleH I ma !lidaA".
The first and only line of input contains a string without any leading and trailing spaces. The input string represents the sentence given to Aadil.
The only line of output prints the sentence(string) such that each word in the sentence is reversed.
0 <= N <= 10^6
Where N is the length of the input string.
Time Limit: 1 second
Welcome to Coding Ninjas
emocleW ot gnidoC sajniN
Always indent your code
syawlA tnedni ruoy edoc
C++ Code:
void reverseIndex(char s[], int i, int j)
{
while(i<j)
{
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
void reverseEachWord(char input[]) {
if(input==nullptr) return;
int read=0;
while(input[read] != '\0')
{
while(input[read] != '\0' && isspace(input[read]))
read++;
if(input[read] == '\0')
break;
int start = read++;
while(input[read] != '\0' && !isspace(input[read]))
read++;
reverseIndex(input, start, read-1);
}
}
0 comments:
Post a Comment