Thread: String swap - function

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Philippines
    Posts
    9

    String swap - function

    Hi I'm new here and I would like some help because my code produces a run time error.
    My problem is I'm going to create a function called reverse_string of type void with a char pointer as an argument called str.
    My program compiles fine. Any feedback would be helpful. Thank you

    Code:
    void reverse_string(char *str)
    {
        int ctr = 0, tctr = 0;
        char temp;
    
        while(str[tctr] != '\0'){ tctr++; }
    
        while(ctr < tctr)
        {
            temp = str[ctr]; // swap characters
            str[ctr] = str[tctr - 1]; 
            str[tctr - 1] = temp;
             ctr++;
             tctr--;
         }
    }

  2. #2
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Your algorithm is implemented correctly. The runtime error is caused by an other instruction. Maybe you are trying to reverse a string which is a constant literal? You need show the rest of the code.

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Philippines
    Posts
    9
    Hi DRK,
    Thanks for the fast reply.

    The rest of my code is only this:
    Code:
     
    #include <stdio.h>
    
    void main (void) 
    {
        char *str = "hello" ;
        reverse_string(str) ;
        printf("reversed:%s\n", str) ;
    }

  4. #4
    Registered User
    Join Date
    Sep 2014
    Posts
    364
    First, the function main returns an integer by default.
    You should recieve a warning while compiling.
    Quote Originally Posted by chris_oliver View Post
    Code:
     
    …
        char *str = "hello" ;
    …
    This is a constant string. It is hardcoded in the programm.
    Try this one:
    Code:
    #include <stdio.h>
    
    int main (void) 
    {
        char str[20];
        sprintf(str, "hello");
        reverse_string(str) ;
        printf("reversed: '%s'\n", str) ;
        return EXIT_SUCCESS;
    }
    Other have classes, we are class

  5. #5
    Registered User
    Join Date
    Apr 2015
    Location
    Philippines
    Posts
    9
    Thank you DRK and WoodSTokk!
    I have googled constant string literals and I found out that this code
    Code:
     
    char *str = "hello" ;
    is indeed a constant and it cannot be modified.

    The code that WoodSTokk suggested was indeed the solution. I have learned something new today.
    Thank you so much for the quick replies.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    to avoid such failures I prefer to define pointers to char literals as const
    Code:
    const char* str = "hello" ;
    In this case compiler will give me a compilation error as soon I try to pass such var to the function which expects char*
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  7. #7
    Registered User
    Join Date
    Apr 2015
    Location
    Philippines
    Posts
    9
    Quote Originally Posted by vart View Post
    to avoid such failures I prefer to define pointers to char literals as const
    Code:
    const char* str = "hello" ;
    In this case compiler will give me a compilation error as soon I try to pass such var to the function which expects char*
    Noted. Thank you so much

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. string swap
    By coder1 in forum C Programming
    Replies: 3
    Last Post: 10-24-2013, 03:05 PM
  2. Swap Function - How to
    By Khabz in forum C++ Programming
    Replies: 11
    Last Post: 03-15-2013, 08:48 AM
  3. swap function
    By c_lady in forum C Programming
    Replies: 4
    Last Post: 06-09-2010, 02:27 PM
  4. swap function
    By SpEkTrE in forum C Programming
    Replies: 8
    Last Post: 02-18-2005, 01:38 PM
  5. Swap Function
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 04-09-2002, 11:11 AM