Hey all,
I have decided that in order to learn i will need to write code. So i am working on all the questions on my book. And as I am teaching myself i end up with no-one to scrutinize my code.

#include <iostream>

/************Declarations of Functions*********/
	void func1();
	void func2();
//=============================================Tue 28 Apr 2009 18:37:48 IST//
/* Global Test Variables */

	int const max_length=18;
	int quest_count=0 ;
	char input_line[max_length]="???sky???";


int main()
{	

	func1();// Character string with While loop
	std::cout<<"No Of Question Marks = " << quest_count<< "\n";
	quest_count=0;
	func2();//Pointer Based Passing.
	std::cout<<"No Of Question Marks(Pointer Based) = " << quest_count<< "\n";
}

void func1()
{
	int i=0;
	while(i<max_length)
		{
			if(input_line[i]=='?')
			quest_count++;
		i++;
		}
}

void func2()
{

	char *ptr=input_line;
	while(*ptr!=0)
	{
		if(*ptr=='?')
			{
			quest_count++;
			}
		*ptr++;
	}

}

The above program counts the number of "?" marks in a string passed to it.

I would like to recieve comments on the code :) and any improvements that can be made.

I think it would be more useful if one could pass the string into a method and have it return the number of question-marks found (as an unsigned int, or in extraordinary cases an unsigned long int).

commented: long time, no see :D +9

Thanks for the reply.

I would define a constant char for the '?' comparer. If you want to change the compare criteria, you only have to change it once.

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.