Problem 48

author
By Supreme2023-03-15
Problem Statement :
Write a function that will scan a character string passed as an argument and convert all lowercase character into their uppercase equivalents
Code:

#include <stdio.h>
#include <ctype.h>

void convertToUpper(char *str);

/*
Write a function that will scan a character string passed as an argument and convert all
lowercase character into their uppercase equivalents.
*/

int main() {
char str[100];
printf("\tUppercase Converter");
printf("\n====================================\n\n");
printf("Enter String : ");
gets(str);

printf("\n------------------------------------\n\n");
printf("Original string: %s\n", str);
convertToUpper(str);
printf("\nConverted string: %s\n", str);
printf("\n------------------------------------\n");

return 0;
}

void convertToUpper(char *str) {
int i = 0;
while (str[i] != '\0') {
if (islower(str[i])) {
str[i] = toupper(str[i]);
}
i++;
}
}

Description :
  • This is a program that converts lowercase characters in a string to uppercase characters using a function.
  • The program starts by declaring a function called convertToUpper that takes a char pointer as its argument.
  • In the main function, the user is prompted to enter a string and the gets function is used to read the input string.
  • The convertToUpper function is then called, passing in the input string as its argument.
  • Inside the convertToUpper function, a loop is used to iterate through each character of the input string.
  • For each character in the string, the islower function is used to check if it is a lowercase letter.
  • If the character is a lowercase letter, the toupper function is used to convert it to an uppercase letter.
  • Finally, the updated string is printed to the console using the printf function in the main function.
Output Image:
Output-image

© Copyright 2022. All right reserved, GTU Coder.