Program to reverse a string without using the reverse function

Problem statement:- Program to reverse a string without using the reverse function.

Data requirement:-

   Input Data:- str

  Output Data:-str

  Additional Data:- i, len

Program in C

Here is the source code of the C Program to reverse a string without using the reverse function.

Code:

#include<stdio.h>
#include<string.h>
int main()
{
    char str[30];
    printf("Enter your String:");
    scanf("%[^\n]",str);
    int i=0;
    printf("After Reversing String is :");
    for(i=strlen(str)-1;i>=0;i--)
        printf("%c",str[i]);
}

Input/Output:
Enter your String:c string program
After Reversing String is :margorp gnirts c

Program in C++

Here is the source code of the C++ Program to reverse a string without using the reverse function.

Code:

#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    string str;
    cout<<"Enter your String:";
    getline(cin,str);
    int i=0;
    int len=0;
    while(str[len]!='\0')
    {
        len++;
    }
    cout<<"After Reversing String is :";
    for(i=len-1;i>=0;i--)
        cout<<str[i];
}

Input/Output:
Enter your String:csinfo360.com
After Reversing String is :moc.063ofnisc

Program in Java

Here is the source code of the Java Program to reverse a string without using the reverse function.

Code:

import java.util.Scanner;
public class p14 {

public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
String str1,str2=" ";
System.out.println("Enter your String:");
str1=cs.nextLine();
for(int i=str1.length()-1;i>=0;i--)
{
str2+=str1.charAt(i);
}
System.out.println("After Reversing String is :"+str2);
cs.close();
}
}

Input/Output:
Enter your String:
string reverse
After Reversing String is : esrever gnirts

Program in Python

Here is the source code of the Python Program to reverse a string without using the reverse function.

Code:

str=input("Enter the String:")
print("After Reversing String is :")
for i in range(len(str)-1,-1,-1):
    print(str[i],end="")

Input/Output:
Enter the String:reverse string in python
After Reversing String is :
nohtyp ni gnirts esrever

Post a Comment

0 Comments