here is the other problem project im sure it's something small im missing.
for some reason there is no print out for the most used letter
demo.cpp
/* File Name: demo.cpp
Chapter No. 12 - Exercise No. 9
Programmer: Carl Sue
Date Last Modified: Mar 23, 2010
Problem Statement: (what you want the code to do)
Write a function that accepts either a pointer to a c-string or
a string object as it's argument the function should return the
character that appears most frequently in the string demonstrate
the function in a complete program.
Input validation: do not accept negative numbers for test scores.
Overall Plan (step-by-step how you want the code to make it happen):
1.
2.
3.
etc.
Classes needed and Purpose (Input, Processing, Output):
*/
#include <iostream>
#include <string.h>
using namespace std;
char mostused(char*sentance);
char mostused(string sentance);
int main(int argc, char * const argv[]){
string test1 = "hello my name is Carl";
char test2[30] = "hello name is Carl";
char output1, output2;
output1 = mostused(test1);
output2 = mostused(test2);
cout << output1;
cout << output2;
return 0;
}
char mostused(char*sentance){
int i, temp, flag = 1, numLength = strlen(sentance);
int d = numLength;
while( flag || (d > 1)){
flag = 0;
d = (d+1) / 2;
for (i = 0; i < (numLength - d); i++){
if (sentance[i + d] > sentance[i]){
temp = sentance[i + d];
sentance[i + d] = sentance[i];
sentance[i] = temp;
flag = 1;
}
}
}
int array[255] = {0};
int j, max, index;
for(j = 0; sentance[j] != 0; j++){
++array[sentance[j]];
}
max = array[0];
index = 0;
for(j = 0; sentance[j] != 0; j++){
if( array[j] > max){
max = array[j];
index = j;
}
}
cerr << (char)index;
return (char)index;
}
char mostused(string sentance){
int i, temp, flag = 1, numLength = sentance.length( );
int d = numLength;
while( flag || (d > 1)){
flag = 0;
d = (d+1) / 2;
for (i = 0; i < (numLength - d); i++){
if (sentance[i + d] > sentance[i]){
temp = sentance[i + d];
sentance[i + d] = sentance[i];
sentance[i] = temp;
flag = 1;
}
}
}
int array[255] = {0};
int j, max, index;
for(j = 0; sentance[j] != 0; j++){
++array[sentance[j]];
}
max = array[0];
index = 0;
for(j = 0; sentance[j] != 0; j++){
if( array[j] > max){
max = array[j];
index = j;
}
}
cerr << (char)index;
return (char)index;
}
thanks for any pointers