String reverse without using string function: Reversing the given string programmatically.
We can reverse the string in 2 ways :
1. Using String reverse function
2. Without using string reverse function
In this program, we reverse the string without using string function.
To reverse the string first we calculate the length of the string then we store string from end character to another string.

Algorithm for reverse a string in c

  1. Declare variables. length, rev
  2. Take input string from the user. string1
  3. Calculate length of string1. length
  4. while length >=0
    rev = string1[length]
  5. Append ‘\0’ at end of rev
  6. Print reversed string. rev

Program for String reverse without using string function

#include<stdio.h>

#include<conio.h>

void main()

{

// declare variables

char str1[50],str2[50]={0};

int i,j=0,len;

// take input from user

printf ("Enter string = ");

gets(str1);

//calculate length of str1

len=strlen(str1);

//copy reverse string to str2

for(i=len-1;i>=0;i--)

{

str2[j]=str1[i];

j++;

}

str2[j]='
#include<stdio.h>

#include<conio.h>

void main()

{

// declare variables

char str1[50],str2[50]={0};

int i,j=0,len;

// take input from user

printf ("Enter string = ");

gets(str1);

//calculate length of str1

len=strlen(str1);

//copy reverse string to str2

for(i=len-1;i>=0;i--)

{

str2[j]=str1[i];

j++;

}

str2[j]='\0';

printf("\n The reverse of given string = %s",str2);

}
'; printf("\n The reverse of given string = %s",str2); }

Output:

String reverse without using string function

Write A Comment