My First String Reversal Program

Hey There,

I just finished this a while ago. I'm sure I've made plenty of errors, but It gets the job done. Any suggestions on how I could "optimize" the code, to make it look prettier? I think I made some statements that really had no affect on the program itself :(

Here it is(My First String Reversal Program):
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
#include <iostream>
#include <string>
#include <stdio.h>
#include <string.h>
char STOP[265];
using namespace std;

int main (void)
{

    cout << "Enter the string to be reversed...\n";
    cin.getline(STOP,265);
   

    int c = strlen(STOP) ;
   
    for (; c >= 0; c--)
    {

        cout << STOP[c];

    }




    return 0;
}


So, any suggestions on what I could've done better? Thanks.

-Code Assassin
Last edited on

The indices of the characters in a string go from 0 to (length - 1). So for a length 4 character string you have character indices 0, 1, 2 and 3.

So try:
 
int c = strlen(STOP) - 1;

Thanks Galik!

I should've thought about that (face-palm) -_-
You can fix that by using pre-decrement:
for (; --c >= 0;)
we can reverse the string in the following way with out traversing the entire length

1
2
3
4
5
6
7
8
9
10
11
12
13
    

    int c = strlen(STOP)-1 ;

    char ch;   
    for (int i=0;i<c/2;i++)
    {	  
	   ch=STOP[i];
	   STOP[i]=STOP[c-i];
	   STOP[c-i]=ch;
    }
    cout<<STOP;
you don't need stdio.h because iostream is the same thing c++ and string shouldn't have the .h

edit: you don't need the .h string for the same reason as above
Last edited on
Thanks Aramil of Elixia.

Never knew that. :)
@NVTKrishna

What? I don't think I went through the whole length... did I?

I updated the code, and here it is:
(Turns out I do need string.h but I was able to take stdio.h out)
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>
#include <string.h>
char STOP[265];
using namespace std;

int main (void)
{

    cout << "Enter the string to be reversed...\n";
    cin.getline(STOP,265);
    

  int c = strlen(STOP) - 1;
    
    for (; c >= 0; c--)
    {

        cout << STOP[c];

    }




    return 0;
}


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
#include<iostream.h>
#include<conio.h>
#include<string.h>
int main()
{
	clrscr();
	char string[3][31], ch ;
	int i, j, len, k ;
	cout<<"Enter 3 strings \n";
	for( i = 0 ; i < 3 ; i++)		//Loop to read strings
	{
		cin.getline(string[i], 31);
	}
	cout<<" The original String is as follows : ";

	for(i = 0 ; i < 3 ; i++)		//Loop to print original strings
		cout<<"\n"<<string[i];

	for(i = 0 ; i < 3; i++)			// Loop to reverse Strings
	{
		len = strlen(string[i]) ;
		for( j = 0, k = len - 1 ; j < len/2 ; j++, k--)
		{
			ch = string[i][j];		// Interchange opposite characters
			string[i][j] = string[i][k];
			string[i][k] = ch;
		}
	}
	cout<<"\n\n The list of Reversed string is as follows : ";
	for(i = 0 ; i < 3 ; i++)
		cout<<"\n"<<string[i];

	return 0;
}

Hoping that this may help!
Topic archived. No new replies allowed.