-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_string.c
More file actions
72 lines (72 loc) · 1.52 KB
/
reverse_string.c
File metadata and controls
72 lines (72 loc) · 1.52 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
// ABC international is checking the company names of its client for palindrome. A string is said to be palindrome if both the input string and the reversed output string are one and the same. So ABC international needs to reverse the names of the companies they have. Write a program to reverse a string using stack implementation. Remember as stack uses LIFO concept the string pushed can be popped out in a reverse order.
// Constraint: String can be of size 10.
// Input: Input string S
// Output: Reverse of a string given as input or overflow if string is above size 10.
// Test Case 1:
// Input:
// madam
// Output
// madam
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 50
struct node
{
char ch;
struct node *next;
} *list = NULL;
typedef struct node stack;
int isEmpty()
{
if (list == NULL)
{
return 1;
}
else
{
return 0;
}
}
void push(char x)
{
stack *newnode = malloc(sizeof(stack));
newnode->ch = x;
if (isEmpty())
{
newnode->next = NULL;
list = newnode;
}
else
{
newnode->next = list;
list = newnode;
}
}
void traverse()
{
stack *pos = list;
while (pos != NULL)
{
printf("%c", pos->ch);
pos = pos->next;
}
}
int main()
{
char st[max];
scanf("%s", st);
if (strlen(st) > 10)
{
printf("Overflow\n");
}
else
{
for (int i = 0; i < strlen(st); i++)
{
push(st[i]);
}
traverse();
}
return 0;
}