Quote:
Originally Posted by slant eyes
hi all, i'm stuck on this here and hope you guys can lend a hand
i'm supposed to write a program that watches for an accidental caps lock press that would lead to a string of characters such as 'tHIS IS THE ERROR'. what the program would do is find such occurences and fix it. ie replace the 't' with a 'T' and the remaining caps with their corresponding lower case values. this is what i have so far. this just replaces all caps with lower cases. i'm not sure how i would locate more than 1 consecutive upper case character. or how i would accept long string input from the keyboard.
thanks
#include <stdio.h>
main()
{
int i;
char z[20];
gets(z);
for(i=0; i<20; i=i+1)
{
if (z[i]>='A' && z[i]<='Z')
{
z[i]=z[i]+ 'a'-'A';
}
}
printf("%s\n",z);
getchar();
}
|
#include <string>
#include <iostream>
#include <cctype>
int main() {
//get a sentence.
std::string mystring;
getline(cin,full_name);
//force the first character of the string to uppercase
mystring[0]=toupper(mystring[0]);
//loop through the rest of the string and force them to lowercase, as long
//as the previous
boolean previousIsCapital=true;
for (int i=1;i < mystring.length()-1;++i) {
// enforce no two capitals in a row. will screw up acronyms, though.
if (previousIsCapital) {
tolower(mystring[i]);
previousIsCapital = false;
else
previousIsCapital = true;
}
}
return 0;
}
The requirements are too vague. How is a computer ever supposed to understand grammar? How would it know that "LOL" is an acronym and is supposed to be all caps? Things like that...