banner



Print String In Reverse C++

Reverse a cord in C

Today in this article we volition learn how to reverse a string in C language. There are various method top reverse a sting that we will be discussing in this page.

Reversing a string is a technique so that the 1st character becomes the last character and then on.

Lets understand this with the help of an case:-

  • Input sting:- RITIKA
  • Output sting:- AKITIR

reverse a string

Algorithm

  • Footstep 1. Have the cord which you lot have to reverse equally the input variable says str.
  • Step 2:- Calculate the length of the string. The bodily length of the cord is one less than the number of characters in the cord. Let actual length exist len.
  • Step 3:- Use a for loop to iterate the cord using a temporary variable to swap the elements.
  • Step four:- Impress the reversed cord.

C-Program-to-Reverse-A-String

C Code :-

Method one

Run

              #include <stdio.h>  #include <string.h>                            // part definition of the revstr()              void revstr(char *str1)  {                              // declare variable                            int i, len, temp;    len =              strlen(str1); // use strlen() to get the length of str string                              // employ for loop to iterate the cord                            for              (i = 0; i < len/2; i++)    {                              // temp variable use to temporary concord the string                            temp = str1[i];     str1[i] = str1[len - i - ane];     str1[len - i - one] = temp;    }   }   int main()  {              char              str[l]="priyanka";              // size of char cord              pri              printf              (" \north Earlier reversing the string: %s \n", str);                              // call revstr() part                            revstr(str);              printf              (" Subsequently reversing the string: %s", str);   }

Output:-

Before reversing the cord: priyanka  After reversing the cord: aknayirp

Method 2

We tin also do the same by using build in functions.

  • similar strrev() in C programming language
  • strrev() is a built in role in C which is comes under cord.h header file
  • this part returns the cord after reversing it.

Run

                              #include <stdio.h>  #include <cord.h>                            int main()  {                              // declare the size of character string                            char              str[100] = "Ritika";              // use strrev() function to reverse a cord              printf ("After the reverse of a string: %s ", strrev(str));              return              0; }

Output:-

Enter a string to be reversed: Ritika Afterward the reverse of a string: akitiR

Method 3

We can also reverse the string by storing the reverse in another array and and so impress.

  • The idea is to transfer the string to another cord in a opposite way let the another array is rev.
  • All we need is the size of the string.
  • The method is to initialize a character array of the equal size and start copying the elements of the input string from the end.
  • And display the rev

Run

#include <stdio.h>  #include <string.h>  int primary() {     char str[1000] = "Priya", rev[thou];     int i, j, count = 0;     printf("String Before Reverse: %south", str);     // finding the length of the string     while (str[count] != '\0') {         count++;     }     j = count - 1;     // reversing the cord by swapping     for (i = 0; i < count; i++) {         rev[i] = str[j];         j--;     }     printf("String After Opposite: %southward", rev);     render 0; }

Output:-

String Before Contrary: priya Cord Later Contrary: ayirp

Method 4

  • This includes apply of 2 pointers one at the starting and ane at the end.
  • the characters are then reversed one by one
  • The process of reversing the string is done with the help of ii pointers.

Run

#include <stdio.h>  #include <cord.h>              // function declaration str_len that                            int              str_len(char *ptr)  {    int i = 0;    while ( *(ptr + i) != '\0')    i++;    return i;  }              void              revstr(char *st)  {              int              len, i;              char              *start, *terminate, temp;    len = str_len (st);    get-go = st;    end = st;              for              (i = 0; i < len - one; i++)    end++;              for              (i = 0; i < len/2; i++)    {      temp = *end;      *end = *start;      *offset = temp;       start++;      finish--;     }   }              int              main()  {              char              st[l]="priyanka";              revstr(st);    printf (" The reverse cord is: %s", st);              render              0;  }

Output:-

 Enter a string to be reversed: priyanka The reverse string is: aknayirp

Print String In Reverse C++,

Source: https://prepinsta.com/c-program/to-print-the-string-in-reverse-order/

Posted by: richardsonnounkilthe.blogspot.com

0 Response to "Print String In Reverse C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel