Write a program to reverse a string. - Data Structure using C

Translate

Thursday, December 10, 2020

Write a program to reverse a string.

 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

Introduction to Arrays

  Introduction to Arrays An array is a data structure that allows you to store a collection of elements of the same type. Each element in th...