| | |
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; Nov 5th, 2009 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 Nov 5th, 2009
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 33 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; 33 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 |
2dgraphics account android api apple applet application array arrays automation banking binary binarytree bluetooth chat chatprogramusingobjects class classes client code component database derby design draw eclipse encryption error event exception fractal game givemetehcodez graphics gui homework html ide if_statement image inheritance input integer interface j2me java javadesktopapplications javaprojects jlabel jni jpanel jtextfield julia linux list loop map method methods midlethttpconnection mobile monitoring netbeans newbie nullpointerexception open-source oracle print printing problem program programming project property recursion reference ria scanner screen search server set size sms sort sourcelabs splash sql sqlite stop string swing testautomation threads time tree ui unicode validation windows





