Eat spaces and newline (C)

spn.c

#include <stdio.h>
#include <string.h>
 
 
int main(void)
{
    char buffer[15] = "Samaras\n";

    /* To remove the newline (LF or CR LF) at the end of the line */
    /* Note: this will put a null terminator at the 1st newline   */

    printf("Before: buffer=|%s|", buffer);
    /* Use of strcspn */
    /*/////////////////////////////////////////*/
    buffer[strcspn(buffer, "\r\n")] = '\0';
    /*/////////////////////////////////////////*/
    printf("After: buffer=|%s|", buffer);
    
    printf("\nSecond part\n");
    char* pointer = "  \tGeorge";
    /* If we have a pointer to the buffer (not the buffer itself) */
    /* to skip over leading whitespace                            */

    printf("Before: pointer=|%s|\n", pointer);
    /* Use of strspn. Different function from before! */
    /*/////////////////////////////////////////*/
    pointer += strspn(pointer, "\t\n\v\f\r ");
    /*/////////////////////////////////////////*/
    printf("After: pointer=|%s|\n", pointer);
   
    /* Now we will eat leading whitespaces and eat the first newline */
    printf("\nThird part\n");
    char line[35] = "    \t  I study in DIT\n";
    char* linePointer = line;
    size_t len;
    printf("Before: linePointer=|%s| and line=|%s|", linePointer, line);
    /*////////////////////////////////////////////////*/
    linePointer += strspn(linePointer, "\t\n\v\f\r ");
    len = strcspn(line, "\r\n");
    line[len] = '\0';
    /*////////////////////////////////////////////////*/
    printf("After: linePointer=|%s| and line=|%s|", linePointer, line);
    /* Which also gives you the length of the remaining part in len */
    printf("\nlen=%zu\n", len);
    printf("ENDofMAIN");
    return 0;
}

This code was developed by me, G. Samaras, but the idea was of the Nomimal Animal.

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

void trim (char *cs)
{
  char *p;
  size_t len = strlen(cs);
  for (p = cs + len - 1; isspace (*p); --p) /* nothing */ ;
  p[1] = '\0';
  for (p = cs; isspace (*p); ++p) /* nothing */ ;
  memmove (cs, p, len - (size_t) (p - cs) + 1);
}

void test ()
{
  char input[1000];
  printf ("Enter something: ");
  fgets (input, sizeof input, stdin);
  trim (input);
  printf ("|%s|\n", input);
}

int main()
{
  test();
  return 0;
}

This code was developed by Josh and I would like to personally thank him for sharing the code with us. He said: ” I didn’t like what you had, since it required knowing all of the whitespace characters, and my version really does overwrite them in the original memory space.”

Have questions about this code? Comments? Did you find a bug? Let me know! 😀
Page created by G. (George) Samaras (DIT)

11 thoughts on “Eat spaces and newline (C)

  1. If trimming white space means removing it, I prefer the below:

    #include
    #include
    #include

    void trim (char *cs)
    {
    char *p;
    size_t len = strlen(cs);
    for (p = cs + len – 1; isspace(*p); –p) /* nothing */ ;
    p[1] = ”;
    for (p = cs; isspace(*p); ++p) /* nothing */ ;
    memmove(cs, p, len – (size_t)(p – cs) + 1);
    }

    void test ()
    {
    char input[1000];
    printf(“enter something: “);
    fgets(input, sizeof input, stdin);
    trim(input);
    printf(“|%s|\n”, input);
    }

    int main()
    {
    test();
    return 0;
    }

    • Yes, that’s what trimming means in this case. 🙂
      However, I have some comments on your code..

      for( p = cs + len – 1 ; isspace(*p) ; -p) /* nothing */ ; // Here, a minus is missing, right?
      p[1] = “; /* That’s a compilation error */

      • I expected you to bring that up, there were some errors caused by the website; it’s correct in my editor.
        I intended – – p, and that character is supposed to be the terminating zero for the string.

  2. Josh, yes I thought that this was the case. You, for posting code here, you need to use a special tag, called sourcecode, but I do not think we can use at the comments of the page.

    So, yes, I like your code and it seems to work smoothly, but, it has two warnings (one should enable -Wall flag, in order to receive them)!
    main.c: In function `trim’:
    main.c:10: warning: array subscript has type `char’
    main.c:12: warning: array subscript has type `char’

    If we declare the pointer to char, into unsigned char, as this link http://stackoverflow.com/questions/9972359/warning-array-subscript-has-type-char suggests, then these warnings vanish, but we then receive an error of invalid binary operands in memmovve. If you accomplish to remove these warnings (or this error), then I will post your code in the page, as spn.c, of course, if you like me to do such a thing. 🙂

  3. No, for sure its the code’s error Josh. 🙂
    See from yourself. You have, in function trim, a char* p and then take a good look in your for loops inside this function. These fors cause the warnings. Probably, you do not receive them, because you haven’t enabled the flag -Wall.
    Are you sure you have the flag enabled? I bet not, try it yourself.
    // I searched codepad, but did not find an option like this.
    In Linux, you can use something like this: gcc -Wall main.c -o executableFile
    In Windows, it really depends on your IDE.

    Seriously, I am pretty sure you have not enabled -Wall flag, which in this case might be not a big deal, but in general, it is a big help and can help you debug much more effectively. 😉

    • Don’t patronize me

      C:\Users\Josh2>notepad trim.c

      C:\Users\Josh2>gcc -Wall -pedantic -o trim trim.c

      C:\Users\Josh2>more trim.c

      #include
      #include
      #include

      void trim (char *cs)
      {
      char *p;
      size_t len = strlen(cs);
      for (p = cs + len – 1; isspace (*p); –p) /* nothing */ ;
      p[1] = ”;
      for (p = cs; isspace (*p); ++p) /* nothing */ ;
      memmove (cs, p, len – (size_t) (p – cs) + 1);
      }

      void test ()
      {
      char input[1000];
      printf (“Enter something: “);
      fgets (input, sizeof input, stdin);
      trim (input);
      printf (“|%s|\n”, input);
      }

      int main()
      {
      test();
      return 0;
      }

      C:\Users\Josh2>trim
      Enter something: This is a sentence
      |This is a sentence|

      C:\Users\Josh2>gcc –version
      gcc (tdm-1) 4.7.1
      Copyright (C) 2012 Free Software Foundation, Inc.
      This is free software; see the source for copying conditions. There is NO
      warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

      I can find no problem whatsoever.

      • Oh, what the…? You are completely correct! I did not have any intension to patronize you, just to express what I am thinking. Sorry, if I did not show that correctly! In order to make up, do you want me to upload the code in the page? 🙂

  4. You can do whatever you want with the code, I just wanted to share my idea. I didn’t like what you had, since it required knowing all of the whitespace characters, and my version really does overwrite them in the original memory space.

    • Yes I see your point. I will provide that in the page, not only for its simplicity, but in order to prevent the users from copying pasting code that will produce errors (because it lies on the comments section of this page).
      Once again sorry for the misunderstanding of mine. My IDE, just had a bad day I guess and produced me these compiler messages. Sorry once again. 🙂

Leave a comment