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.

// 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 :-)

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)

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..

but it won't let me enter space as a search character

Your program

// 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++;
	}

define

char c = ' ';
String s = "space space";

and run
This piece of code does not discriminate spaces
Look in your code places where space is removed

silly me :-)))
problem was that i was using a class for keyboard input that trims space..
sorry for the trouble :-))) thnx

You gave advice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.