we have to declare in c a separate function that convert upper in lowercases and count the uppercases which where converted, but i can´t find my mistake..
#include <stdio.h>
#include <string.h>
char umwandlung(char text)
{
int n, upper=0;
if (text >= 65 && text <= 90)//upper in lowercases
{
text = text 32;
}
I´m not sure if my declaration to count the converted uppercase is right, i tried to copy and apply it to my code.. but it won't work
for (n=0; text[n]!=0; n )
{
if (text[n] >= 'A' && text[n] <= 'Z')
{
upper ;
}
}
printf("\n%i Buchstaben wurden geandert\n",upper);
return text;
}
int main(void)
{
char satz[80];
int i, x, upper=0, n;
printf("\ngross in klein \n");
printf("Bitte geben Sie einen Satz mit max. 80 Zeichen ein:\n");
gets(satz);
x = strlen(satz);
for (i = 0; i <= x; i )
{
satz[i] = umwandlung(satz[i]);
}
printf("\n%s\n",satz);
}
CodePudding user response:
The function can be declared and defined the following way.
#include <ctype.h>
size_t to_lower_case( char *s )
{
size_t n = 0;
for ( ; *s; s )
{
if ( isupper( ( unsigned char )*s ) )
{
*s = tolower( ( unsigned char )*s );
n;
}
}
return n;
}
and called like
size_t n = to_lower_case( satz );
Pay attention to that the function gets
is unsafe and is not supported by the C Standard. Instead use the function fgets
or scanf
. Gor example
scanf( "y[^\n]", satz );
CodePudding user response:
Your implementation doesn't work because your function has the signature:
char umwandlung(char text)
. This means that your function receives a char as a parameter and returns a char. But your function needs the address of the string that you want to change and to return an int
because I think your string could be larger, so it should be something like: int umwandlung(char *text)
Another problem is that you call your function for every character in that string so in this way you can't change anything in that string, maybe only to count the uppercase.
I will put below an implementation starting for your example:
#include <stdio.h>
#include <string.h>
int umwandlung(char *text)
{
int i, upper=0;
for(i = 0; i < strlen(text); i ) {
if(text[i] >= 65 && text[i] <= 90) {
text[i] = 32;
upper ;
}
}
return upper;
}
int main(void)
{
char satz[80];
int i, x, upper=0, n;
printf("\ngross in klein \n");
printf("Bitte geben Sie einen Satz mit max. 80 Zeichen ein:\n");
fgets(satz, 80, stdin);
upper = umwandlung(satz);
printf("\n%i Buchstaben wurden geandert\n",upper);
printf("\n%s\n",satz);
}
Don't use gets
function anymore, it's already deprecated.