How to reverse a string using array?

Showing Answers 1 - 35 of 35 Answers

deepam chakraborty

  • Sep 10th, 2006
 

after inserting an string in an array place a counter to find the length of array, then start with for loop. function shown below

for (i=n-1;i>=o;i--)

{

  printf("%c",a[i]);

}

/*a[ ] is the string variable*/

/*n is the size of thr string*/  

  Was this answer useful?  Yes

santhosh kumar r

  • Oct 7th, 2006
 

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{

  char name[25],rev[25];

  int i,l;

  printf("enter the name");

 gets(name);

  l=strlen(name);

  for(i=0;i<l;i++)

     {

         name[i]=rev[l-1-i];

          i++;

      }

getch();

} 

 

 

 

vishal

  • Nov 30th, 2006
 

/*without using standard functions*/

#include<stdio.h>

#include<string.h>

#include <conio.h>

main()

{

  char s[40],c,r[40];

  int i,j,p;

  clrscr();

  printf("Enter the string:");

  gets(s);

  for(i=0;s[i]!='';i++);

             j=i;

  printf("The original string is:%s",s);

  printf("nThe reverse string is:");

  for(p=j-1,i=0;p>=0;p--,i++)

 r[i]=s[p];

  r[i]='';

  printf("The reverse string is:%s",r);

  getch();

 }

  Was this answer useful?  Yes

R.S.Jayasathyanarayanan

  • Dec 16th, 2006
 

#include#include#includevoid main(){ char name[25],rev[25]; int i,l; printf("enter the name"); gets(name); l=strlen(name)-1; for(i=l,j=0;i>=0l;i--,j++) { name[i]=rev[j]; }getch();}

  Was this answer useful?  Yes

jintojos

  • Jun 16th, 2008
 

#include #include char fun(); int i; char str[10]; void main() { clrscr(); printf("Enter The String :"); fun(); str[i]=''; printf("nThe Revers Is :%s",str); getch(); } char fun() { char ch; if((ch=getchar())!='n') fun(); if(ch!='n') str[i++]=ch; }

  Was this answer useful?  Yes

ya this is as simple as,

void Reverse(char a[])
{
     register int i,j;  //becoz i and j is used frequently
     char temp;
    
     for(i=0,j=strlen()-1;i<j;i++,j--)
         temp = a[i], a[i] = a[j], a[j] = temp;
}


freakGrl

  • Jul 17th, 2011
 

Reverse a string using array.............plz comment on this

Code
  1. # include <iostream.h>

  2. # include <string.h>

  3.  

  4. void reverseit(char array[],int no)

  5. { char c; int len,x, mid=0;

  6.         if(no==0)

  7.         {for(len=0,x=0; array[x] !='x0';len++,x++); }

  8.         else

  9.         { len = 0; len = no; }

  10.  

  11.         mid = (len-1)/2;

  12.  

  13.         for(int i=0,j=len-1; i <=mid; i++,j--)

  14.         { c = array[i];

  15.           array[i] = array[j];

  16.           array[j] = c;

  17.         }

  18.         cout << endl<<array;

  19. }

  20.  

  21. void main()

  22. { char data[80]; int nos=0;

  23.         cout <<"enter string to reverse it  "; cin.getline(data,80);

  24.         reverseit(data,0);

  25.         cout <<endl<<"enter string to reverse it  "; cin.getline(data,80);

  26.         cout <<endl<<"enter Nos to reversed "; cin >>nos;

  27.         reverseit(data,nos);

  28. }

  29.  

  Was this answer useful?  Yes

chita ranjan patra

  • Aug 3rd, 2011
 

I am giving only the logic,using the pointer
implement this logic as your requirement
it will be effective

Code
  1. reverse (char *p) {

  2. char c, *p1, *p2;

  3. if (strlen(p) <= 1) return;

  4.  

  5. for (p1=p, p2=p+strlen(p)-1; p1<p2; c=*p1, *p1=*p2, *p2=c, p1++, p2--);

  6.  

  7. }

  8.  

  Was this answer useful?  Yes

Code
  1. #include<stdio.h>

  2. #include<string.h>

  3. #include<conio.h>

  4. main()

  5. {

  6. int i;

  7. char str[100];

  8. clrscr();

  9. printf("enter string:");

  10. scanf("%s",str);

  11. for(i=strlen(str)-1;i>=0;i++)

  12. printf("%c",str[i]);

  13. getch();

  14. }

  Was this answer useful?  Yes

Simple code ...

Code
  1. #include<stdio.h>

  2. #include<string.h>

  3. #include<conio.h>

  4. main()

  5. {

  6. int i;

  7. char str[100];

  8.  

  9. printf("enter string:");

  10. gets(str);

  11. for(i=strlen(str)-1;i>=0;i--)

  12. printf("%c",str[i]);

  13.  

  14. }

  Was this answer useful?  Yes

moumita

  • Mar 2nd, 2015
 

no, it is not at all helpful. Sumita Arora se chapke jyada bhao mat kha

  Was this answer useful?  Yes

Code
  1. #include<stdio.h>

  2. #include<string.h>

  3.  

  4. int main() {

  5.    char str[100], temp;

  6.    int i, j = 0;

  7.  

  8.    printf("

  9. Enter the string :");

  10.    gets(str);

  11.  

  12.    i = 0;

  13.    j = strlen(str) - 1;

  14.  

  15.    while (i < j) {

  16.       temp = str[i];

  17.       str[i] = str[j];

  18.       str[j] = temp;

  19.       i++;

  20.       j--;

  21.    }

  22.  

  23.    printf("

  24. Reverse string is :%s", str);

  25.    return (0);

  26. }

  Was this answer useful?  Yes

shiwang

  • Dec 4th, 2016
 

Code
  1. #include <math.h>

  2. #include <stdio.h>

  3. #include <string.h>

  4. #include <stdlib.h>

  5. #include <assert.h>

  6. #include <limits.h>

  7. #include <stdbool.h>

  8.  

  9. int main(){

  10.     int n,x;

  11.     scanf("%d",&n);

  12.     int *arr = malloc(sizeof(int) * n);

  13.     for(int arr_i = 0; arr_i < n; arr_i++){

  14.         scanf("

  15. %d",&arr[arr_i]);}

  16.     for(int arr_i = 0; arr_i < n; arr_i++)

  17.        {

  18.     x=strlen(arr);

  19.     printf("%d",arr[(x-1)-arr_i]);

  20.     }

  21.     return 0;

  22. }

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions