Thread: Reverse A String

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    1

    Reverse A String

    I Want Programe Of Reverse A String

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    Use standard function strrev defined in string.h
    Code:
    Prototype
    char*strrev(char*)
    Use it like
    Code:
    char orignalstring[]="Happy",revstring[50];
    revstring=strrev(orignalstring);
    [/code]

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Quote Originally Posted by vaibhav
    Use standard function strrev defined in string.h
    Code:
    Prototype
    char*strrev(char*)
    Use it like
    Code:
    char orignalstring[]="Happy",revstring[50];
    revstring=strrev(orignalstring);
    [/code]
    Nope, it is not a standard function.

    @the OP, do a board search on 'reverse a string' - this comes up often.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Not to mention
    http://cboard.cprogramming.com/annou...ouncementid=39

    And not to mention how Writing In Initial Caps Really Sucks
    http://www.catb.org/~esr/faqs/smart-questions.html

    > I Want Programe Of Reverse A String
    Did Yoda translate that into English for you?
    Program reverse a string not will I give you - mmmmm

  5. #5
    That was cruel...
    It's a good thing I came accross this thread, I was wanting to learn this. Once again, you guys have been very useful and I've just found this site.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Stick around for a couple years answering these questions and find out that the advice given was not harsh. Or if you are a masochist, post a question on comp.lang.c.

    Read the guidelines: show an attempt and there are those with plenty of experience that will correct all of the flaws, well beyond things you will even imagine for some time. Ignore the rules, and you will be treated like a freeloader. Quid pro quo.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    That was...interesting...?

  8. #8
    Registered User
    Join Date
    Dec 2005
    Posts
    13
    hey try this .. im also new here but i think the porb is small so i can give u my prog toreverse a strng,,
    Code:
    #include<stdio.h>
    char rev[20];
    void reverse(char *x)
    {
    	int i=0,j=0;
    	while(x[i]!='\0')
    		i++;
    	i--;
    	while(i>=0)
    	{
    		rev[j]=x[i];
    		j++;
    		i--;
    	}
    	rev[j]='\0';
    }
    void main()
    {
    	char str[20];
    	printf("enter the strning to be reversed : ");
    	scanf("%s",str);
    	reverse(str);
    	printf("\n\nreversed sring is : %s",rev);
    }
    any comments welcome.....

  9. #9
    !anExpert
    Join Date
    Mar 2005
    Location
    pa
    Posts
    155
    wow..

    somtimes people can just amaze you.. over and over

  10. #10
    Registered User
    Join Date
    Dec 2005
    Posts
    13
    what was that????????

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you've never heard of a swap function?

    in this case it would almost literally cut your time in half:
    Code:
    #include <iostream>
    
    int main()
    {
    	char swap;
    	char line[]="RaceCar";
    	short int i=0;
    	short int x=6;
    
    	for(;i<x;i++,x--)
    	{
    		swap=line[i];
    		line[i]=line[x];
    		line[x]=swap;
    	}
    
    	std::cout<<line<<std::endl;
    	return 0;
    }
    and yes, I know it's C++ on a C board, but all you need to pay attention to is that for loop
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > any comments welcome.....
    You used void main - main always returns an int - see previous posts and the FAQ
    You used a global variable
    You limited the length to only 20 characters
    You didn't check for input overflows

  13. #13
    Registered User
    Join Date
    Jan 2006
    Posts
    1
    Quote Originally Posted by =viki=
    hey try this .. im also new here but i think the porb is small so i can give u my prog toreverse a strng,,
    Code:
    #include<stdio.h>
    char rev[20];
    void reverse(char *x)
    {
    	int i=0,j=0;
    	while(x[i]!='\0')
    		i++;
    	i--;
    	while(i>=0)
    	{
    		rev[j]=x[i];
    		j++;
    		i--;
    	}
    	rev[j]='\0';
    }
    void main()
    {
    	char str[20];
    	printf("enter the strning to be reversed : ");
    	scanf("%s",str);
    	reverse(str);
    	printf("\n\nreversed sring is : %s",rev);
    }
    any comments welcome.....
    there may some error above.
    Code:
    void reverse(char *x)
    {
    	int i=0,j=0,z=0;
    	while(x[i]!='\0')
    		i++;
    	rev[i--]='\0';
    	z=i/2;	//or z=i>>1;
    	for(;z;z--)
    	{
    		rev[j]=x[i];
    		j++;
    		i--;
    	}
    }
    Last edited by tqh831213; 01-03-2006 at 10:35 AM.

  14. #14
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    Here are some better swap functions...

    tongue in cheek

    Code:
    void swap(int &x, int &y)
    {
        x -= y;
        y += x;         // y gets the original value of x
        x = (y - x);    // x gets the original value of y
    }
    
    void swap2(int &x, int &y)
    {
    	x ^= y ^= x ^= y;
    }

  15. #15
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by treenef
    Here are some better swap functions...

    Thanks, I needed a chuckle.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. ........ed off at functions
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-29-2002, 09:37 PM