C_C++_BJH_01. Capitalize the First Letter
Design a function that meets the following requirements:
l Capitalize the first letter of each word in a character string.
l If the first letter is capitalized or the first character is not a letter, leave the first character unchanged.
- Function to be implemented:
void vConvert2Upper(char *pInputStr, long lInputLen, char *pOutputStr);
[Input] pInputStr: pointing to a character string
lInputLen: length of the character string
pOutputStr: pointing to a (lInputLen+1)–sized memory for output
[Output] None
[Note] Implement only the function algorithm, and do not perform any input or output.
Input: this is a Dog.
Output: This Is A Dog.
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 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
|
| #include <iostream> #include <string.h> #include <ctype.h> using namespace std; void vConvert2Upper(char *pInputStr, long lInputLen, char *pOutputStr) { if ((pInputStr == NULL) || (lInputLen <= 0) || (pOutputStr == NULL)) { return; } for (int i = 0; i < lInputLen; i++) { if ((i == 0) || (*pInputStr == ' ')) { if (*pInputStr == ' ') { *pOutputStr = *pInputStr; pInputStr++; pOutputStr++; } if (islower(*pInputStr)) { *pOutputStr = *pInputStr + 'A' - 'a'; } else { *pOutputStr = *pInputStr; } } else { *pOutputStr = *pInputStr; } pInputStr++; pOutputStr++; } *pOutputStr = '\0'; } int main() { char *pInputStr = "this is a Dog."; char pOutputStr[20]; vConvert2Upper(pInputStr, strlen(pInputStr), pOutputStr); cout << pOutputStr; return 0; }
|