Hot File

Programming Question Reverse String without using String function

View: 312    Dowload: 0   Comment: 0   Post by: hanhga  
Author: none   Category: Javascript   Fields: Other

0 point/0 review File has been tested

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.

Introduction

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

Programming Question Reverse String without using String function Posted on 01-10-2016  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. 5/10 312

Comment:

To comment you must be logged in members.

Files with category

  • JUnit 5 State Of The Union using java

    View: 367    Download: 0   Comment: 0   Author: none  

    JUnit 5 State Of The Union using java

    Category: Javascript
    Fields: Other

    4.5/1 review
    JUnit 5 has been under development for about 14 months now and the prototype is almost a year old. Time to summarize what happened so far, where the project stands, and where it’s going.

  • Getting Started with Dropwizard using java

    View: 362    Download: 0   Comment: 0   Author: none  

    Getting Started with Dropwizard using java

    Category: Javascript
    Fields: Other

    4.5/1 review
    Dropwizard is a framework for building RESTful web services in Java. In this tutorial we’re going to have a look at how to get started with developing a Dropwizard application by building a new service from scratch.

  • Build Query NULL Value in MySql

    View: 190    Download: 0   Comment: 0   Author: none  

    Build Query NULL Value in MySql

    Category: Javascript
    Fields: Other

    2.5/2 review
    Misunderstanding NULL is common mistake beginners do while writing MySql query. While quering in MySql they compare column name with NULL. In MySql NULL is nothing or in simple word it isUnknown Value so if you use comparison operator for NULL values...

  • Manage Your JavaScript Application State with MobX

    View: 194    Download: 0   Comment: 0   Author: none  

    Manage Your JavaScript Application State with MobX

    Category: Javascript
    Fields: Other

    2.25/2 review
    This article was peer reviewed by Michel Weststrate and Aaron Boyer. Thanks to all of SitePoint’s peer reviewers for making SitePoint content the best it can be!

  • Build Bringing Pages to Life with the Web Animations API

    View: 204    Download: 0   Comment: 0   Author: none  

    Build Bringing Pages to Life with the Web Animations API

    Category: Javascript
    Fields: Other

    4.5/2 review
    This article is by guest author Dudley Storey. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • How to Style Google Custom Search Manually

    View: 179    Download: 0   Comment: 0   Author: none  

    How to Style Google Custom Search Manually

    Category: Javascript
    Fields: Other

    0/0 review
    Website owners very often decide on using Google’s Custom Search Engine (GCSE) for searching through their content instead of using built-in and/or custom search functionality. The reason is simple – it’s much less work, and most often it does the...

  • Test React Components Using Jest

    View: 4795    Download: 0   Comment: 0   Author: none  

    Test React Components Using Jest

    Category: Javascript
    Fields: Other

    4.5/1 review
    This article is by guest author Jack Franklin. SitePoint guest posts aim to bring you engaging content from prominent writers and speakers of the JavaScript community.

  • Programming Question Reverse String without using String function

    View: 312    Download: 0   Comment: 0   Author: none  

    Programming Question Reverse String without using String function

    Category: Javascript
    Fields: Other

    0/0 review
    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.

 
Newsletter Email

File suggestion for you

File top downloads

logo codetitle
Codetitle.com - library source code to share, download the file to the community
Copyright © 2015. All rights reserved. codetitle.com Develope by Vinagon .Ltd