How to Change String to Upper Case without strupr in C?

Strings are a crucial programming component often used to facilitate user interaction.

For character storage, use a string.

C lacks the data type string, in contrast to most computer languages. It generates a character array and is defined using the char data type.

Also Read: How to Compare two Strings without using strcmp in C?

char str[] = “hello world”;

Double quotes (“”) indicate the char data type for the string str.

Note: It’s essential to double-quote.

Use the code below to output the variable str.

print(“%s”, str);

The format specifier %s tells C that we are working with a string.

Also Read: How to Implement Function Strncpy & Strcpy in C

Because of the string’s role in enabling seamless user interaction, when handling forms in the backend development, if the email address or password is incorrect or too brief, or when characters need to be added or removed from a text message, string manipulation is required.

Common functions include managing string arrays, centering, left- and right-aligning, and searching for text occurrences inside strings. The standard library of the C programming language has a collection of functions known as string handling routines that implement operations on strings (both character and byte strings).

These string handling routines’ function prototypes are specified in the common header file string.h. This lesson will cover how to change a string to the upper caseHow to change a string’s case in C with and without using the strupr string handling function.

How to Change the String to Upper Case using strupr Function in c?

Making the whole string uppercase implies capitalizing every letter.

The case of the supplied string is changed to upper case using the strupr() function. The cstring header file contains a method for manipulating strings.

Here, the terms “string” and “uppercase” are used.

Syntax: strupr(string variable);

The string variable that needs to have its case changed to uppercase is passed as a parameter to the strupr method.

strupr(str);

This code creates an uppercase string from the str variable (defined above).

printf(‘%s’, strupr(str));

This will return the desired output, i.e., in uppercase.

The outcome will be: HELLO WORLD

Any case format, including lowercase or alternating capitals, is acceptable for the function’s string parameter.

How to Change the String to Upper Case without strupr in C?

Without utilizing the string handling method strupr, we may do the same task using ASCII code.

In ASCII coding, different symbols are represented by integers in the range 0-127.

ASCII – American standard code for information Interchange.

For instance, the decimal number 65 is given to A, and the number 97 is given to the letter “a.”

By observing the distinction between uppercase and lowercase decimal numbers, we may quickly change a text to uppercase.

For the transformation, adhere to the following code.

#include<stdio.h>

This is a standard I/O header file.

int main()

Every C program’s initial function is launching, executing, and ending the program. 

{

 char str[] = “hello world’;

 int i;

Declaring an str variable and int type variable i.

 for (i=0; str[i]!=’\0′; i++)

For loop to run till ‘\0’ is encountered.

 {

           if (str[i]>=97 && str[i]<=122)

           Check the ASCII value of str[i] range. If it is between 97 to 122, do the next step.

           {

                       str[i] = str[i]-32;

Str[i] is modified by 32 by reducing its value. Such that uppercase characters are used in the resulting ASCII code.

           }

 }

 printf (“String in uppercase is: “,str);

 Print string variable value.

 return 0;

}

Note: The ASCII value can be verified by printing it.

Char str = ‘a’;

printf (‘%d’, str);

Greetings, My name is Rumi Sadaf. I work as both a content writer and a programmer. In essence, I explain what I know and aid others in understanding programming concepts. Happy Viewing.

Leave a Comment