reverse of given string worlds c program -asr
#include <stdio.h>
void main(){
int i=0,len,num=0,a[10],j,f,l;
char s[100];
while(1){
s[i]=getchar();
if(s[i]=='\n'){
len=i; //get chatacters
s[i]=' '; //set last character as space
break; //break if enter pressed
}
i++;
}
for(i=0;i<len;i++){
if(s[i]==' '){
a[num]=i+1; //detect the positions where space is present
num++;
}
}
a[num]=len+1; //insert length in top index + incremented for including the space that we assigned
for(i=num;i>=0;i--){
f=a[i];
l=a[i-1];
for(j=l;j<f;j++){
printf("%c",s[j]); //prints reverse
}
}
}
void main(){
int i=0,len,num=0,a[10],j,f,l;
char s[100];
while(1){
s[i]=getchar();
if(s[i]=='\n'){
len=i; //get chatacters
s[i]=' '; //set last character as space
break; //break if enter pressed
}
i++;
}
for(i=0;i<len;i++){
if(s[i]==' '){
a[num]=i+1; //detect the positions where space is present
num++;
}
}
a[num]=len+1; //insert length in top index + incremented for including the space that we assigned
for(i=num;i>=0;i--){
f=a[i];
l=a[i-1];
for(j=l;j<f;j++){
printf("%c",s[j]); //prints reverse
}
}
}
Comments
Post a Comment