Thread: How to rearrange values in a string?

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    17

    How to rearrange values in a string?

    I've been trying to manipulate strings as of recently, but with very little success. The program I've been working on is supposed to take a predetermined string and remove the character indicated. I feel like I'm either going about this completely wrong or I'm just missing something a tad bit smaller. I'm not sure how to access the string properly so that I can copy it step by step to another array, skip over the specific character and then continue copying until it hits the null character at the end of the string. Is using subscripts to access the array a bad idea altogether?

    Well, here's my code (if you can even call it that!):

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    
    int main()
    {
      void rmchr(char s[], char a);
      
      char s = "abracadabra";          // String
      char a = "a";                    // Character
      
      printf("The string is \"%s\" with %s.\n", s, a);
      rmchr(s, a);
      printf("The string is \"%s\" without %s.\n", s, a);
      
      system("PAUSE");	
      return 0;
    }
    
    void rmchr(char s[], char a)    // Remove 'a' from the string 's'
    {
         int i = 0;                 // Counter variable for string 's'
         int j = 0;                 // Counter variable for string 'temp' 
         char temp = '\0';          // Set string 'temp' to NULL
         
         while(s[i] != '\0')
         {
            while (s[i] != a)
            {
               temp[j] = s[i];
               j++;
            }
            i++;   
         }
         strcpy(s, temp);      
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try turning on your compiler's warnings. Neither s, nor a are pointers to characters, or arrays, they're declared as single chars. Even if you fix that, unless you make them arrays (specifically s), you're still going to have problems, because you cannot modify string literals, which is what they would be, assuming you just use pointers instead of arrays. You'll need something like:
    Code:
    char s[] = "abcdefg";
    char *a = "a"; /* or just 'char a = 'a'; */
    You also cannot use strcpy on single characters in the manner you're attempting. temp is also not an array.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Actually you don't need temp to remove character. This manipulation can be done with the same string. For exsample remove 'C'
    Code:
    A B C D E
    | |  / /
    V V V V 
    A B D E
    the place for 'D' is ready, after you copy 'D' - you got place for 'E' etc
    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

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    Quote Originally Posted by vart
    Actually you don't need temp to remove character. This manipulation can be done with the same string. For exsample remove 'C'
    Code:
    A B C D E
    | |  / /
    V V V V 
    A B D E
    the place for 'D' is ready, after you copy 'D' - you got place for 'E' etc
    This is what I was hoping to illustrate but I guess it didn't work out so well.

    Anyways, thanks for the help so far! One the bright side, my lastest build can atleast compile now. The only problem is now when I try to run the program it just crashes.

    The new build:
    Code:
    /* This program is designed to take a predetermined string and remove an
     * already specified character from that string.  It will print the string
     * both before and after the character has been removed.
     */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
      void rmchr(char s[], char a);
      
      char s[] = "abracadabra";          // String
      char a = 'a';                      // Character
      
      printf("The string is \"%s\" with %s.\n", s, a);
      rmchr(s, a);
      printf("The string is \"%s\" without %s.\n", s, a);
      
      system("PAUSE");	
      return 0;
    }
    
    void rmchr(char s[], char a)    // Remove 'a' from the string 's'
    {
         int v = 0;                 // Counter variable for string 'temp' 
         int skip = 0;              // Counter for skipped spaces
         char *ptr = s + v;         // Pointer to traverse array
         char *sptr = ptr + skip;   // Pointer for skipped spaces
         
         while(*ptr != '\0')
         {
            while (*ptr != a)
               *ptr = *sptr;
               skip++;
            v += 1;   
         }  
    }

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    void rmchr(char s[], char a)    // Remove 'a' from the string 's'
    {
         int v = 0;                 // Counter variable for string 'temp' 
         int skip = 0;              // Counter for skipped spaces
         char *ptr = s + v;         // Pointer to traverse array
         char *sptr = ptr + skip;   // Pointer for skipped spaces
         
         while(*ptr != '\0')
         {
            while (*ptr != a)
               *ptr = *sptr;
               skip++;
            v += 1;   
         }  
    }
    1. you update v in the loop, but don't use the new value, so ptr and sptr are not changed
    2. instead of incrementing v you can directly increment pointers to move along the string
    3. skip indicates how much leters are skipped? then it should be incremented only when the letter is actually skipped. You increment it currently every time.
    4. your indentetion does not corresponds the actual loop structure - so you should fix or indentetion, or add brackets. Actually you should add brackets even if the loop or if/else block has only one line - it is safer for future code modifications
    5. check what will happen if the internal loop comes to the end of string
    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

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    17
    Ok, so I've improved the structure of the program so that it finally makes sense, but I'm still having a run-time error. It gets to the first printf statement, but then the window suddenly crashes.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
      void rmchr(char a[], char c);
      
      char a[] = "abracadabra";          // String
      char c = 'a';                      // Character
      
      printf("The string is \"%s\" with the character '%c'.\n", a, c);
      rmchr(a, c);
      printf("The string is \"%s\" without the character '%c'.\n", a, c);
      
      system("PAUSE");	
      return 0;
    }
    
    void rmchr(char a[], char c)    // Remove 'c' from the string 'a'
    {
         char *temp;              // Temporary array
         char *ptr = a;           // Pointer to traverse array
         char *tptr = temp;       // Pointer for the temp array
         
         while(*ptr != '\0')
         {
            if(*ptr != c)
            {
               *tptr = *ptr;
               tptr++;
            }
            ptr++;
         }
         *tptr = '\0';
         strcpy(a, temp);     
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    char *temp; // Temporary array
    it is not an array - it is just a dangling pointer - it does not point any memory you can write
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  4. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM