我們必須在 ca 中宣告單獨的函式,將大寫轉換為小寫并計算轉換的大寫,但我找不到我的錯誤..
#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;
}
我不確定我對轉換后的大寫進行計數的宣告是否正確,我嘗試復制并將其應用于我的代碼..但它不起作用
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);
}
uj5u.com熱心網友回復:
可以通過以下方式宣告和定義該函式。
#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;
}
并稱之為
size_t n = to_lower_case( satz );
注意該函式gets是不安全的,不受C標準支持。而是使用函式fgetsor scanf。戈爾示例
scanf( "y[^\n]", satz );
uj5u.com熱心網友回復:
您的實作不起作用,因為您的函式具有簽名:
char umwandlung(char text). 這意味著您的函式接收一個字符作為引數并回傳一個字符。但是您的函式需要您想要更改的字串的地址并回傳一個,int因為我認為您的字串可能更大,所以它應該是這樣的:int umwandlung(char *text)
另一個問題是您為該字串中的每個字符呼叫您的函式,這樣您就無法更改該字串中的任何內容,也許只能計算大寫字母。
我將在下面為您的示例提供一個實作:
#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);
}
不要再使用gets函式了,它已經被棄用了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/367254.html
上一篇:C一些頭檔案相互包含
下一篇:移位右側運算元型別
