Student Portfolio >> Index >> Coding >> Exercise 4
#include<iostream.h>
#include<math.h>
#include<string.h>
const int SIZE = 100;
int countcaps(char[]); int main()
{
char string_input[SIZE];
int caps_ctr = 0;
cout << "Type a sample string: "; // reads in string input
cin.getline(string_input,SIZE);
caps_ctr = countcaps(string_input); //function call to count capital letters
//------------------------------------------------------------------------
// prints out output of function call after assignment to variable
//------------------------------------------------------------------------
cout << "\n";
cout << "\n";
cout << "-------------------------------------------";
cout << "\n";
cout << "The number of CAPITAL letters in the input string is: " << caps_ctr << "\n";
cout << "-------------------------------------------";
cout << "\n";
cout << "\n";
return 0;
}
//function definition int countcaps(char string_input[])
{
int i, caps_ctr=0;
for (i=0; i < strlen(string_input); i++)
{
//cout << "character input is: " << string_input[i];
//cout << "\n";
if (string_input[i] > 'A' && string_input[i] <= 'Z' )
caps_ctr++;
}
return caps_ctr;
}
<< Back Forward >> |
Copyright 2005 Kerstin Kegel