I'm trying to create a function that will identify whether the first letter input is upper or lower case then output the rest of the string in that same case(upper/lower).
For example, "Hi there"
would become "HI THERE"
.
I'm not familiar with fgets
. Once I run it I can input and press enter and the program doesn't run. I'm not getting any compiler errors. I believe I went wrong in the void shift function.
Also, I know gets
is not recommended, is fgets
similar? Or is it better to use scanf
?
#include <stdio.h>
#include <ctype.h>
void shift (char *my_string); // Function declaration
int main()
{
char inputstring[50];
printf("Enter a string\n");
char *my_string = inputstring;
shift(my_string); // Function
}
void shift (char *my_string) // Function definition
{
int i =0;
char ch;
for(i=0; i<50; i++)
fgets(my_string, 50, stdin);
while(my_string[i])
{
if(ch>='A' && ch<= 'Z') // When first char is uppercase
{
putchar (toupper(my_string[i]));
i++;
}
else if (ch>='a' && ch <= 'z') // When first char is lowercase
{
putchar(tolower(my_string[i]));
i++
}
}
return;
}
You don't need to call fgets()
fifty times. It reads a line from stdin
and writes it to my_string
. It seems you only want to read one line, not fifty (and keep only the last one). The 50 is the maximum number of characters (minus one) that will be read and written to the buffer. This limit is to prevent buffer overflow. See fgets().
Try removing the for
loop on the line before the fgets()
call. Also, you don't need the my_string
in main()
. The corrected code:
#include <stdio.h>
#include <ctype.h>
void shift (char *my_string);//function declaration
int main()
{
char inputstring[50];
printf("Enter a string\n");
shift(inputstring);
}
void shift (char *my_string) //function definition
{
int i;
char ch;
if ( fgets(my_string, 50, stdin) == NULL )
return;
ch = my_string[0];
for ( i=0; my_string[i]; i++ )
{
if(ch>='A' && ch<= 'Z') //when first char is uppercase
{
putchar (toupper(my_string[i]));
}
else if (ch>='a' && ch <= 'z')//when first char is lowercase
{
putchar(tolower(my_string[i]));
}
}
return;
}
Edit: Added ch
initialization, pointed out by @thurizas. Changed while
loop to for
loop. Added check to return value of fgets()
as suggested by @JonathanLeffler. (See his comment about the buffer size.)