C code to C++

I need to use C++ codes but this code I have is currently a C code.
What is the C++ code version of this?


without using " printf( ) "
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

using namespace std;

void main() 
{ 
	string name;
	cout << "Enter name: ";
	cin >> name;
	cout << endl << "Reverse string is :";
	reverse(name.begin(), name.end());
	cout << name << endl << endl;
	system("pause");
}
Last edited on
I need to convert the line printf("%c",pop(&s)); in C++ using cout<<
i must apply implementation of stacks in my code, that's why i cant use reverse string / strrev.
Well you asked for it to be converted to C++ you didn't say that you couldn't use reverse(...). Why can't you use reverse(...)?
Well, without using std::reverse, and using std::stack:

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
#include <iostream>
#include <stack>
#include <string>

using namespace std;

void add(stack<char>& s, char c)
{
	s.push(c);
}

void main() 
{ 
	stack<char> s;
	string name;
	cout << "Enter name: ";
	cin >> name;

	for (string::iterator p = name.begin(); p != name.end(); ++p)
		add(s, *p);

	cout << endl << "Reverse string is :";
	
	while (!s.empty())
	{
		cout << s.top();
		s.pop();
	}

	system("pause");
}
Topic archived. No new replies allowed.