[ back to toc ]

c problems

Date: 2002/06/17 12:58

c programming problem
software used : microsoft visual c++ 6.0
1.How can I count the number of times a character(specified by the
user)appears in a sentence(also entered by the user)?I have read notes on
finding a character in a sentence but the notes only show how the search
function finds where the first occurence of the character appears.

2.How can I display either all the odd or all the even numbers between two
numbers entered by the user?The user enters the two numbers and also
selects whether odd or even numbers are to be displayed.I know how to find
the range between the two numbers but do not know how to display either
the odd or even numbers as specified by the user.
1. You can count the number of characters stepping through the string and
counting the number of characters. For example:

char q; /* this is the character to count */
char buffer[1000]; /* this holds the string */
char *s;
int count;

here somehow buffer gets the string to count the number of characters to
count, and q gets the character to count

count = 0;
s = buffer;
while( *s ){
if( *s == q )count++;

now count contains the number of the characters.

2. I assume that you need to know if the number is odd or even. The lowest
bit of the number will tell whether the number is odd or even.

Thus if the variable 'a' holds the number then


is true only if the number is odd.


[ back to toc ]