Split a sentence in to letters and send to array!

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.

Anyone know how i should do? :)
First put the sentence into a string then fish out the letters one by one by putting them into an array.
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.
But how do i make sure that spaces does not enter the array?
Test each character (using an if statement) as you copy it from the input string to your destination array, a character at a time.
First replace all spaces in the string with std::string. Here is an example for you to learn from:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#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;
}


Then you can compare the letters and there will be no space in there.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#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;
}


Original: A man, a plan, a canal - Panama!
Copy 1:   Aman,aplan,acanal-Panama!
Copy 2:   amanaplanacanalpanama
Thanks to what you guys said, i came up with this idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<string>
#include<ctime>

using namespace std;

const int CAPACITY=50;

int main()
{
	string input="";
	char sentence[CAPACITY]={};
	char sentenceReversed[CAPACITY]={};

	cout<<"Enter a sentance or a word: ";
	getline(cin, input);
	for(int i=0;i<input.size();i++)
	{
		if(input.at(i)!=' ')
		{
		sentence[i]=input.at(i);
		cout<<sentence[i];
		}
	
	}

	cout<<endl;


	return 0;
}


This code puts the input: "Hello there" into the array "sentence" like this: "Hellothere". And that is what i wanted.

Now i came up with a new problem *sigh*

I want the array "sentenceReversed" to store the values from "sentence" but from last to first.

Visual:

|H| |E| |L| |L| |O| <-- sentence[CAPACITY]
|O| |L| |L| |E| |H| <-- sentenceReversed[CAPACITY]

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:

1
2
3
4
5
6
for(...)
{
sentenceReversed[counter-1]=sentence[i];
counter--;
}


But this did not work.

If you have an idea to solve this, I would be glad if i could get som hints or ideas :)


Last edited on
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.
Topic archived. No new replies allowed.