1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
|
#include <iostream>
int charsetIndex(char* charset, char chr, size_t dim);
void waitForEnter();
int main()
{
char charset[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
'U', 'V', 'W', 'X', 'Y', 'Z' };
std::cout << "'B' was found in charset at position "
<< charsetIndex(charset, 'B', sizeof(charset)/sizeof(char))
<< '\n';
waitForEnter();
return 0;
}
/* charsetIndex() - searches charset for the character chr.
* charset is supposed to be not null-terminated.
* dim is supposed to be charset dimension.
* Returns the character position inside the string or -1 if not found.
*/
int charsetIndex(char* charset, char chr, size_t dim)
{
for(size_t i = 0; i < dim; i++) {
if (chr == charset[i]) { return i; }
}
return -1; // not found
}
void waitForEnter()
{
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
|