Write a program which, given a string, converts all lower case letters to upper case and leaves the others unchanged.

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>
#include <ctype.h>
#define MAX 100

void get_line(char *, int);

int main(int argc, char ** argv) {
    char line[MAX];
    get_line(line, MAX);

    for(int i = 0; line[i] != 0; i++)
    return 0;

void get_line(char line[], int N) {
    char c;
    int i;
    for (i = 0; i < N-1 && (c = getchar()) != '\n'; i++)
        line[i] = c;
    line[i] = '\0';

When you compile and execute the above program it produces the following result on Linux:

Hello String

Brief Explanation

  • The program starts by calling the function get_line which takes two parameters, a character array and the max size of a line. Using the function getchar and a for loop, a line is read from the standard input. The function getchar returns the next input character each time it is called, or EOF when it encounters end of file. The symbolic constant EOF is defined in <stdio.h>
  • Using the function putchar, toupper and a for loop, all lower case letters in character array line are converted to upper case while upper case letters are left unchanged.
  • The function putchar prints a character each time it is called: putchar(c); prints the contents of the integer variable c as a character, usually on the screen.
  • The function toupper converts a lower case letter to upper case. toupper(c) returns the corresponding upper case letter; otherwise it returns c.
  • A return value of zero implies normal termination; non-zero values signal unusual or erroneous termination conditions.

Add comment

Security code