Loading...

Programming Question Reverse String without using String function

View: 992    Dowload: 0   Comment: 0   Post by: hanhga   Category: Javascript   Fields: Other

Write a program to reverse string without using string function. You don’t have to use any in-built string library function. This problem can be solved by multiple approaches. Let’s check it.

Method 1.

Reverse String using Stack

In my previous post i have explained what is stack and how to implement stack using array.

Using the same concept let’s create a stack and push all the characters of a string. Once all the characters are pushed then pop the character and print it. This operation will print the string in reverse order .  Check program to reverse a string using stack.

#include<stdio.h>
#include<string.h>
 
#define max 100
int top,stack[max];
 
void push(char x){
      /* Insert character into the stack.*/
      if(top==max-1){
          printf("stack overflow");
      }  else {
          stack[++top]=x;
      }  
 
}
 
void pop(){
 
    /* Removing an element from the stack. */
    printf("%c",stack[top--]);
}
 
main()
{
  /* Define string value. */
   char str[]="Programming";
 
   int len = strlen(str),i;
 
   /* Push the characters of a string. */
   for(i=0;i<len;i++)
        push(str[i]);
 
   for(i=0;i<len;i++)    
      pop();        
}

Method 2.

Reverse String by Swapping their Position

In this approach we reverse a  string by swapơping their position. For this let’s take two indexes (first and last) .

a) Initialize first index with 0 and last index with arraylength-1

 first = 0;
      last = strlen(str)-1;

b) In next step swap the position of character occur in first and last index. Repeat this process until first index is less than or equal to last index.

Reverse String Program in C

C program code to reverse string without using string function.

#include <stdio.h>
#include <string.h>
main()
{
   char str[]="reverse";
 
   int first = 0,temp;
   int last = strlen(str)-1;
 
   /* Swap the position of an element. */
 
   while (first <= last){
 
       temp = str[first];
       str[first] = str[last];
       str[last] = temp;
 
       /* Increment first index and decrement last index. */
 
       first++;
       last--;
   }
 
   printf("Reverse of a string is %s",str);
}

Reverse String in PHP

We have seen the c code to reverse a string, now let’s write the same code in PHP.

<?php
$str = "learning PHP";
 
//Initialize first and last indexes
 
$first=0;
$last = strlen($str)-1;
 
   /* Swap the position of an element */
 
   while ($first <= $last){
 
       $temp = $str[$first];
       $str[$first] = $str[$last];
       $str[$last] = $temp;
 
       $first++;
       $last--;
   }
 
echo "Reverse string is ". $str;
 
?>

 

Programming Question Reverse String without using String function

Write a program to reverse string without using string function. You don’t have to use any in-built string library function. This problem can be solved by multiple approaches. Let’s check it.

Posted on 01-10-2016 

Comment:

To comment you must be logged in members.

Files with category

  • Mini Youtube Using ReactJS

    Mini Youtube Using ReactJS

    View: 25    Download: 2   Comment: 0

    Category: Javascript     Fields: none

    This is one the best starter for ReactJS. MiniYoutube as the name suggests is a youtube like website developed using reactJS and youtube API. This project actually let's you search , play and list youtube videos. Do check it out and start learning...

  • Angular 6 Starter with Laravel 5.6 API Service

    Angular 6 Starter with Laravel 5.6 API Service

    View: 62    Download: 0   Comment: 0

    Category: Javascript     Fields: none

    Angular 6 and Laravel 5.6 This project is a starter for creating interface with Angular using bootstrap && css && sass and using Laravel 5.6 for api requests. Demo Installation This project is divided in two parts (projects) and before use them you...

  • Simple Richtext Editor Based on pellJS

    Simple Richtext Editor Based on pellJS

    View: 26    Download: 0   Comment: 0

    Category: Javascript     Fields: none

    A simple visual editor for websites using the pell javascipt. It also has the option to switch between visual editor mode and source code mode. I will upload an update for new functionality soon. Source Code Editor Visual Editor

  • Data Visualization for BI: How to Design Layouts for .NET Financial Reports

    Data Visualization for BI: How to Design Layouts for .NET Financial Reports

    View: 37    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    With the Active Reports Server, you can have a multi-tenant environment where users from various departments, companies, or other specifications can log in, view their reports (and only their reports), export the data, or set up a distribution...

  • AngularJS and REST API

    AngularJS and REST API

    View: 204    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    This is a tutorial for those interested in a quick introduction to AngularJS and REST API. We will build the familiar Periodic Table of the Elements found in every chemistry textbook, and allow the user to select a Chemical Element by clicking on...

  • Collective Intelligence, Recommending Items Based on Similar Users' Taste

    Collective Intelligence, Recommending Items Based on Similar Users' Taste

    View: 168    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Using Collaborative Filtering to find people who share tastes, and for making automatic recommendations based on things that other people like.

  • Think Like a Bird for Better Parallel Programming

    Think Like a Bird for Better Parallel Programming

    View: 157    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Coding an application to run in parallel is hard, right? I mean, it must be hard or we’d see parallel programs everywhere. All we'd see are slick parallel apps that use every available core effortlessly. Instead multi-threaded apps are the exception...

  • Getting Started with the Bing Search APIs

    Getting Started with the Bing Search APIs

    View: 169    Download: 0   Comment: 0

    Category: Javascript     Fields: Other

    Bing Search API is a set of REST interfaces that find web pages, news, images, videos, entities, related searches, spelling corrections, and more in response to queries from any programming language that can generate a web request. Applications that...

 
File suggestion for you
Loading...
File top downloads
Loading...
Loading...
Codetitle - library source code to share, download the file to the community
Copyright © 2018. All rights reserved. codetitle Develope by Vinagon .Ltd