-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreversedNames.cpp
More file actions
55 lines (44 loc) · 1.05 KB
/
Copy pathreversedNames.cpp
File metadata and controls
55 lines (44 loc) · 1.05 KB
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
52
53
54
55
#include <iostream>
#include <fstream>
#include <string.h>
using namespace std;
// Function to reverse a string in place
void reverse(char *name)
{
int n = strlen(name);
char ch;
for (int i = 0; i < n / 2; i++)
{
ch = name[i];
name[i] = name[n - i - 1];
name[n - 1 - i] = ch;
}
}
int main()
{
int n;
cout << "Enter number of names to be read: ";
cin >> n;
char buffer[20]; // buffer to store name
fstream fp1, fp2;
fp1.open("names.txt", ios::out); // opening in write mode
cout << "Enter the names, one per line:" << endl;
for (int i = 0; i < n; i++)
{
cin >> buffer;
fp1 << buffer << endl;
}
fp1.close();
fp1.open("names.txt", ios::in); // Read
fp2.open("revnames.txt", ios::out); // write
cout << "The reversed names are:" << endl;
for (int i = 0; i < n; i++)
{
fp1 >> buffer;
reverse(buffer); // Reverse the name
fp2 << buffer << endl;
cout << buffer << endl;
}
fp1.close();
fp2.close();
}