Skip to main content

All Caps

Let's write a program that accepts a word from the user and prints that word out in all caps. You may assume that the word will be at most 50 characters. Here is a sample run.

Enter a word: r2d2
r2d2 in all caps: R2D2

What are the steps?

Think about what you will need to do to solve this problem... What do you already know how to do? Try to break it down step by step, then expand for the solution:

Click to see steps
  1. Prompt user for a word
  2. Read the word into a string
  3. Go through each character in the string
  4. Check if the character is a lowercase letter
  5. If it is lowercase, convert it to uppercase
  6. If it's not lowercase, leave it unchanged
  7. Print the result

Looking at the above steps, we know how to prompt and read input but what we don't know how to do is read an entire word as we had always just read in a number or a single character.

We also need to be able to go through all the characters of the word and upper case all the lower case ones.

Let's start building our program and consider what we might need.

Storing "words"

To store multiple characters, we need to use a concept called strings. Strings are essentially a sequence of characters. In C, the full name for this type of data is null-terminated character strings. Notice the addition of the words null-terminated. In C, strings are character arrays that end with the addition of a null character, which we write as \0. The null character does not get printed when you print the string. It is there only to indicate that this is where the string ends.

All C strings require this null character. The use of this null character is actually the cause of numerous errors and security flaws. Understanding how C strings work is a very important part of learning to program in C.

Since C strings are just character arrays, we declare character arrays in order to store strings. Normally we would use the provided maximum capacity directly from our spec. But remember that C strings must include a null character. Thus, our array needs an extra spot just to hold this null char.

#include <stdio.h>
#define WORDMAX 50

//this function reads in a word from the user
void getWord(char word[]);


int main(void)
{

//we need the +1 for the null char. if we use just WORDMAX
//the variable would only be able to hold 49 characters max!
char word[WORDMAX + 1];
getWord(word);
return 0;
}

void getWord(char word[])
{
//...
}

To read the in a word, we use the format code %s. %s allows us to read a sequence of characters up to by not including a whitespace character (space, tabs, newlines... etc.)

#include <stdio.h>

void getWord(char word[]);

int main(void)
{
char word[100];

getWord(word);

return 0;
}

void getWord(char word[])
{
printf("Enter a word: ");
scanf("%s", word);
}

Next we want to write a function to convert a string to allcaps


void allCaps(char word[]
{
//calculate the distance between lower case to upper case.
//As lower case sequence is same as upper case
//sequence.. that is a -> b ->c ... A->B->C...
//Thus the distance between the the corresponding letters are the same;
int distance = 'a' - 'A';
int i;
//loop through and check for null char to get to the end
for (i = 0 ; word[i] != '\0'; i ++){
//if lower case
if(word[i] >='a' && word[i] <= 'z'){
//convert
word[i] = word[i] - distance;
}


}
}

Putting it all together:

#include <stdio.h>

void getWord(char word[]);
void allCaps(char word[]);

int main(void)
{
char word[100];

getWord(word);
printf("%s in all caps: ", word);
allCaps(word);
printf("%s\n", word);
return 0;
}

void getWord(char word[])
{
printf("Enter a word: ");
scanf("%s", word);
}
void allCaps(char word[])
{
//calculate the distance between lower case to upper case.
//As lower case sequence is same as upper case
//sequence.. that is a -> b ->c ... A->B->C...
//Thus the distance between the the corresponding letters are the same;
int distance = 'a' - 'A';
int i;
//loop through and check for null char to get to the end
for (i = 0 ; word[i] != '\0'; i ++){
//if lower case
if(word[i] >='a' && word[i] <= 'z'){
//convert
word[i] = word[i] - distance;
}

}
}