scanning char array for \0 problem

Thread Solved
Reply

Join Date: Jan 2008
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

scanning char array for \0 problem

 
0
  #1
Jan 22nd, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,541
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 704
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: scanning char array for \0 problem

 
0
  #2
Jan 22nd, 2008
>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.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

Re: scanning char array for \0 problem

 
0
  #3
Jan 22nd, 2008
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. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,951
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: scanning char array for \0 problem

 
0
  #4
Jan 22nd, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 6
Reputation: Matt You is an unknown quantity at this point 
Solved Threads: 0
Matt You Matt You is offline Offline
Newbie Poster

Re: scanning char array for \0 problem

 
0
  #5
Jan 22nd, 2008
Originally Posted by Duoas View Post
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC