No. 07 - Reverse words in a sentence
来源:互联网 发布:五五开淘宝店地址 编辑:程序博客网 时间:2024/05/01 17:44
No. 07 - Reverse words in a sentence
Problem: Reverse the order of words in a sentence, but keep words themselves unchanged. Words in a sentence are divided by blanks. For instance, the reversed output should be “student. a am I” when the input is “I am a student”.
Analysis: This is a very popular interview question of many companies. It can be solved with two steps: Firstly we reverse all characters in a sentence. If all characters in sentence “I am a student.” are reversed, it becomes “.tneduts a ma I”. Not only the order of words is reversed, but also the order of characters inside each word is reversed. Secondly, we reverse characters in every word. We can get “student. a am I” from the example input string with these two steps.
The key of our solution is to implement a function to reverse a string, which is shown as the Reverse function below:
void Reverse(char *pBegin, char *pEnd)
{
if(pBegin == NULL || pEnd == NULL)
return;
while(pBegin < pEnd)
{
char temp = *pBegin;
*pBegin = *pEnd;
*pEnd = temp;
pBegin ++, pEnd --;
}
}
Now we can reverse the whole sentence and each word based on this Reverse function with the following code:
char* ReverseSentence(char *pData)
{
if(pData == NULL)
return NULL;
char *pBegin = pData;
char *pEnd = pData;
while(*pEnd != '\0')
pEnd ++;
pEnd--;
// Reverse the whole sentence
Reverse(pBegin, pEnd);
// Reverse every word in the sentence
pBegin = pEnd = pData;
while(*pBegin != '\0')
{
if(*pBegin == ' ')
{
pBegin ++;
pEnd ++;
}
else if(*pEnd == ' ' || *pEnd == '\0')
{
Reverse(pBegin, --pEnd);
pBegin = ++pEnd;
}
else
{
pEnd ++;
}
}
return pData;
}
Since words are separated by blanks, we can get the beginning position and ending position of each word by scanning blanks. In the second phrase to reverse each word in the sample code above, the pointer pBegin points to the first character of a word, and pEnd points to the last character.
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a referenced tohttp://codercareer.blogspot.com/. If you are going to use it in your books, please contact the author via zhedahht@gmail.com . Thanks.
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a referenced tohttp://codercareer.blogspot.com/. If you are going to use it in your books, please contact the author via zhedahht@gmail.com . Thanks.
- No. 07 - Reverse words in a sentence
- reverse words in a sentence
- Reverse the words in a sentence in place
- Leetcode NO.186 Reverse Words in a String II
- leetCode No.151 Reverse Words in a String
- Reverse words in a string.
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- Reverse Words in a String
- HDOJ 1588 - Gauss Fibonacci
- No. 04 - Paths with Specified Sum in Binary Tree
- No. 05 - The Least k Numbers
- 初学者对通讯录软件开发的整体把握与分块实现------添加列表新内容并实现界面的跳转
- No. 06 - Post-order Traversal Sequences of Binary Search Trees
- No. 07 - Reverse words in a sentence
- No. 08 - Calculate 1+2+…+n
- Semantics in CG language
- 2012-7-12 周五 总结
- No. 09 - Numbers with a Given Sum
- No. 10 - K-th Node from End
- No. 11 - Print Binary Trees from Top to Bottom
- [开源] 本blog的开源项目
- 长方体类