Program Discussion :: Basics
64 / 279
Write a program to reverse a string using minimum control variable and minimum iteration and with and without temporary variable.
Answer:
#include
#include
#include
void main()
{
clrscr();
char str[100], temp;
int i=0, j;
cout
Asked In ::
Language:
Rajsekaran
20 Jun, 2017 11:23 PM
<!DOCTYPE html>
<html>
<body>
<?php
function Str_rev_withouttemp() {
$str ="what is your name"; /* input string */
echo "Input string is :" . $str."<br/>";
$name = strrev ($str);
/* printing string in reverse order */
echo " reverse string without temp variable is:-- \t ".$name."<br/>";
}
Str_rev_withouttemp();
function Str_rev_withtemp() {
$str ="what is your name"; /* input string */
echo "Input string is :" . $str."<br/>";
$arr = str_split($str); /* conversion of string into array */
$arrlength=count ($arr); /* counting lengh of array */
$arrc=array(); /* initialising an empty array and its initial index */
for( $x= $arrlength,$j=0 ; $x>=0; $x--,$j++) /* loop for reading the input array */
{
$arrc[$j]=$arr[ $x]; /* reversing the array */
}
$name= implode ("",$arrc); /* conversion of array into string */
echo " reverse string with temp variable is :--\t".$name."<br/>"; // printing string in reverse order
}
Str_rev_withtemp();
?>
</body>
</html>
Language:
Sharan
7 Jul, 2017 9:30 AM
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char str[100], temp;
int i=0, j;
printf("Enter the String : ");
gets(str);
i=0;
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
printf("Reverse of the String = %s",str);
getch();
}
Language:
Nick
7 Jul, 2017 9:30 AM
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
char str[100], temp;
int i=0, j;
cout<<"Enter the String : ";
gets(str);
i=0;
j=strlen(str)-1;
while(i<j)
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
i++;
j--;
}
cout<<"Reverse of the String = "<<str;
getch();
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.*;
public class Reverse
{
public static void main(String []args)
{
String rev="";
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string: ");
String str=sc.nextLine();
int len=str.length();
for(int i=len-1;i>=0;i--)
{
char ch=str.charAt(i);
rev=rev+ch;
}
System.out.println("Reverse string is: "+rev);
}
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.*;
public class Reverse
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a string: ");
String str=sc.nextLine();
StringBuffer sb=new StringBuffer(str);
System.out.println("Reverse of string: "+sb.reverse());
}
}