-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReverseString.cpp
51 lines (43 loc) · 1.23 KB
/
ReverseString.cpp
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
46
47
48
49
50
51
#include <iostream>
using namespace std;
/* function to reverse char array */
void reverseString(char name[], int size) {
/* starting and last index of char array */
int start = 0;
int end = size - 1;
/* reversing char array */
while (start < end) {
/* swapping first with last and moving to next
to reverse char array */
swap(name[start++], name[end--]);
}
}
/* function to find length of char array */
int getLength(char name[]) {
/* size of char array*/
int size = 0;
/* loop will run until it finds termination character = '\0' */
for (int i = 0; name[i] != '\0'; i++) {
size++;
}
/* returing size */
return size;
}
int main() {
/* array to store name (one word only) */
char name[50];
cout << "Enter your name: ";
cin >> name;
/* printing name */
cout << "Your name is: " << name << endl;
/* finding length of char array i.e. name
and storing in len*/
int len = getLength(name);
/* printing length of char array */
cout << "Length of string: " << len << endl;
/* reversing char array */
reverseString(name, len);
/* printing reversed char array*/
cout << "Reversed string: " << name << endl;
return 0;
}