This is homework- I have to use C-strings and in this code I need to replace all four letter words with the word love. My original idea was to perform a check to make sure I have 4 letters together and then change those instances to be 'l', 'o', 'v', 'e' respectively. I encountered a break point in my if statement that checked to make sure I had four letters. Here is what it looks like(also please keep the language simple, I am really new with programming):
oLLLLo where o is not a letter, and L is a letter. o can be nothing (start of string, does not exist, or end of string, which is the numerical value zero) or other things like space, punctuation, etc.
once you replace, start looking again at positon o (the second one) in the string.
At a guess (working it by hand here) ... does it happen to replace a 6 letter word with lllove?
You are using isalpha wrong; It returns a bool, not a char.
1 2 3 4
if(isalpha(userString[i+1]) == true)
{
// it is an alphabetical character.
}
Instead of the getline function for cin, I'd suggest the >> operator, this will get one word at a time so you don't have to worry about checking before and after for white-space.
1 2 3 4
while(cin >> userWord)
{
// do your checking here.
}
You could then use strlen from <cstring> to tell you if the word is 4 letters, but you'd still have to check that it didn't have numbers in it....
I think my professor wants us to use the getline function. I changed that too, but I still have a break when I try to enter my for loop. Here is the updated code:
iterate until you find zero as a character in the string. That is the end of string marker in C.
you also iterate too far, you cant check array[102] which happens when I = 100... its out of bounds.
char userString[101]; //this is accessible from 0 to 100
cout << "Please enter a sentence to be modified: " << endl;
cin.getline(userString, 100);
for (int i = 0; i < 101; i++) //this is ok but...
//down here you access [I+5]. that is not ok at 99.. etc
Because I can't use strlen because the sentence might be multiple words long.
The use of strlen isn't required, but it would certainly simplify the logic inside the loop. What makes you think a string with multiple tokens in it makes strlen useless?
Again I'm still pretty new into programming. I thought that strlen just returned the int value of the length of the string in total. I don't know how to use it to find only four letter sections. But I guess if I don't need strlen, how can I fix this so that I don't get "assertion failure" errors?
I thought that strlen just returned the int value of the length of the string in total.
It does. That is quite useful when you don't want to exceed the length of the string in total as you do not want to do.
I don't know how to use it to find only four letter sections.
Again, that isn't what you would use it for. You would use it for its intended purpose - to find the length of a C-string.
1 2 3 4 5 6 7 8 9 10 11 12 13
char userString[101]; //this is accessible from 0 to 100
cout << "Please enter a sentence to be modified: " << endl;
cin.getline(userString, 100);
unsigned length = strlen(userString);
unsigned tokenLength = 4;
// Since it is impossible to find a token of length tokenLength in
// the last bit of a string that is smaller than that, avoid looking for one there:
for (unsigned i = 0; i < length - tokenLength; i++) {
// ...
}
That definitely took care of the assertion failure, however now I'm kinda lost for how to change those four letters to what I need them to be changed to. Does the new for loop parameter not allow me to use i as an index of userString like I did previously (i.e. userString[i+1] = 'l'; ?
#include<iostream>
usingnamespace std;
int main(){
bool runAgain;
do{
char userString[100];
cout << "Please enter a sentence to be modified: " << endl;
cin.getline(userString, 100);
int count = 0;
for (int i = 0; i < 100; i++){
if ( isalpha(userString[i]) ){count++;}
else
{
if(count != 4){count = 0;}
if (count == 4){
userString[i-4] = 'l';
userString[i-3] = 'o';
userString[i-2] = 'v';
userString[i-1] = 'e';
count = 0;
}
}
}
cout << userString<<endl;
cout << "Would you like to run this again? (1 for yes, 0 for no)" << endl;
cin >> runAgain;
cin.ignore(1000, '\n');
}while (runAgain == true);
system("pause");
return 0;
}
Please enter a sentence to be modified:
what is this my dear?frie?
love is love my love?love?
Would you like to run this again? (1 for yes, 0 for no)
1
Please enter a sentence to be modified:
what four>
love love>
Would you like to run this again? (1 for yes, 0 for no)
Please enter a sentence to be modified:
what four
love love
Would you like to run this again? (1 for yes, 0 for no)
Does the new for loop parameter not allow me to use i as an index of userString like I did previously (i.e. userString[i+1] = 'l'; ?
You're free to use i as an index.
In Kemort's code you should check out the use of count to track the state required (the length of the current token) as an alternative to your nested if statements (but keep in mind Kemort's overall algorithm processes "unused" portions of the buffer which don't need to be touched.)