Skip to content

Commit 64a0aef

Browse files
authored
Create reverse-string.cpp
1 parent db94823 commit 64a0aef

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

reverse-string.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// C++ program to demonstrate how to reverse a string accepted from a user using character array
2+
3+
#include <iostream>
4+
using namespace std;
5+
void reverse(char [], int);
6+
int main()
7+
{
8+
char str[15]; int i = -1;
9+
10+
do {
11+
i++;
12+
str[i] = getchar();
13+
} while (str[i] != '\n' && i<14);
14+
15+
int length = i;
16+
reverse(str, length);
17+
18+
for (int j = 0; j < length; j++) {
19+
cout << str[j];
20+
}
21+
return 0;
22+
}
23+
void reverse(char arr[], int size) {
24+
int start = 0, end = size - 1;
25+
while (start < end) {
26+
char temp = arr[start];
27+
arr[start] = arr[end];
28+
arr[end] = temp;
29+
start++;
30+
end--;
31+
}
32+
}

0 commit comments

Comments
 (0)