-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReverseastringusingStack.cpp
More file actions
84 lines (64 loc) · 1.51 KB
/
ReverseastringusingStack.cpp
File metadata and controls
84 lines (64 loc) · 1.51 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//{ Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
char* reverse(char *str, int len);
int main()
{
long long int t;
cin>>t;
while(t--)
{
char str[10000];
cin>>str;
long long int len=strlen(str);
char *ch=reverse(str,len);
for(int i=0;i<len;i++)
{
cout<<ch[i];
}
cout<<endl;
}
return 0;
}
// } Driver Code Ends
//return the address of the string
char* reverse(char *S, int len)
{
stack<char> charStack;
// Push characters onto the stack
for (int i = 0; i < len; i++) {
charStack.push(S[i]);
}
// Pop characters from the stack to reverse the string
for (int i = 0; i < len; i++) {
S[i] = charStack.top();
charStack.pop();
}
return S;
}
chatgpt solution
#include <iostream>
#include <stack>
using namespace std;
char* reverse(char *S, int len)
{
stack<char> charStack;
// Push characters onto the stack
for (int i = 0; i < len; i++) {
charStack.push(S[i]);
}
// Pop characters from the stack to reverse the string
for (int i = 0; i < len; i++) {
S[i] = charStack.top();
charStack.pop();
}
return S;
}
int main() {
char str[] = "Hello, World!";
int len = sizeof(str) / sizeof(str[0]) - 1; // Exclude the null terminator
cout << "Original String: " << str << endl;
char* reversedStr = reverse(str, len);
cout << "Reversed String: " << reversedStr << endl;
return 0;
}