Easy Tutorial
❮ C Command Line Arguments C Examples Binary Decimal Convert ❯

C Exercise Example 98

C Language Classic 100 Examples

Title: Input a string from the keyboard, convert all lowercase letters to uppercase, and then output it to a disk file named "test". The input string ends with an exclamation mark.

Program Analysis: None.

Program Source Code:

Example

//  Created by www.tutorialpro.org on 15/11/9.
//  Copyright © 2015年 tutorialpro.org. All rights reserved.
//

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main()
{
    FILE* fp = NULL;
    char str[50];
    int i, len;
    printf("Enter a string:\n");
    gets(str);
    len = strlen(str);
    for(i = 0; i < len; i++)
    {
        if(str[i] <= 'z' && str[i] >= 'a')
            str[i] -= 32;
    }
    if((fp = fopen("test", "w")) == NULL)
    {
        printf("error: cannot open file!\n");
        exit(0);
    }
    fprintf(fp, "%s", str);
    fclose(fp);

    system("pause");
    return 0;
}

The above example output results are as follows:

Enter a string:
www.tutorialpro.org

C Language Classic 100 Examples

❮ C Command Line Arguments C Examples Binary Decimal Convert ❯