C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-26-2009, 02:37 PM   #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?
jensbodal is offline   Reply With Quote
Old 11-26-2009, 03:17 PM   #2
Registered User
 
Join Date: Jan 2009
Posts: 202
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'.
Subsonics is offline   Reply With Quote
Old 11-26-2009, 03:46 PM   #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
jensbodal is offline   Reply With Quote
Old 11-26-2009, 04:08 PM   #4
Registered User
 
Join Date: Jan 2009
Posts: 202
So you DO want space characters but not newline, then why not just use [^\n] as you did in your example?
Subsonics is offline   Reply With Quote
Old 11-26-2009, 04:12 PM   #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.
jensbodal is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 04:44 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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