Explain how this code works. #include<stdio.h> void upper_string(char*); int mai
ID: 3815079 • Letter: E
Question
Explain how this code works.
#include<stdio.h>
void upper_string(char*);
int main()
{
char string[2000];
int con=1;
while(con == 1)
{
// reading entire line in to char array string
printf("Enter a sentence to convert it into upper case ");
scanf(" %[^ ]",string);
// function to convert lower case sentence to upper case
upper_string(string);
printf(" Entered sentence in upper case is "%s" ", string);
printf(" Press 1 to Continue or any other number to exit...... ");
scanf("%d",&con);
}
return 0;
}
void upper_string(char *string)
{
while(*string)
{
if ( *string >= 'a' && *string <= 'z' )
{
*string = *string - 32;
}
string++;
}
}
Explanation / Answer
The main part of code reside at here:
void upper_string(char *string)
{
while(*string)
{
if ( *string >= 'a' && *string <= 'z' )
{
*string = *string - 32;
}
string++;
}
As you know that this code convert the lower case string to upper case
Here is explaination of each line in upper_string()
here string is pointer variable
void upper_string(char *string)
{
// loop each character in string
while(*string)
{
// check if character is lower case
if ( *string >= 'a' && *string <= 'z' )
{
// convert the character to upper case
*string = *string - 32;
}
// incrementing the string to point to next character
string++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.