duplicating and reversing a string?

This is a discussion on duplicating and reversing a string? within the C Programming forums, part of the General Programming Boards category; Hello, I'm quite new to c programming and I'm trying to solve this exercise Write a function that reverses a ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    6

    duplicating and reversing a string?

    Hello,
    I'm quite new to c programming and I'm trying to solve this exercise

    Write a function that reverses a string e.g world to dlrow
    .
    Use this prototyp
    /* Returns a reversed copy of s. */
    Code:
    char *reverse(char *s);
    The function should reverse a copy and not the original string. Use strdup to to copy the given string.
    Use strlen to determine the length of the string
    I am able to use strlen and also strdup separately but I have no clue how I should combine the two commands to help me with the task. I would be really grateful for any help which could point me into the right direction.

    What I have so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *reverse (char *s);
    
    int main(int argc, char* argv[])
    {
    	char word[999] = {0};
    	int result = 0;
    
    	printf("Please enter a word:");
        scanf("%s", &word);
    	
    	reverse (&word);
    	
    	return 0;
    }
    
    char *reverse (char *s)
    {
    	int i = 0;
    	int length = 0;
    	char *copy;
    
    	copy = NULL;
    	length = strlen (s);
    
    	copy = strdup(s);
    	printf ("%s\n", copy);   /*test*/
    }

  2. #2
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    - replace "&word" with "word"
    - goto the end of string then loop to begin and collect any char in a temp-variable, then copy temp to your string

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Quote Originally Posted by BillyTKid View Post
    - replace "&word" with "word"
    - goto the end of string then loop to begin and collect any char in a temp-variable, then copy temp to your string
    Thanks I deleted the &.
    I think my major problem ist that I don't know exactly how I should go to the end (or beginning) of the string when I only use a pointer.

  4. #4
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    You go to the end of string with
    Code:
    char *s = "blah";
    char *end = s+strlen(s);
    end points to terminating char '\0' from s.
    For non-empty strings you can loop to begin like:
    Code:
    while( end-- != s )
      putchar( *end );

  5. #5
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Quote Originally Posted by BillyTKid View Post
    You go to the end of string with
    Code:
    char *s = "blah";
    char *end = s+strlen(s);
    This pointer stuff is quite overwhelming and confusing
    So according with what I read s contains the address of the first letter
    So let's say the word is "apple" -->*s = a --> s = (address number)

    length of apple = 5
    i.e s + strlen(s) = (address number + 5) that would contain 0 (because end of string)

    But what I really don't understand is why does *end contain 0 now? Don't we pass on the address to *end. How does it know that we want the content of the address saved?
    I hope my explanations are not too confusing . I'm really lost right now.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    If end points to the null character, then *end yields the value of the null character.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Quote Originally Posted by laserlight View Post
    If end points to the null character, then *end yields the value of the null character.
    I guess I have to accept it although I'm not sure I really understand it

    So I tried this but now it's crashing
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *reverse (char *s);
    
    int main(int argc, char* argv[])
    {
    	char word[999] = {0};
    	
    	printf("Please enter a word:");
                    scanf("%s", &word);
    	
    	reverse (word);
    	
    	return 0;
    }
    
    char *reverse (char *s)
    {
    	int i = 0;
    	int length = 0;
    	char temp = 0;
    	char *end = s + strlen(s) - 1; 
    	char *copy;
    	copy = NULL;
    	length = strlen(s);
    
    	for (i; i < length; i++) 
    	{
    		temp = *end;                 /*copies the content of end into temp*/
    		copy = strdup(temp);     /*duplicates temp*/
    		copy++;                        /*adds one to address*/
    		end--;                       
    	}
    	
    }
    The crash comes when attempting copy = strdup(temp);. It reads
    Unhandled exception at 0x0043110c in Master.exe: 0xC0000005: Access violation reading location 0x00000066.
    Obviously I'm doing something wrong. Any hints before I go slightly mad?

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    You should be using strdup once, before you start the reversing loop.

    Oh, and you're back using &word for scanf when you should not be taking the address.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Quote Originally Posted by laserlight View Post
    You should be using strdup once, before you start the reversing loop.

    Oh, and you're back using &word for scanf when you should not be taking the address.
    deleted the &.

    ok so my temp variable needs to be an array? Which makes me wonder what the use of strdup should achieve if I can simple turn it and save it in another array?

    Thanks for your patient help. I really appreciate it .
    Last edited by Serenity; 10-26-2010 at 10:57 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,448
    Quote Originally Posted by Serenity
    ok so my temp variable needs to be an array?
    No. I tell you what: write a reverse_inplace function that reverses the string in-place, i.e., you do not call strdup but rather reverse the string through the pointer to the first character of the string that is passed as an argument. Then, write your reverse function by using strdup with reverse_inplace. Maybe this breaking down of the problem will help you arrive at a correct solution.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Aug 2010
    Posts
    231
    Your code is stuff, not my. Its too difficult for you to read 3 short lines of code?
    strdup is not ANSI C.
    Code:
    void printreverse (char *s)
    {
      char *end = s+strlen(s);
      while( end-- != s )
        putchar( *end );
    }
    and now you
    Code:
    char * reverse (char *s)
    {
      char *end = s+strlen(s);
      ???
      while( end-- != s )
      {
        ???
      }    
    }
    Last edited by BillyTKid; 10-26-2010 at 12:12 PM.

  12. #12
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Turn up your compiler warning levels and fix the warnings.
    You're doing silly things like decaring a function to return something and then not returning anything. The compiler will emphatically complain that this is a problem (which it certainly is), if you let it!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  13. #13
    Registered User
    Join Date
    Jul 2007
    Posts
    131
    Quote Originally Posted by BillyTKid View Post
    strdup is not ANSI C.
    Thanks for this. I've been for long in belief that it is even though I've went through the standard many times. Luckily POSIX is okay for me mostly.

  14. #14
    Registered User
    Join Date
    Oct 2010
    Posts
    6
    Quote Originally Posted by BillyTKid View Post
    Your code is stuff, not my. Its too difficult for you to read 3 short lines of code?
    strdup is not ANSI C.
    I'm sorry I have upset you I didn't mean too. I never said that your code isn't good. I believe it is. It is just very hard for me to implement it because this is by far the hardes exercise we had until now.

    Turn up your compiler warning levels and fix the warnings.
    You're doing silly things like decaring a function to return something and then not returning anything. The compiler will emphatically complain that this is a problem (which it certainly is), if you let it!
    I know it's not giving anything back yet. I'm trying to get the reversing right before. I know it's complaining about it and I will fix it later but unfortunately that's not the reason why it's not working


    I guess I will give up for now . Thanks for all your help and I'm sorry if I upset anyone.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21