Home Forums C Programming how to swap a string

Viewing 2 reply threads
  • Author
    Posts
    • #1925
      sumantkumarsuman
      Participant

      hi every one I am new to this forum, I want to know how to swap a string without using any of the library functions,
      I have a string “helloworld”, now I want to swap it to “worldhello”, without using any of the library functions,
      please help
      thank you
      with regards
      chaitanya

    • #3174
      arun_raj7
      Participant

      @ahsun
      Unless you are saving the source to the include directory and compiling from thereof, you need the brackets “<" & ">” around the include headers, not just quotes. Also, I am pretty sure he wasnt looking for a program to print two strings to output, Im pretty sure he was looking to get the string the user wanted to “swap” and then “swap” it.
      @chaitanya.b
      Im not sure quite how you mean. A program would have to use some major heuristics to be able to decipher something of that sort. Maybe you are talking about something that would do something like this:
      “hello im a string” 
      “string a im hello”

      If that is so then you can use strtok() to truncate the string to whitespace then print them in reverse order. If you wish to not use the library functions to reverse the string itself, you can write a strtok() function yourself and use it that way. Mainly, you would have to use homebrew functions to do the work for you, lets see if i can dig up some source code I may have laying around somewhere…
      Ok here are some examples of the functions, I dont remember who wrote them but I guess they work… I have never used them so dont expect anything great…
       

      #define NULL '';
      // my strcmp() function
      int mCmp(char *f, char *s)
      {
       if (f==s)
        return 0;
       return 1;
      }
      // my string length
      int mslen(char *ch)
      {
       char[] bf = ch;
       int y=0;
       do{
        y++;
       }while (bf[y]!=NULL);
       return y;
      }
      //my strtok() example
      char* mstrtok(char *first, const char *sec)
      {
       char[] buff; //first buffer
       char[] buf; //second buffer
       buff = first; // initialize first buffer
       buf[0]=buff[0]; // initialize second
       for (int x=0;x  {
        if (mCmp(buff[x],(char *)sec)
         return &buf; // if our next character equals the control character we will end and return buf
        buf[x]=buff[x];//copy next bit
       }
       //if this point should be reached then return the string
       return &buf;
      }
      // my concat example - strcat()
      char* mconcat(char *fir, char *sec)
      {
       int y=0;
       for (int x = 0;x  {
        if (fir[x]==NULL)
        {
         x--;
         for (y=0;y     x++;fir[x] = sec[y];
         return fir;
        }
       }
      }
    • #3175
      niklaesh
      Participant

      Bloodrager he didnt mention that he wanted to get input from the user. I made the programme according to the cituation he placed in the post.
      Any how thanks for correcting me about “<" ">“, actualy that is mys style of coding. I will try to adopt the changes.
      Thanks

Viewing 2 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.