Question about scanf and spaces or whitespace

This is a discussion on Question about scanf and spaces or whitespace within the C Programming forums, part of the General Programming Boards category; Hey everyone, just found these forums and they seem to be the most active C forums I've found. I'm fairly ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    6

    Question about scanf and spaces or whitespace

    Hey everyone, just found these forums and they seem to be the most active C forums I've found. I'm fairly new to programming and have a quick question about strings. If I want to ask the user for input using scanf, and the user input has spaces, what is the best way to deal with that?

    Example (Doesn't work but I thought it should)

    Code:
    scanf("%s",string);
    printf("The string entered is: %s,string);
    
    User inputs: "Hi this is a string"
    
    Output: "Hi"
    Example (Works but doesn't like the best way)

    Code:
    scanf("%[^\n]",string);
    printf("The string entered is: %s,string);
    
    User inputs: "Hi this is a string"
    
    Output: "Hi this is a string"
    So I can get it to work using %[^\n], which to my knowledge basically tells the scanf to not stop taking input if a space is found. So can a simply Scanf(%s) not take spaces or whitespace?

  2. #2
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    Quote Originally Posted by jensbodal View Post
    So I can get it to work using %[^\n], which to my knowledge basically tells the scanf to not stop taking input if a space is found. So can a simply Scanf(%s) not take spaces or whitespace?
    [^\n] tells scanf to take input until a newline is found. Try:

    Code:
    scanf("%[^ ]",string);
    printf("The string entered is: %s" , string);
    And it is also makes sense to add a max size that is smaller than 'string'.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Hmm I tried your suggestion but I can still only get [^\n] to work. I will just post my whole code to give you a better understanding, I'm just trying to write a simple trim function that will remove spaces from user input. (Well I shouldn't say 'trying' because my code works, but I just don't think using %[^\n] was the right way to do it.

    Code:
    #include <stdio.h>
    
    void strTrim(char* str1, char* str2)
     {
      int i = 0; //String 1's  counter variable
      int i2 = 0; //String 2's counter variable
    
      while (str1[i] != '\0') //While String 1 is not finished being processed
    	{
    	 if (str1[i] == ' ') //Search for spaces
    	  {
    	   i++; //If a space is found then keep searching string one for another character or NULL
    	  }
    	 else {str2[i2] = str1[i]; i++;i2++;} //If no space is found then copy the current character shown by str1[i] to str2[ii]
    	}
     }
    
    
    int main(void)
    {
     char strA[129] = "\0";
     char strB[128] = "\0";
    
     printf("\nString to trim is: "); //Prompt user for String A (Type something like "This has spaces"
     scanf("%[^\n]",strA); //String A input, [^\n] scans ignoring newlines.  Tried with [^ ] to ignore spaces but that didn't work.
     strTrim(strA,strB); //Trim the spaces from String A and copy to String B
     printf("\nString B is: %s\n\n",strB); //Shows String B which is String A with all spaces remove
    }
    Note: I'm just using Ubuntu's terminal to compile (cc trim.c)
    Last edited by jensbodal; 11-26-2009 at 03:49 PM. Reason: Stated how I am compiling

  4. #4
    Registered User
    Join Date
    Jan 2009
    Posts
    1,485
    So you DO want space characters but not newline, then why not just use [^\n] as you did in your example?

  5. #5
    Registered User
    Join Date
    Nov 2009
    Posts
    6
    Sorry I just reread my original post and I wasn't very clear, but it looks like I am using scanf correctly to accept input of a string that contains spaces. Thank you for your replies
    Last edited by jensbodal; 11-26-2009 at 04:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Saving spaces in arrays?
    By alexnb185 in forum C Programming
    Replies: 6
    Last Post: 11-23-2007, 11:53 AM
  2. Quick Question Regarding Scanf() & Phone Numbers!
    By weaveram in forum C Programming
    Replies: 3
    Last Post: 09-20-2007, 10:58 AM
  3. Every other scanf not working
    By Rob20050 in forum C Programming
    Replies: 3
    Last Post: 08-31-2007, 01:48 AM
  4. Newbie problem with scanf function...
    By Mithal in forum C Programming
    Replies: 1
    Last Post: 11-13-2005, 10:28 PM
  5. Scanf with spaces
    By Bajanine in forum C Programming
    Replies: 4
    Last Post: 09-01-2002, 04:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21