Array Issue! Assistance Please

Write your question here.
Hi all, I am fairly new to coding and really could use some help with this exersize.

"Write a program that prints the first letter of each word in a given sentence forwards and backwards."

I was given the hint to use an array to store the first letter of every word. The goal is to have something like... "Hey there how are you" output as "Hthay" and then reversed as "yahtH".

I need to help starting off and any help or suggestions are helpful :)
I suggest getting the entire line into a buffer, then use a for loop to find all the spaces. For each space found, take the letter immediately after it and store it in another buffer as your final result. Reverse the first result with another loop for the reversed result. Make sure that you include the first letter, which is a special case because it doesn't have anything before it.
interesting exercise. please post working code here when you got it already :D
closed account (oy0Gy60M)
I was given the hint to use an array to store the first letter of every word. The goal is to have something like... "Hey there how are you" output as "Hthay" and then reversed as "yahtH".

Pseudo-code code may be :
1
2
3
4
5
6
7
8
9
10
11
12
13
getline(cin, str);
stringstream ss;
string word;
string abbreviation;
ss << str;
while(ss >> word)
{
    abbreviation += word[0];
}

cout << abbreviation << endl;
reverse(abbreviation.begin(), abbreviation.end());
cout << abbreviation << endl;
Last edited on
closed account (48T7M4Gy)
Take advantage of the fact that a <string> string is already useable as an array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
    std::string line;
    
    std::cout << "Enter a sentence: ";
    std::getline(std::cin, line);
    
    std::cout << line[???];
    for(int i = 0; i <  ???;  i++)
    {
        if (line[???] == ???
            std::cout << line[ ...];
    }
    std::cout <<???;
    
    return 0;
}
Enter a sentence: Hey there how are you
Hthay
Program ended with exit code: 0
Last edited on
closed account (oy0Gy60M)
@kemort
How about "yahtH"?
closed account (48T7M4Gy)
How about "yahtH"?


Simply create a new string instead of printing the characters out and a reverse iteration through it.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
    std::string line;
    std::string answer;
    
    std::cout << "Enter a few words: ";
    std::getline(std::cin, line);
    
    answer = line[0];
    for(int i = 0; i < line.length() - 1; i++)
    {
        if (line[i] == ' ')
            answer += line[i + 1];
    }
    std::cout << "Answer: " << answer << '\n';
    
    for (int i = answer.length(); i >= 0; i--)
        std::cout << answer[i];
    std::cout << '\n';
    
    return 0;
}
use this
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
#include<iostream>
#include<string>
using namespace std;

int main()
{
	char size[1000];
	cin.get(size,100);
	cout << "Name : ";
	cout << size;

	// now run the array in reverse
	cout << "\nName in reverse : ";

	int num = strlen(size); // check how many chracter the name contain
	for (int i = num; i >= 0; i--)
	{
		cout << size[i];
	}

		cout << endl;


	system("pause");
}
Last edited on
closed account (48T7M4Gy)
Reverse a string answer:

1
2
3
for (std::string::reverse_iterator rit=answer.rbegin(); rit!=answer.rend(); ++rit)
        std::cout << *rit ;
    std::cout << '\n';
Topic archived. No new replies allowed.