Thread: removing spaces from a string in an array of strings

  1. #1
    Registered User
    Join Date
    Apr 2018
    Posts
    43

    removing spaces from a string in an array of strings

    I am working on a project that reads input from a file ,stores them into an array and remove the spaces in each string array.
    I am doing this so that I can check if the string that were read in the input file are correct. Then I will check if the array "cardarray" contains all the cards from stringcards. But I haven't gotten to that yet. Do you also have any tips how I could do that?
    This what I have come up with but I keep getting an error that I am smashing the stack.



    Code:
    int main (int argc, char **argv) {
    
    const char * stringcard[] = { "REDA","RED2"
    "RED3"
    "RED4"
    "RED5"
    "RED6"
    "RED7"
    "RED8"
    "RED9"
    "RED10"
    "REDJ"
    "REDQ"
    "REDK"
    };
    
    char *reds[max];
    char * cardarray[max];
    
    int i;
    
        FILE *file = argc > 1 ? fopen (argv[1], "r") : stdin;
        if (file == NULL)
            return 1;
            if(argc!=2){
                printf("[ERR]");
                return 0;
            }
        
       for (i =0; i < max; i++) {
           
    reds[i] = malloc( stringlength);
        fgets(reds[i], stringlength, file);
    
      }
          
        int i2 = 0; 
          for (i =0; i < max; i++){
              
        printf ("%s", reds[i]);
         
      }
     
      for(i= 0;i<max;i++){
     char *p = strtok (reds[i], " ");
    
    
        while (p != NULL)
        {
            cardarray[i2++] = p;
            p = strtok (NULL, " ");
        }
    }
    
     for (i =0; i < max; i++){
              
        printf ("%s", cardarray[i]);
         
      }
    
      
        return 0;    
          
       }

    Input:
    Code:
    RED A
    RED 2
    RED 3
    RED 4
    RED 5
    RED 6
    RED 7
    RED 8
    RED 9
    RED 10
    RED J
    RED Q
    RED K

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Write a function that takes a string and removes spaces from it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    but I don't think hat would solve my stack smashing problem

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, it would. You should write functions that do one thing and do it well. This way, you can divide a big program into smaller pieces such that the smaller pieces are easier to test and debug; hence you will find and solve your stack smashing problem.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    char *reds[max];
    char * cardarray[max];

    You would seem to need max*2 cardarray entries.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2018
    Posts
    43
    I am almost there
    I wrote a function.
    But now it does removes the space from the first array element.

    Code:
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define max 13
    #define stringlength 8
    const char * stringcard[] = { "REDA","RED2"
    "RED3"
    "RED4"
    "RED5"
    "RED6"
    "RED7"
    "RED8"
    "RED9"
    "RED10"
    "REDJ"
    "REDQ"
    "REDK"
    };
    char *removechar(char *str, int ch)
    {
        int i = 0;
        char *cpos = str;
        
        while((cpos = strchr(cpos, ch)))
        {
            strcpy(cpos, cpos + 1);
        }
        return str;
    }
    
    
    int main (int argc, char **argv) {
    
    char *reds[max];
    
    
    int i;
    
        FILE *file = argc > 1 ? fopen (argv[1], "r") : stdin;
        if (file == NULL)
            return 1;
            if(argc!=2){
                printf("[ERR]");
                return 0;
            }
        
       for (i =0; i < max; i++) {
           
    reds[i] = malloc( stringlength);
        fgets(reds[i], stringlength, file);
    
      }
          
        
          for (i =0; i < max; i++){
              
        printf ("%s", reds[i]);
         
      }
    
    
    removechar(*reds,' ');
    
    
     
     
    
     for (i =0; i < max; i++){
              
        printf ("%s", reds[i]);
         
      }
    
      
        return 0;    
          
       }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Another thing you need to do is to fix your atrocious indentation and use blank lines appropriately to separate logical sections of code, e.g.,
    Code:
    int main(int argc, char **argv) {
        const char * stringcard[] = {
            "REDA",
            "RED2"
            "RED3"
            "RED4"
            "RED5"
            "RED6"
            "RED7"
            "RED8"
            "RED9"
            "RED10"
            "REDJ"
            "REDQ"
            "REDK"
        };
    
        char *reds[max];
        char *cardarray[max];
    
        int i;
    
        FILE *file = argc > 1 ? fopen(argv[1], "r") : stdin;
        if (file == NULL)
        {
            return 1;
        }
    
        if (argc != 2)
        {
            printf("[ERR]");
            return 0;
        }
    
        for (i = 0; i < max; i++)
        {
            reds[i] = malloc(stringlength);
            fgets(reds[i], stringlength, file);
        }
    
        int i2 = 0;
        for (i = 0; i < max; i++)
        {
            printf("%s", reds[i]);
        }
    
        for (i = 0; i < max; i++)
        {
            char *p = strtok(reds[i], " ");
            while (p != NULL)
            {
                cardarray[i2++] = p;
                p = strtok(NULL, " ");
            }
        }
    
        for (i = 0; i < max; i++)
        {
            printf("%s", cardarray[i]);
        }
    
        return 0;
    }
    It does look like your strtok attempts are to do this "remove spaces" thing... I daresay that's a wrong approach. A simpler approach is to loop over the string with two pointers: one pointer points to the current write position, the other points to the current read position. As you iterate until and including the terminating null character, if the write position and read positions differ, you write the current character in the read position to the write position if it is not a space.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Quote Originally Posted by Obvious Guy View Post
    Code:
    char *removechar(char *str, int ch)
    {
        int i = 0;
        char *cpos = str;
        
        while((cpos = strchr(cpos, ch)))
        {
            strcpy(cpos, cpos + 1);
        }
        return str;
    }
    One problem with this function is that strcpy() with overlapping strings (cpos and cpos+1 are overlapping) is undefined. You may or may not get the results that you want or expect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. removing spaces in a string
    By camel-man in forum C Programming
    Replies: 13
    Last Post: 04-01-2011, 09:43 AM
  2. Replies: 3
    Last Post: 12-10-2010, 12:07 AM
  3. removing spaces from a string
    By bradleym83 in forum C++ Programming
    Replies: 4
    Last Post: 07-28-2005, 01:59 PM
  4. Replies: 1
    Last Post: 03-08-2005, 12:02 PM
  5. Removing spaces from strings
    By PunkyBunny300 in forum C Programming
    Replies: 6
    Last Post: 02-21-2003, 02:37 PM

Tags for this Thread