943,948 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 3390
  • C RSS
Jan 22nd, 2008
0

scanning char array for \0 problem

Expand Post »
I'm trying to make a function that can scan a char array, and finish it's business when it comes to a '\0'. so here is the for loop i came up with:
  1. for( i=0 ; charArray[i] != '\0' ; i++ )
But here is an example of entered data: "1234 432"

It seems to end the loop at the space. So are spaces and null characters similar? how can i differentiate between the two so that it keeps going right through a space and only stops at a null?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt You is offline Offline
6 posts
since Jan 2008
Jan 22nd, 2008
0

Re: scanning char array for \0 problem

>So are spaces and null characters similar?
No, not at all.

>how can i differentiate between the two
You don't need to differentiate between the two. You need to figure out why your code is stopping when you don't think it should. We can help, but you need to post more code. Preferably a complete program that exhibits the problem.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jan 22nd, 2008
0

Re: scanning char array for \0 problem

here ya go:
  1. #include <stdio.h>
  2.  
  3. int dummy;
  4.  
  5. int stringSplitter(char arrayToSplit[], int arraySize);
  6.  
  7. int main()
  8. {
  9. char userString[20];
  10.  
  11. printf("Enter 3 numbers, seperated by spaces: ");
  12. scanf( "%s", userString);
  13.  
  14. stringSplitter(userString, 20);
  15.  
  16. scanf("%d", dummy);
  17. }
  18.  
  19. int stringSplitter(char arrayToSplit[], int arraySize)
  20. {
  21. unsigned int i;
  22.  
  23. for( i=0 ; arrayToSplit[i]!='\0' ; i++ )
  24. {
  25. printf("%c", arrayToSplit[i]);
  26.  
  27. }
  28.  
  29. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt You is offline Offline
6 posts
since Jan 2008
Jan 22nd, 2008
0

Re: scanning char array for \0 problem

Ah, the loop ends at the space because the string ends at the space. scanf stops reading input at spaces. So when you enter "1234 432" the userString gets "1234", and " 432" is left waiting to be read.

If you want to get a string from the user try using fgets().

Good luck.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Jan 22nd, 2008
0

Re: scanning char array for \0 problem

Click to Expand / Collapse  Quote originally posted by Duoas ...
Ah, the loop ends at the space because the string ends at the space. scanf stops reading input at spaces. So when you enter "1234 432" the userString gets "1234", and " 432" is left waiting to be read.

If you want to get a string from the user try using fgets().

Good luck.
Thank you! I never would have figured that out. It all works the way it should now.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Matt You is offline Offline
6 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Putting Whitespaces and newlines
Next Thread in C Forum Timeline: display array element with passing value...no undesstand~help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC