Even though you have written "Write your question here." and used code tags (I'd actually like to congratulate you, so few new users use them), I'm afraid that 51 lines of code is not a question. Neither is the title, though it is an imperative and therefore commands us to help you, and nowhere do you provide any errors which anyone could fix.
The program should be able to change every four letter word in the sentence typed in by the user to the word "love". The user types in a sentence. After the program has changed the sentence to lower and upper case and taken out the spaces, it must check for words which are four characters in length and change the word to "love"
I need someone to show me how to find every four letter word in the sentence and change it to the word "love'. Every four letter word in the sentence. i managed to do the first part of the problem that is, to change the first letter of the sentence to upper and to take out space and change any capitals to lower in the sentence. its only the last part I am having a problem with to change the four letter word.
Then instead of removing all the spaces to start with, use them to find the amount of times you have to increment your position along the string until you find another one. If you find a whitespace, then increment 5 times and find the next whitespace, then you have found a 4 letter word and you change it to "love". Then when you've been through the whole string, you remove the spaces.
Work it out first with pen and paper then translate the process into C++ code.
Leaving out the capitalization part here is some code which replaces each four letter word with the word LOVE.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string sentence;
int charCount,charPos;
char letter;
sentence = "this is a test of the program but will it work";
charCount = 0;
cout << sentence << endl;
for (charPos=0; charPos<sentence.size();charPos++)
{
letter = sentence[charPos];
charCount = charCount + 1; // count the letters found
if (letter == 32) // found a space
{
if (charCount == 5) // count includes the space after the number of letters
{
sentence[charPos-4]='L'; // replace the last four positions with the word LOVE
sentence[charPos-3]='O';
sentence[charPos-2]='V';
sentence[charPos-1]='E';
}
charCount=0; // reset length of words
}
}
if (charCount == 4) // deal with any last word
{
sentence[charPos-4]='L'; // replace the last four positions with the word LOVE
sentence[charPos-3]='O';
sentence[charPos-2]='V';
sentence[charPos-1]='E';
}
cout << sentence << endl;
return 0;
}
You need to use the string tokenizer which is a function that splits up the line to produce pointers to tokens (words) in the sentence. Then use string length functionality to check each word length and make the necessary change(s).
#include <iostream>
#include <string>
using std::cin;
using std::cout;
using std::string;
// Given a stirng and the starting position of a word, return
// the postiion just past the end of the word. For example,
// if str is "Come in Tokyo" and pos is 5 (the start of "in") then
// this returns 7 (the position just past "in")
size_t
endOfWord(const string & str, size_t pos)
{
while (pos < str.size() && isalpha(str[pos])) {
++pos;
}
return pos;
}
// Given a string and a starting position, return the position of the
// start of the next word. In other words, skip non-letters until you
// find one. Returns str.size() if there are no more words
size_t
startOfWord(const string & str, size_t pos)
{
while (pos < str.size() && !isalpha(str[pos])) {
++pos;
}
return pos;
}
int
main()
{
size_t start, end;
string str;
getline(cin, str);
for (start = startOfWord(str, 0);
start < str.size(); start = startOfWord(str, end)) {
end = endOfWord(str, start); // find end of current word
if (end - start == 4) {
// replace 4 letter word with love
str.replace(start, 4, "love");
}
}
cout << str << '\n';
}
fonz wrote: It only changes the first letter of the word.
Strange it worked fine for me. There were some errors in dealing with any four letter word at the start and end of the sentence which I have since corrected in the previous post.
You could use strtok or the str.replace but rolling your own means you know how it works.
#include <iostream>
#include <string>
#include <algorithm>
#include <sstream>
#include <cctype>
usingnamespace std;
int main()
{
std::cout << "Please enter a sentence ";
std::string input;
std::getline(std::cin, input);
std::string word;
std::stringstream ss(input);
int count = 0;
while(ss >> word)
{
//Make the entire word lowercase
std::transform(word.begin(), word.end(), word.begin(), ::tolower);
//change the word to love when a 4 letter word is found
if(word.size() == 4)
word = "love";
//Make the first word in the sentence uppercase
if(count == 0)
word.front() = toupper(word.front());
//Print the word
std::cout << word << ' ';
++count;
}
return 0;
}
Note: This code does not account for things that are not words such as numbers