<br />
<b>Warning</b>:  Undefined array key "global_protection_id" in <b>/home/wikitechy/public_html/interview-questions/wp-content/plugins/content-protector/inc/class-ps-rest-handler.php</b> on line <b>51</b><br />
{"id":478,"date":"2021-07-13T11:18:34","date_gmt":"2021-07-13T11:18:34","guid":{"rendered":"https:\/\/www.wikitechy.com\/interview-questions\/?p=478"},"modified":"2021-09-13T07:26:17","modified_gmt":"2021-09-13T07:26:17","slug":"write-a-program-to-reverse-a-string-using-stack-data-structure","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/","title":{"rendered":"Write a program to reverse a string using stack data structure ?"},"content":{"rendered":"<div class=\"TextHeading\">\n<div class=\"hddn\">\n<h2 id=\"write-a-program-to-reverse-a-string-using-stack-data-structure\" class=\"color-pink\" style=\"text-align: justify;\">Write a program to reverse a string using stack data structure ?<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li>Given a string, reverse it using stack. For example\u00a0<a href=\"https:\/\/www.wikitechy.com\/\" target=\"_blank\" rel=\"noopener\">\u201cWikitechy\u201d<\/a>\u00a0should be converted to \u201cyhcetikiW\u201d.<\/li>\n<li>Following is simple algorithm to reverse a string using stack.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"Content\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li>Empty stack to be created.<\/li>\n<li><a href=\"https:\/\/www.wikitechy.com\/tutorials\/javascript\/creating-multiline-string-in-javascript\" target=\"_blank\" rel=\"noopener\">String<\/a>\u00a0to stack put one by one push all characters.<\/li>\n<li>From the stack and put them back to string one by one pop all\u00a0characters.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<div class=\"ImageContent\">\n<div class=\"hddn\"><img decoding=\"async\" class=\"img-responsive center-block aligncenter\" src=\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg\" alt=\" Reserve a String Using Stack\" \/><\/div>\n<div>\n<h2 id=\"sample-code-in-java\" class=\"color-purple\" style=\"text-align: justify;\">Sample Code in Java<\/h2>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-javascript code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-javascript code-embed-code\">import java.util.*; <br\/>  <br\/>\/\/stack <br\/>class Stack <br\/>{ <br\/>    int size; <br\/>    int top; <br\/>    char[] a;  <br\/>  <br\/>    \/\/function to check if stack is empty <br\/>    boolean isEmpty() <br\/>    { <br\/>        return (top &lt; 0); <br\/>    } <br\/>      <br\/>    Stack(int n) <br\/>    { <br\/>        top = -1; <br\/>        size = n; <br\/>        a = new char[size]; <br\/>    } <br\/>  <br\/>    \/\/function to push element in Stack <br\/>    boolean push(char x) <br\/>    { <br\/>        if (top &gt;= size) <br\/>        { <br\/>        System.out.println(&quot;Stack Overflow&quot;); <br\/>        return false; <br\/>        } <br\/>        else<br\/>        { <br\/>            a[++top] = x; <br\/>            return true; <br\/>        } <br\/>    } <br\/>  <br\/>    \/\/function to pop element from stack <br\/>    char pop() <br\/>    { <br\/>        if (top &lt; 0) <br\/>        { <br\/>        System.out.println(&quot;Stack Underflow&quot;); <br\/>        return 0; <br\/>        } <br\/>        else<br\/>        { <br\/>            char x = a[top--]; <br\/>            return x; <br\/>        } <br\/>    } <br\/>} <br\/>  <br\/>  <br\/>\/\/ Driver code <br\/>class Main <br\/>{ <br\/>    \/\/function to reverse the string <br\/>    public static void reverse(StringBuffer str) <br\/>    { <br\/>        \/\/ Create a stack of capacity  <br\/>        \/\/ equal to length of string <br\/>        int n = str.length(); <br\/>        Stack obj = new Stack(n); <br\/>          <br\/>        \/\/ Push all characters of string  <br\/>        \/\/ to stack <br\/>        int i; <br\/>        for (i = 0; i &lt; n; i++) <br\/>        obj.push(str.charAt(i)); <br\/>      <br\/>        \/\/ Pop all characters of string  <br\/>        \/\/ and put them back to str <br\/>        for (i = 0; i &lt; n; i++) <br\/>        {  <br\/>            char ch = obj.pop(); <br\/>            str.setCharAt(i,ch); <br\/>        } <br\/>    }  <br\/>      <br\/>    \/\/driver function <br\/>    public static void main(String args[]) <br\/>    { <br\/>        \/\/create a new string <br\/>        StringBuffer s= new StringBuffer(&quot;Wikitechy&quot;); <br\/>          <br\/>        \/\/call reverse method <br\/>        reverse(s); <br\/>          <br\/>        \/\/print the reversed string <br\/>        System.out.println(&quot;Reversed string is &quot; + s); <br\/>    } <br\/>} <\/code><\/pre> <\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"output\" class=\"color-blue\">Output<\/h2>\n<\/div>\n<\/div>\n<div class=\"CodeContent\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<figure class=\"highlight\">\n<pre><code id=\"code1\" class=\"hljs typescript\" data-lang=\"\"><span class=\"nt\">Reversed <span class=\"hljs-built_in\">string<\/span> is yhcetikiW.\r\n<\/span><\/code><\/pre>\n<\/figure>\n<\/div>\n<\/div>\n<div class=\"TextHeading\" style=\"text-align: justify;\">\n<div class=\"hddn\">\n<h2 id=\"time-complexity\" class=\"color-blue\">Time Complexity:<\/h2>\n<\/div>\n<\/div>\n<div class=\"Content\">\n<div class=\"hddn\">\n<ul>\n<li style=\"text-align: justify;\">O(n) where n is number of characters in\u00a0stack.<\/li>\n<\/ul>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Answer : Given a string, reverse it using stack&#8230;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"passster_activate_protection":false,"passster_protect_child_pages":"","passster_protection_type":"password","passster_password":"","passster_activate_overwrite_defaults":"","passster_headline":"","passster_instruction":"","passster_placeholder":"","passster_button":"","passster_id":"","passster_activate_misc_settings":"","passster_redirect_url":"","passster_hide":"no","passster_area_shortcode":"","gtb_hide_title":false,"gtb_wrap_title":false,"gtb_class_title":"","gtb_remove_headerfooter":false,"footnotes":""},"categories":[3028],"tags":[195,3230,971,491,221,368,203,199,214,198,363,3057,3228,205,222,15934,484,3054,3231,3225,196,212,3232,207,366,204,3221,3219,3224,3235,3218,3222,3226,3215,3214,3216,3227,3233,3234,3217,15925,206,972,3229,200,3055,197,3223,968,3056,3220,285,969],"class_list":["post-478","post","type-post","status-publish","format-standard","hentry","category-data-structure","tag-accenture-interview-questions-and-answers","tag-algorithm-for-palindrome-using-stack","tag-altimetrik-india-pvt-ltd-interview-questions-and-answers","tag-applied-materials-interview-questions-and-answers","tag-bharti-airtel-interview-questions-and-answers","tag-bmc-software-interview-questions-and-answers","tag-capgemini-interview-questions-and-answers","tag-casting-networks-india-pvt-limited-interview-questions-and-answers","tag-cgi-group-inc-interview-questions-and-answers","tag-chetu-interview-questions-and-answers","tag-ciena-corporation-interview-questions-and-answers","tag-collabera-te-interview-questions-and-answers","tag-data-structure-to-reverse-string-other-than-stack","tag-dell-international-services-india-pvt-ltd-interview-questions-and-answers","tag-flipkart-interview-questions-and-answers","tag-geekyants-interview-questions-and-answers","tag-genpact-interview-questions-and-answers","tag-globallogic-india-pvt-ltd-interview-questions-and-answers","tag-how-to-push-a-string-into-a-stack-in-c","tag-how-to-reverse-a-stack-in-c","tag-ibm-interview-questions-and-answers","tag-indecomm-global-services-interview-questions-and-answers","tag-java-input-string","tag-mphasis-interview-questions-and-answers","tag-netapp-interview-questions-and-answers","tag-oracle-corporation-interview-questions-and-answers","tag-rereverse-a-string-using-stack-in-cstack-string-java","tag-reverse-a-linked-list-using-stack-in-c","tag-reverse-a-list-using-stack-in-c","tag-reverse-a-number-using-stack","tag-reverse-a-number-using-stack-in-c","tag-reverse-a-string-in-java","tag-reverse-a-string-using-stack-c","tag-reverse-a-string-using-stack-in-c","tag-reverse-a-string-using-stack-in-java","tag-reverse-a-string-using-stack-in-python","tag-reverse-string-using-stack-data-structure-in-c","tag-reverse-string-using-stack-in-java","tag-reverse-words-in-a-string-using-stack","tag-reverse-words-in-a-string-using-stack-c","tag-samsung-interview-questions-and-answers","tag-sap-labs-india-pvt-ltd-interview-questions-and-answers","tag-sapient-consulting-pvt-ltd-interview-questions-and-answers","tag-stack-string-java","tag-tech-mahindra-interview-questions-and-answers","tag-tracxn-technologies-pvt-ltd-interview-questions-and-answers","tag-unitedhealth-group-interview-questions-and-answers","tag-use-a-stack-to-reverse-the-words-of-a-sentence","tag-wipro-infotech-interview-questions-and-answers","tag-wm-global-technology-services-india-pvt-ltd-limited-wmgts-interview-questions-and-answers","tag-write-a-program-to-reverse-a-string-using-stack-data-structu","tag-xoriant-solutions-pvt-ltd-interview-questions-and-answers","tag-yodlee-infotech-pvt-ltd-interview-questions-and-answers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.6 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Write a program to reverse a string using stack data structure ?<\/title>\n<meta name=\"description\" content=\"Write a program to reverse a string using stack data structure ? - Polymorphism in Java is a concept in different ways we can perform a single action\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Write a program to reverse a string using stack data structure ?\" \/>\n<meta property=\"og:description\" content=\"Write a program to reverse a string using stack data structure ? - Polymorphism in Java is a concept in different ways we can perform a single action\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/\" \/>\n<meta property=\"og:site_name\" content=\"Wikitechy\" \/>\n<meta property=\"article:published_time\" content=\"2021-07-13T11:18:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-09-13T07:26:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg\" \/>\n<meta name=\"author\" content=\"Editor\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Editor\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/\",\"name\":\"Write a program to reverse a string using stack data structure ?\",\"isPartOf\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg\",\"datePublished\":\"2021-07-13T11:18:34+00:00\",\"dateModified\":\"2021-09-13T07:26:17+00:00\",\"author\":{\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\"},\"description\":\"Write a program to reverse a string using stack data structure ? - Polymorphism in Java is a concept in different ways we can perform a single action\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/#primaryimage\",\"url\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg\",\"contentUrl\":\"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg\"},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#website\",\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/\",\"name\":\"Wikitechy\",\"description\":\"Interview Questions\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757\",\"name\":\"Editor\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g\",\"caption\":\"Editor\"},\"url\":\"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Write a program to reverse a string using stack data structure ?","description":"Write a program to reverse a string using stack data structure ? - Polymorphism in Java is a concept in different ways we can perform a single action","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/","og_locale":"en_US","og_type":"article","og_title":"Write a program to reverse a string using stack data structure ?","og_description":"Write a program to reverse a string using stack data structure ? - Polymorphism in Java is a concept in different ways we can perform a single action","og_url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/","og_site_name":"Wikitechy","article_published_time":"2021-07-13T11:18:34+00:00","article_modified_time":"2021-09-13T07:26:17+00:00","og_image":[{"url":"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg"}],"author":"Editor","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Editor","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/","url":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/","name":"Write a program to reverse a string using stack data structure ?","isPartOf":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/#primaryimage"},"image":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/#primaryimage"},"thumbnailUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg","datePublished":"2021-07-13T11:18:34+00:00","dateModified":"2021-09-13T07:26:17+00:00","author":{"@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757"},"description":"Write a program to reverse a string using stack data structure ? - Polymorphism in Java is a concept in different ways we can perform a single action","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/data-structure\/write-a-program-to-reverse-a-string-using-stack-data-structure\/#primaryimage","url":"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg","contentUrl":"https:\/\/cdn.wikitechy.com\/interview-questions\/data-structure\/reserve-a-string-using-stack.jpg"},{"@type":"WebSite","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#website","url":"https:\/\/www.wikitechy.com\/interview-questions\/","name":"Wikitechy","description":"Interview Questions","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wikitechy.com\/interview-questions\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/4d5a581fb5470d1560324bddc5e8b757","name":"Editor","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.wikitechy.com\/interview-questions\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/e9531079fe7e07841b7b156c04d65e5f39d4adfd18b6ffe3edfff8ca5aab85b5?s=96&d=mm&r=g","caption":"Editor"},"url":"https:\/\/www.wikitechy.com\/interview-questions\/author\/editor\/"}]}},"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/comments?post=478"}],"version-history":[{"count":3,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/478\/revisions"}],"predecessor-version":[{"id":3527,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/posts\/478\/revisions\/3527"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/media?parent=478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/categories?post=478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/interview-questions\/wp-json\/wp\/v2\/tags?post=478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}