-1

I have a string of 80 chars (line from .txt file)
Somewhere at the end of the string I have numbers or strings or chars and "," (comma) between them.
I need to delete these spaces around "," so I will be able to get them by strtok().
Any ideas ?
For example :

String : " name: today 12 ,r ,ab,     5     ,   seven"<br>

I need : " name: today 12,r,ab,5,seven"

JanR
  • 6,052
  • 3
  • 23
  • 30

3 Answers3

3

You can apply this algorithm ::

  1. Find the element , in this case a space.
  2. Replace the element with an element of your choice, in this case an empty character.

This function might come handy for replacing any character to a string. You might add the char *replace function as a snippet and use it later for similar purposes.

   char *replace(const char *the_input_string, char the_character,
                 const char *replacing_string) 
  {
      int count = 0;
      const char *t;
      for(t=the_input_string; *t; t++)
          count += (*t == the_character);

      size_t rlen = strlen(replacing_string);
      char *res = (char*)malloc(strlen(the_input_string) + (rlen-1)*count + 1);
      char *ptr = res;
      for(t=the_input_string; *t; t++) 
      {
          if(*t == the_character) 
          {
              memcpy(ptr, replacing_string, rlen);
              ptr += rlen;
          }
          else 
              *ptr++ = *t;
      }
      *ptr = 0;
      return res;
  }

Driver Program ::

int main(int argc, char const *argv[])
{
    const char *s = replace("name: today 12 ,r ,ab, 5 , seven", ' ', "");
    printf("%s\n", s);
    return 0;
}

Please refer to this link and the code might be verisimilar but use the above code as the solution mentioned there might throw some errors or warnings.

Community
  • 1
  • 1
0

You may give this a try!
Replace with your code where necessary.

#include <stdio.h>
#include <stdlib.h>

int main()
{   int i;
    char line[] = "name: today 12 ,r ,ab, 5 , seven";
    int length = strlen(line);
    char line2[length];
    for(i = 0; i<length; i++) {
        if(!isspace(line[i])) {
             line2[i] = line[i];
        }
    }
    for(i = 0; i<length; i++){
        printf("%c", line2[i]);
    }
    return 0;
}
CodeWalker
  • 2,281
  • 4
  • 23
  • 50
  • 2
    This will delete all spaces, while i need to delete spaces only around "," – Mark Vaitzman Mar 10 '16 at 05:12
  • This solution will also leave gaps in the destination string, because you use the same index for both source and destination, but the destination. – M Oehm Mar 10 '16 at 05:57
0

Because the resulting string will be shorter then the original string, you can do the replacement in place: When you find a comma, copy it and skip the following space. To treat the space before the comma, keep track of the first space after the last non-space character and skip that, too if necessary:

    #include <stdlib.h>
    #include <stdio.h>
    #include <ctype.h>

    void remspc(char *str)
    {
        char *first = str;          // dest. char after last non-space
        char *q = str;              // destination pointer

        // skip leading white space
        while (isspace((unsigned char) *str)) str++;

        while (*str) {
            if (*str == ',') {
                q = first;          // skip space before comma
                *q++ = *str++;
                first = q;

                // skip space after comma
                while (isspace((unsigned char) *str)) str++;
            } else {
                // remember last non-space
                if (!isspace((unsigned char) *str)) first = q + 1;
                *q++ = *str++;
            }
        }

        *first = '\0';
    }

    int main(void)
    {
        char str[] = " name: today 12, r ,ab,   ,   5     ,   seven";

        remspc(str);
        puts(str);

        return 0;
   }

This solution will run commas that are separated by white space together, which may lead to problems with strtok, because it will consider stretches of commas as a single delimiter.

M Oehm
  • 28,726
  • 3
  • 31
  • 42
  • this works great, the only thins its deletes the spaces at the begining of the string as well , before "name". I'll change that. thanks – Mark Vaitzman Mar 10 '16 at 18:43
  • Well, I think you can spot the line that is responsible for that and remove it. You are free to modify the code according to your needs, of course. – M Oehm Mar 10 '16 at 18:50