C programming language

C: convert string to lowercase and uppercase

Introduction

Today we will see how to convert a string in C to uppercase. We will also see how to convert a string to lowercase. That is, to convert something like “Hello World” to “hello world”. Or something like “Good night” to “GOOD NIGHT”.

For this we will use some functions that the standard library has: tolower and toupper.

ctype.h library in C

To use toupper and tolower in C we will need to include the library that defines it. We simply include it with #include:

#include <ctype.h>

With this we can use toupper and tolower in C to convert strings.

Convert C string to uppercase

There is no direct way to convert a string to upper case; but there is a way to convert a char to a capital letter.

Remember that strings are only arrays of characters; so we can go through them and capitalize each of its elements. Then let’s see this program that defines a string and then converts it to uppercase to print it later:

See the gist on github.

First we define a string. We print it to show it in its original state. Then we do a for loop where we increase the index from 0 until we reach the termination character \0

For each element in the array we convert the capital letter using toupper function that comes when we include ctype.h

The execution of the program is as follows:

Convert C string to upper case

You can try it here.

Convert string to lowercase in C

This is almost the same as above, but now we use tolower. The same code above, but to convert a string to lowercase, looks like this:

See the gist on github.

What we do is go through the string and turn each character into its lowercase representation.

Try it here.

Conclusion

This is how we can convert a string to uppercase or lowercase using the C language.


I am available for hiring if you need help! I can help you with your project or homework feel free to contact me.
If you liked the post, show your appreciation by sharing it, or making a donation

parzibyte

Freelancer programmer ready to work with you. Web, mobile and desktop applications. PHP, Java, Go, Python, JavaScript, Kotlin and more :) https://parzibyte.me/

Compartir
Publicado por
parzibyte

Entradas recientes

Receipt designer for thermal printers – Free and open source

In the last months I have been working on a ticket designer to print on…

4 months hace

JavaScript: store and read files with the Origin Private File System

In this post you will learn how to use the Origin Private File System with…

7 months hace

JavaScript: download file with fetch

In this post you will learn how to download a file in the background using…

7 months hace

SQLite3 with vanilla JavaScript and OPFS – Hello world

In this post I will show you how to use SQLite3 directly in the web…

9 months hace

Python Thermal Printing: A Comprehensive Guide for Printing on Thermal Printers

In this tutorial, we'll explore how to effortlessly print receipts, invoices, and tickets on a…

10 months hace

Image printing on Thermal printer

When printing receipts on thermal printers (ESC POS) sometimes it is needed to print images…

10 months hace

Esta web usa cookies.