String reverse output-fix needed

Hello !

I need to do program where i will input sentence and then output it in reverse order. Example: today is Monday ----> Monday is today. So I have 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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <cstring>
using namespace std;
 
int main()
{
    char niz[20],niz2[20];
    int lenght,k=0;
     
    cout<<"Sentence: ";
    cin.getline(niz,20);
     
    cout << endl << niz << endl;
    lenght=strlen(niz);
    int temp=lenght;
    for (int i=lenght; i>=0; i--)
    {
         if (niz[i] == ' ')
         {
             for (int j=i; j<=temp; j++) 
              {
                  if (niz[j]!= ' '){   
                     niz2[k] = niz[j];
                     k++;
                     }
              }        
               
              niz2[k+1] = ' '; 
              temp = i;  
         }
         else if (i==0) 
         {
             for (int j=0; j<=temp; j++)  
             {
                 niz2[k] = niz[j];
             }
         }
    }
     
    cout << niz2 << endl;
    return 0;
}


For output I only get one word of sentence,example: Monday is today--->today and nothing else. So i would like to ask if someone can fix program,thanks.
Last edited on
Let assume that temp has the initial value of length. In this case loop

1
2
3
4
5
6
7
            for (int j=i; j<=temp; j++) 
              {
                  if (niz[j]!= ' '){   
                     niz2[k] = niz[j];
                     k++;
                     }
              } 


copies the terminating zero byte in array niz2 because niz[temp] == '\0'.

So statement

cout << niz2 << endl;

will output characters until it will encountered '\0'.

If a character array s has the length = strlen( s ) equal to N then s[N] == '\0'
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

void reverse() ;

int main()
{
    std::cout << "Sentence: " ;
    reverse() ;
}

void reverse()
{
    std::string word ;
    std::cin >> word ;

    if ( std::cin.peek() != '\n' )
        reverse() ;
    else
        std::cout << "Reversed: " ;

    std::cout << word << ' ';
}
Sentence: i am yoda
Reversed: yoda am i
your code
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cstring>
using namespace std;
 
int main(){
	char niz[20] = {' '};
	char niz2[20] = {' '};
    int lenght,k=0;
    int j = 0;
    cout<<"Sentence: ";
    cin.getline(niz,20);
     
    cout << endl << niz << endl;
    lenght=strlen(niz);
    int temp=lenght;
    for (int i=lenght; i>=0; i--)
    {
         if (niz[i] == ' ')
         {
             for (j=i; j<temp; j++) 
              {
                  if (niz[j]!= ' ')
				  {   
                     niz2[k] = niz[j];
                     k++;
                  }
              } 
			 k++;
              niz2[k - 1] = ' '; 
              temp = i;  
         }
         else if (i==0) 
         {
             for (int j=0; j<=temp; j++)  
             {
                 niz2[k] = niz[j];
				 k++;
             }
         }
    }
     
    cout << niz2 << endl;
	cin.ignore();
    return 0;
}



my code
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
32
33
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
	stringstream ss;
	vector<string> words;
	string temp;

	cout << "Please enter a string: ";
	getline(cin, temp);

	ss << temp;

	while(ss >> temp)
	{
		words.push_back(temp);
	}

	vector<string>::iterator it;

	for (it = words.end(); it != words.begin(); it--)
	{
		
		cout << *(it - 1) << ' ';
	}

	cin.ignore();
	return 0;
}
@Yanson


Instead of

1
2
3
4
5
6
7
8
	vector<string>::iterator it;


	for (it = words.end(); it != words.begin(); it--)
	{
		
		cout << *(it - 1) << ' ';
	}


it would be much better to write

1
2
3
4
5
	for ( auto it = words.rbegin(); it != words.rend(); ++it)
	{
		
		cout << *it << ' ';
	}

Last edited on
thanks vlad!
Topic archived. No new replies allowed.