问:C ++ - 使用类反转字符串(句子和单词)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了问:C ++ - 使用类反转字符串(句子和单词)相关的知识,希望对你有一定的参考价值。
对于我的代码,我试图创建一个具有两个函数的类:
- 显示每个单词反转的cstring
- 显示整个cstring反转
我的两个测试句子是"Hi There"
和"To Be"
,所以输出是:
erehT iH
eB oT
iH erehT
oT eB
这是我的代码:
#include <iostream>
#include <cstring>
using namespace std;
class cStringType {
public:
char sentenceInput[80]; //Member variable
void reverse_sentence(); //Member function
void reverse_words(); //Member function
}; //Bottom of cStringType
int main()
{
cStringType sentence1, sentence2;
//Objects declared of cStringType
cout << "Please enter a sentence!
" << endl;
cin.get(sentence1.sentenceInput, 79, '
');
cin.ignore(80, '
');
cout << "
Please enter another sentence!
" << endl;
cin.get(sentence2.sentenceInput, 79, '
');
cout << "
The first sentence reversed: ";
sentence1.reverse_sentence();
cout << endl;
cout << "The second sentence where each word is reversed: ";
sentence2.reverse_words();
cout << endl;
cout << endl;
cout << "The first sentence where each word is reversed: ";
sentence1.reverse_words();
cout << endl;
cout << "The second sentence reversed: ";
sentence2.reverse_sentence();
cout << endl;
return 0;
}
void cStringType::reverse_sentence()
{
char reverse_sentence;
//Reverse entire sentence using loop
for (int i = 0; i < strlen(sentenceInput) / 2; i++)
{
//Reverse the sentence using the length of the
//variable in the class
reverse_sentence = sentenceInput[i];
//First get the user input
//Set your variable equal to the variable in the class
sentenceInput[i] = sentenceInput[strlen(sentenceInput) - i - 1];
//Then reverse the characters and word order
//Starts from the last character in the array
//and goes backwards to 0
sentenceInput[strlen(sentenceInput) - i - 1] = reverse_sentence;
//Set the variable equal to the result
//sentenceInput is now the reverse of the user input in main
}
cout << sentenceInput << endl;
//Output of the new sentence
}
void cStringType::reverse_words()
{
int beginning, end, j = 0;
char reverse_words;
//Reverse each word separately using loop
for (int i = 0; i <= strlen(sentenceInput); i++)
//Get the length of the sentence in the class
{
if (sentenceInput[i] == ' ' || sentenceInput[i] == ' ')
//Check for spaces or null characters
//This allows only the letters of each word to be
//reversed, not the entire sentence
{
for (beginning = j, end = i - 1;
beginning < (i + j) / 2; beginning++, end--)
//j is the beginning of the array; increases
//i is the end of the array; decreases
{
reverse_words = sentenceInput[beginning];
//Set a variable equal to the first
//word in the original user input
sentenceInput[beginning] = sentenceInput[end];
//Set the first letter of a word equal to
//the last letter of a word
sentenceInput[end] = reverse_words;
//Set the result equal to the variable
//sentenceInput is now the user input where each
//word is reversed
}
}
j = i + 1;
}
cout << sentenceInput << endl;
//Output of the new sentence
}
当我尝试运行代码时,输出变为如下所示:
Please enter a sentence! Hi There Please enter another sentence! To Be The first sentence reversed: erehT iH The second sentence where each word is reversed: oT eB The first sentence where each word is reversed: There Hi The second sentence reversed: Be To
我试着修理它,但无济于事。输出永远不会正确。
有什么方法可以解决这个问题吗?或者更好的是,简化代码?我相信问题在于函数中的代码。
答案
您的代码的主要问题是它为两个转换使用相同的缓冲区。换句话说:你正在颠倒你已经完全逆转的同一个字符串中的单词。因此,您需要拥有原始字符串的另一个副本才能独立完成这些操作。
关于简化代码,您需要定义一个函数,该函数可以在给定指针和大小或begin
和end
指针的情况下反转字符串。然后,您可以在整个字符串或搜索空格字符时找到的每个单词上使用此函数:
char *begin = sentenceInput; //points to the beginning of the word
char *end = sentenceInput + strlen(sentenceInput);
for (char *it = begin; it != end; ++it)
if (*it == ' ') {
reverse(begin, it);
begin = it + 1;
}
reverse(begin, end); //reverse the last word
reverse
函数可以是std::reverse
,可以在上面的代码和整个字符串中使用,如下所示:
std::reverse(sentenceInput, sentenceInput + strlen(sentenceInput))
或者你可以像这样创建一个类似的函数:
void reverse(char *begin, char *end)
{
--end; //point to the last character instead of one-past-last
while (begin < end)
std::swap(*begin++, *end--);
}
另一答案
我建议使用堆栈,它是一种自然的方式来看待它。
所以
#include <stack>
然后功能就是这样
void cStringType::reverse_words()
{
int beginning, end, j = 0;
char reverse_words;
stack<char> lastWord;
//Reverse each word separately using loop
for (int i = 0; i <= strlen(sentenceInput); i++)
//Get the length of the sentence in the class
{
if (sentenceInput[i] == ' ' || sentenceInput[i] == ' ')
//Check for spaces or null characters
//This allows only the letters of each word to be
//reversed, not the entire sentence
{
//we want to print the last word that was parsed
while(!lastWord.empty())
{
//we print in the reverse order the word by taking off the stack char by char
cout<< lastWord.top();
lastWord.pop();
}
cout<<" ";
}
//if the letter is not space or end of string then push it on the stack
else
lastWord.push(sentenceInput[i]);
j = i + 1;
}
cout << sentenceInput << endl;
//Output of the new sentence
}
以上是关于问:C ++ - 使用类反转字符串(句子和单词)的主要内容,如果未能解决你的问题,请参考以下文章