![]() |
| | #1 |
| Registered User Join Date: Nov 2009
Posts: 6
| Question about scanf and spaces or whitespace 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"
Code: scanf("%[^\n]",string);
printf("The string entered is: %s,string);
User inputs: "Hi this is a string"
Output: "Hi this is a string"
|
| jensbodal is offline | |
| | #2 | |
| Registered User Join Date: Jan 2009
Posts: 202
| Quote:
Code: scanf("%[^ ]",string);
printf("The string entered is: %s" , string);
| |
| Subsonics is offline | |
| | #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
}
Last edited by jensbodal; 11-26-2009 at 03:49 PM. Reason: Stated how I am compiling |
| jensbodal is offline | |
| | #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 | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |