Write program to reverse a String.
Source 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
| #include<iostream>
using namespace std;
int main( )
{
char str[80];
cout<<"Enter string: ";
cin.getline(str, 80);
int l; //Hold length of string
for(l = 0; str[l] != '\0'; l++); //finding length of string
int temp;
for(int i = 0, j = l - 1; i < l/2; i++, j--)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
}
cout << "Reverse string: " << str << endl;
return 0;
}
|
Post a Comment