Yêu cầu: Viết chương trình đổi kí tự đầu tiên của mỗi từ thành chữ in hoa
Thuật toán:
- Tìm kí tự đầu tiên của một từ
- Kiểm tra xem kí tự có phải kí tự in thường. Sau đó, thay thế bằng kí tự in hoa (c = c – 32)
/**************Replace lowcase to uppercase character********** * Author: vncoding * Date : 12/01/2016 ***************************************************************/ #include "stdio.h" #include "conio.h" #include "string.h" void replowcase(char *str); void main() { char str[] = " c programming at vncoding forum "; char c = '+'; printf("original string: '%s'\n", str); replowcase(str); printf("new string: '%s'\n", str); getch(); } /************************************************ Replace first character of each word into uppercase *************************************************/ void replowcase(char *str) { int length = strlen(str); int i; bool isSpace = false; for (i = 0; i < length; i++) { // the first character of string if (i == 0 && str[i] != ' ' && str[i] != '\t') { if (str[i] <= 'z' && str[i] >= 'a') { str[i] = str[i] - 32; } } if (str[i] == ' ' || str[i] == '\t') { isSpace = true; } else if (isSpace) { if (str[i] <= 'z' && str[i] >= 'a') { str[i] = str[i] - 32; } isSpace = false; } } }
Kết quả:
Leave a Reply
You must be logged in to post a comment.