Hello guys. I have a simple question, i'm about to do a program where i would like to enter a sentence and then split the sentence into letter by letter and then put it in to an array.
For example:
"Enter a sentence"
input: My name is Santa
Now i want the array to save the sentence as this:
[M][y][][n][a][m][e][][i][s][][S][a][n][t][a]
Why i want to do this is because i later want to check if a word or sentence is a palindrome. And if i can do like this then i can just make another array and put in the values from last to first and check if the arrays are equal.
But how do i make sure that spaces does not enter the array? Because if want to test if a sentence is a palindrome, I cannot have spaces between words. I cannot figure out how to either remove the spaces or how to add spaces between every character.
#include <iostream>
#include <string>
int main()
{
int x = 0;
std::string toread = "";
std::cout << "Enter a line of text. Letter f will be ignored.\n";
getline(std::cin, toread); //Assign user inputted line to string toread.
std::cout << "Original: " << toread << "\n"; //Output original string
while(x < toread.length()) //While x is less than the length of string to read
{
if(toread.at(x) == ' ') //Search the string for f
{
toread.replace(x,1, ""); //Replace with nothing
}
++x; //Move on to the next charactor in the string
}
std::cout << "Modified: " << toread << "\n"; //Output the modified string.
return 0;
}
#include <iostream>
#include <string>
int main()
{
std::string original = "A man, a plan, a canal - Panama!";
// To remove only spaces
std::string copy1 = original;
size_t x = 0;
while ((x = copy1.find(' ', x)) != std::string::npos) // a space is found
copy1.replace(x,1, ""); // Replace with nothing
// To remove anything which is not a letter, and make all lowercase
std::string copy2;
for (x=0; x<original.size(); x++ )
if (isalpha(original[x]) )
copy2 += tolower(original[x]);
std::cout << "Original: " << original << "\n";
std::cout << "Copy 1: " << copy1 << "\n";
std::cout << "Copy 2: " << copy2 << "\n";
return 0;
}
Cause then i can check if a word or senctence is equal with its reversed array and in that case it's a palindrome. First i tried to create an int counter= 0
This counter were supposed to increase everytime a word was copied from the input to the sentence array. Then i could use the counter like this:
So you did this int counter= 0 and then used sentenceReversed[counter-1] where the subscript is negative and thus invalid, and then decrement counter for the next iteration, meaning a series of invalid subscripts. I'm surprised the program did not crash.
If you want to reverse a string, there are two approaches I can think of. One way is to copy the last character of the source to the first position of the destination, then the next-to-last to the second and so on. Remember to ensure the destination string is properly terminated (if using c-strings).
1 2 3 4 5
int len = strlen(sentence); // length of sentence
int i;
for (i=0; i<len; i++)
sentenceReversed[i] = sentence[len-i-1];
sentenceReversed[i] = 0; // terminate the string with null
Another way is to reverse the string in place by swapping the first character with the last, second with next-to-last and so on, until the middle is reached. I'll leave that as an exercise for you to try for yourself.
So you did this int counter= 0 and then used sentenceReversed[counter-1] where the subscript is negative and thus invalid, and then decrement counter for the next iteration, meaning a series of invalid subscripts. I'm surprised the program did not crash.
Yes but the int counter=0 was increasing everytime a letter was copied from sentence to sentenceReversed. So that the counter in the end would have the same value as amount of letters in sentenceReversed. After that i knew that if i put the sentenceReversed[counter-1] the sentence [x] would be copied to the value equals to the counter-1. Which would be the number for the last storage in the array. :)
Thanks for the help by the way. It works perfectly with the example you should, But i think it seems nice to do the other way, So i will try figure it out too :)
I don't see where counter was increasing. I only saw this: counter--; where the value started at zero, then was decremented and became -1, -2 and so on. Anyway, it doesn't matter, I think you've moved on past that now.