problem with character space (character search)

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2009
Posts: 3
Reputation: snehula is an unknown quantity at this point 
Solved Threads: 0
snehula snehula is offline Offline
Newbie Poster

problem with character space (character search)

 
0
  #1
27 Days Ago
Hi :-)

Yeah I'm new to this whole thing, that's why I'm probably going to ask a lot of stupid questions, I apologize in advance..
The story is: I'm writing a program to enter a string and a search character and output the number of times this character occured in the string.

  1. // s for string and c for search character
  2. int i = 0;
  3. int result = 0; //number of occurences
  4. for(i = 0; i < s.length(); i++)
  5. {
  6. if (s.charAt(i) == c) //
  7. result++;
  8. }

Everything is fine, but I haven't got a clue how to handle the situation where the search character is a space (or other special characters i guess)..
Thanks for the help :-)
Last edited by peter_budo; 27 Days Ago at 9:02 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks)
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
27 Days Ago
You've written a program to count the specific character given c. This character may also be a space. How much can be different characters in your text? Each character has its representation as an integer.
Create an array of sufficient numbers of cells, and then in a single loop process, increase a value of a cell at the index, where index is the integer representation of char. (base ascii set of chars 0-255)
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: snehula is an unknown quantity at this point 
Solved Threads: 0
snehula snehula is offline Offline
Newbie Poster

hmm..

 
0
  #3
27 Days Ago
Originally Posted by quuba View Post
You've written a program to count the specific character given c. This character may also be a space. How much can be different characters in your text? Each character has its representation as an integer.
Create an array of sufficient numbers of cells, and then in a single loop process, increase a value of a cell at the index, where index is the integer representation of char. (base ascii set of chars 0-255)
What you're saying is get the index position of the given character (is it?), which i had to do for another program.. this one only has to count how many times the character occurs in the input string.
Anyway, my problem is not that I can't count the occurences or find the index positions of the character.
It's all working fine but it won't let me enter space as a search character. I don't know if I'm supposed to reference space some other way but anyway, this is supposed to be a simple little program where the user can enter any search character they like. Apparently the space key is a special character..
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #4
26 Days Ago
but it won't let me enter space as a search character
Your program
  1. // s for string and c for search character
  2. int i = 0;
  3. int result = 0; //number of occurences
  4. for(i = 0; i < s.length(); i++)
  5. {
  6. if (s.charAt(i) == c) //
  7. result++;
  8. }
define
  1. char c = ' ';
  2. String s = "space space";
and run
This piece of code does not discriminate spaces
Look in your code places where space is removed
Last edited by quuba; 26 Days Ago at 7:50 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 3
Reputation: snehula is an unknown quantity at this point 
Solved Threads: 0
snehula snehula is offline Offline
Newbie Poster

auch!

 
0
  #5
23 Days Ago
silly me :-)))
problem was that i was using a class for keyboard input that trims space..
sorry for the trouble :-))) thnx
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #6
23 Days Ago
You gave advice.
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