Search code examples
cuppercaselowercase

Upper and lower case


I need to make program where I can insert a line with words, and program will make all words like first one(upper lower cases).

Example:
Insert line - AbbA hall fameee class

and the output of the program should be: - AbbA HalL FamEee ClaSs

How can I check full word, and get out what symbols there is - upper or lower? And then make all words with same scheme.


Solution

  • [Comment] Create a boolean array and capture the upper/lower case information of each character of the first word. Using the same boolean array, format your remaining words. This is all I can help you for now. You can try writing a program and get back to the forum so that you can get more response. Since I do not have privilege to post comment am adding my comment in answer section

    [Answer] Here you go...

    #include <stdio.h>
    #include <string.h>
    
    int main() {
    
        char inputStr[] = "AbbA hall fameee class";
        char bool_arr[50];
        char *ptr;
        int len = 0;
        int loopcnt = 0;
        int i = 0;
    
        printf("Input Str: %s\n", inputStr);    
    
        ptr = strtok(inputStr, " ");
        len = strlen(ptr);
    
        for(i = 0; i < len; i++) {
            if(toupper(ptr[i]) != ptr[i]) {
                bool_arr[i] = 0;    //Lower case
            }
            else {
                bool_arr[i] = 1;    //Upper case
            }
        }
    
        while(ptr != NULL) {
            if(strlen(ptr) < len) {
                loopcnt = strlen(ptr);
            }
            else {
                loopcnt = len;
            }
    
            for(i = 0; i < loopcnt; i++) {
                if(bool_arr[i] == 0 && 
                    (tolower(ptr[i]) != ptr[i])) {
                    ptr[i] = tolower(ptr[i]);
                }
    
                if(bool_arr[i] == 1 && 
                    (toupper(ptr[i]) != ptr[i])) {
                    ptr[i] = toupper(ptr[i]);
                }
            }
    
            printf("%s ", ptr);     
    
            ptr = strtok(NULL, " ");    
        }
    }