This program takes a string input from the user and converts all the lowercase characters in the string to uppercase characters.
#include<stdio.h>
#include<string.h>
int main()
{
char some_string[100];
int x;
printf("Enter the String:");
scanf("%s", some_string);
for(x = 0; x <= strlen(some_string); x++)
{
if (some_string[x] >= 97 && some_string[x] <= 122)
some_string[x] = some_string[x] - 32;
}
printf("\nUppercase is: %s", some_string);
return 0;
}
Result:
Enter the String:something
Uppercase is: SOMETHING
Process returned 0 (0x0) execution time : 4.734 s
Press any key to continue.
Here is a block by block explanation of the code:
#include <stdio.h>
#include <string.h>
int main()
{
This code block includes the necessary header files and declares the main function.
char some_string[100];
int x;
This declares a character array to store the input string and an integer variable to iterate over the string.
printf("Enter the String:");
scanf("%s", some_string);
This prompts the user to enter a string and reads the input string into the some_string
character array.
for(x = 0; x <= strlen(some_string); x++)
{
if (some_string[x] >= 97 && some_string[x] <= 122)
some_string[x] = some_string[x] - 32;
}
This loop iterates through the input string and converts any lowercase characters to uppercase. The strlen()
function is used to determine the length of the string. The if
statement checks whether the character is a lowercase letter by comparing its ASCII code to the values for lowercase letters. If it is, it is converted to uppercase by subtracting 32 from its ASCII code.
printf("\nUppercase is: %s", some_string);
return 0;
}
Finally, this code block prints the converted string to the console and returns 0 to indicate successful program execution.
No comments:
Post a Comment