Write a program to reverse a string.
Sample input
hello how are you
Sample output
olleh woh era uoy
code👇👇👇👇👇👇👇👇👇👇👇👇😍
#include<stdio.h>
#include<string.h>
char stack[1000];
int top=-1;
void push(char item){
top++;
stack[top]=item;
}
void main(){
int i,n,j;
char c[1000];
fgets(c,1000,stdin);
strcat(c," ");
n=strlen(c);
for(i=0;i<n;i++){
if(c[i]==' '){
for(j=top;j>=0;j--)
if(stack[j]!='\n')
printf("%c",stack[j]);
top=-1;
printf(" ");
}
else{
push(c[i]);
}
}
}
No comments:
Post a Comment