| | |
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:
Solved Threads: 0
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.
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 :-)
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.
Java Syntax (Toggle Plain Text)
// s for string and c for search character int i = 0; int result = 0; //number of occurences for(i = 0; i < s.length(); i++) { if (s.charAt(i) == c) // result++; }
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)
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
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)
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)
•
•
Join Date: Nov 2009
Posts: 3
Reputation:
Solved Threads: 0
•
•
•
•
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)
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..
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
0
#4 26 Days Ago
•
•
•
•
but it won't let me enter space as a search character
Java Syntax (Toggle Plain Text)
// s for string and c for search character int i = 0; int result = 0; //number of occurences for(i = 0; i < s.length(); i++) { if (s.charAt(i) == c) // result++; }
Java Syntax (Toggle Plain Text)
char c = ' '; String s = "space space";
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.
![]() |
Similar Threads
- Index of a character array (C++)
- phonebook problem (C++)
- search in a string (Assembly)
- Count number of occurrences of a character search it in a file (C++)
- How to Remove Newline character from a VB String (Visual Basic 4 / 5 / 6)
- Problem with string search of an array (C++)
- Turn Off Display and Select an Animated Character in Search Companion in Windows XP (Windows tips 'n' tweaks)
Other Threads in the Java Forum
- Previous Thread: Help with Code
- Next Thread: Line Clipping and Drawing
| Thread Tools | Search this Thread |
-xlint android api applet application array arrays automation bi binary blackberry block bluetooth chat class classes client code compile compiler component database developmenthelp eclipse error fractal freeze game gameprogramming givemetehcodez graphics gui health html ide image input int integer j2me j2seprojects java javac javaprojects jetbrains jni jpanel jtable julia learningresources lego linux list login loop loops mac main map method methods mobile netbeans newbie notdisplaying number online page print problem program programming project qt recursion scanner screen server set singleton size sms sort sql string swing system template textfields thread threads time title tree tutorial-sample update variablebinding windows working xor





