3. ALGORITHM CLASSS
// A stack based function to reverese a string
void reverse(char str[])
{
// Create a stack of capacity equal to length of string
int n = strlen(str);
struct Stack* stack = createStack(n);
// Push all characters of string to stack
int i;
for (i = 0; i < n; i++)
push(stack, str[i]);
// Pop all characters of string and put them back to str
for (i = 0; i < n; i++)
str[i] = pop(stack);
}
C C++ DS JAVA TRAINING INSTITUTES IN KPHB HYDERABAD KPHB
4. ALGORITHM CLASSS
C C++ DS Data Structures CPP JAVA TRAINING INSTITUTE KPHB HYDERABAD
Using implicit STACK: Reverse a string
// A stack based function to reverese a string
void reverse(char *str)
{
if ( !str)
return ;
reverse(str+1);
printf("%c",*str);
}
5. ALGORITHM CLASSS
C C++ DS Data Structures CPP JAVA TRAINING INSTITUTE KPHB HYDERABAD
STACK: Reverse a string
Following is simple algorithm to reverse a string using stack.
1) Create an empty stack
1) One by one push all characters of string to stack.
1) One by one pop all characters from stack and put them back to
string.
a b c 0d
10 32 4
top
stack
NULL
top
a
b
d
c
top
top
top
6. ALGORITHM CLASSS
Following is simple algorithm to reverse a string using stack.
1) Create an empty stack
1) One by one push all characters of string to stack.
1) One by one pop all characters from stack and put them back to
string.
d c b 0a
10 32 4
top
stack
NULL
top
a
b
d
c
top
top
top
C C++ DS JAVA TRAINING INSTITUTES IN KPHB HYDERABAD KPHB
7. Algorithm Class
Email: algorithm.class@gmail.com
Mobile: 9963930865
Website: http://sites.google.com/site/algorithmclass
G+ : http://plus.google.com/+AlgorithmClass/posts
Facebook : http://www.facebook.com/AlgorithmClassCCppDsJavaTrainingKphbHyderabad
ALGORITHM CLASSS, http://sites.google.com/site/algorithmclass
C C++ DS Data Structures CPP JAVA TRAINING INSTITUTE KPHB HYDERABAD
/