本文目錄一覽:
- 1、C語言編程:從鍵盤中輸入一個英文字符串
- 2、C語言編程:輸入一行字符,輸出其中英文字母、空格、數字和其它字符的個數?
- 3、c語言:輸入一行英文字符串,把每個單詞第一個字母變為大寫,輸出修改後的字符串
- 4、c語言怎樣才能輸入一行字符,以回車作為結束標誌,分別統計出大寫字母,小寫字母,空格,數字和其他字符?
C語言編程:從鍵盤中輸入一個英文字符串
#includestdio.h
#includestdlib.h
int main()
{
int strSize = 100;
char *str = (char *)malloc(sizeof(char) * strSize);
int charNum = 0;
char input;
//逐個字符輸入字符串,可以輸入int可以表示的最大值個字符
printf(“請輸入任意個字符:\n”);
while(true)
{
scanf(“%c”,input);
if(input != ‘#’)
{
if((input = ‘A’ input = ‘Z’) || (input = ‘a’ input = ‘z’))
{
if(charNum strSize)
{
strSize += 100;
str = (char *)realloc(str,strSize);
}
str[charNum] = input;
charNum++;
}
}
else
{
break;
}
}
//輸入結果分析
int i = 0,j = 0;
char *tempChar = (char *)malloc(sizeof(char) * charNum);
int *tempCharNum = (int *)malloc(sizeof(int) * charNum);
int charType = 0;
bool exist = false;
for(i = 0; i charNum; i++)
{
exist = false;
tempChar[i] = ‘#’;
tempCharNum[i] = 0;
for(j = 0; j charNum; j++)
{
if(tempChar[j] == ‘#’)
{
break;
}
if(tempChar[j] == str[i])
{
exist = true;
tempCharNum[j] += 1;
}
}
if(exist == false)
{
tempChar[charType] = str[i];
tempCharNum[charType] = 1;
charType++;
}
}
int t1;
char t2;
for(j = 0; j charType – 1; j++)
{
for(i = 0; i charType; i++)
if(tempCharNum[i] tempCharNum[i+1])//如果a[i]大於a[i+1]
{
//交換a[i]和a[i+1]的值,即把較大的元素往後排
t1 = tempCharNum[i];
tempCharNum[i] = tempCharNum[i+1];
tempCharNum[i+1] = t1;
t2 = tempChar[i];
tempChar[i] = tempChar[i+1];
tempChar[i+1] = t2;
}
}
for(i = 0; i charNum; i++)
{
if(tempChar[i] != ‘#’)
{
printf(“單詞:%c,次數:%d\n”,tempChar[i],tempCharNum[i]);
}
}
free(str);
free(tempChar);
free(tempCharNum);
return 0;
}
C語言編程:輸入一行字符,輸出其中英文字母、空格、數字和其它字符的個數?
#include stdio.h
int isletter(char c)
{
return c=’a’c=’z’||c=’A’c=’Z’;
}
int isdigit(char c)
{
return c=’0’c=’9′;
}
int isblank(char c)
{
return c==’ ‘;
}
int main()
{
char c;
int letters,digits,blanks,others;
for(letters=digits=blanks=others=0;(c=getchar())!=’\n’;)
if(isletter(c))
letters++;
else if(isdigit(c))
digits++;
else if(isblank(c))
blanks++;
else
others++;
printf(“letters:%d blanks:%d digits:%d others:%d\n”,letters,blanks,digits,others);
return 0;
}
c語言:輸入一行英文字符串,把每個單詞第一個字母變為大寫,輸出修改後的字符串
#includelt;ctype.hgt;
#includelt;string.hgt;
#includelt;stdio.hgt;
int main(int argc,char*argv[])
{
char str[100+1];
int isfirst=1;
char ch;
int i=0;
while((ch=getchar())!=EOF)
{
if(isalpha(ch))
{
if(isfirst==1)
{
ch=toupper(ch);
isfirst=0;
}
}
else
{
isfirst=1;
}
str[i++]=ch;
}
strlt;igt;=’\0′;
printf(“%s\n”,str);
return 0;
}
擴展資料:
printf用法:
printf()函數的調用格式為:printf(“lt;格式化字符串gt;”,lt;參量表gt;)。
其中格式化字符串包括兩部分內容:一部分是正常字符,這些字符將按原樣輸出;另一部分是格式化規定字符,以”%”開始,後跟一個或幾個規定字符,用來確定輸出內容格式。
參量表是需要輸出的一系列參數,其個數必須與格式化字符串所說明的輸出參數個數一樣多,各參數之間用”,”分開,且順序一一對應,否則將會出現意想不到的錯誤。
比如:
int a=1234;
printf(“a=%d\n”,a);
輸出結果為a=1234。
scanf()是C語言中的一個輸入函數。與printf函數一樣,都被聲明在頭文件stdio.h里,因此在使用scanf函數時要加上#includelt;stdio.hgt;。
int scanf(const char*restrict format,…);
函數scanf()是從標準輸入流stdin(標準輸入設備,一般指向鍵盤)中讀內容的通用子程序,可以說明的格式讀入多個字符,並保存在對應地址的變量中。
如:
scanf(“%d%d”,a,b);
函數返回值為int型,如果a和b都被成功讀入,那麼scanf的返回值就是2。
c語言怎樣才能輸入一行字符,以回車作為結束標誌,分別統計出大寫字母,小寫字母,空格,數字和其他字符?
C代碼和運行結果如下:
統計結果正確,望採納~
附源碼:
#include stdio.h
int main() {
char s[100];
fgets(s, 100, stdin); // 輸入一行字符,包括行尾的’\n’
int i = 0, upper = 0, lower = 0, space = 0, digit = 0, other = 0;
while (s[i] != ‘\n’) {
if (s[i] = ‘A’ s[i] = ‘Z’)
upper++;
else if (s[i] = ‘a’ s[i] = ‘z’)
lower++;
else if (s[i] == ‘ ‘)
space++;
else if (s[i] = ‘0’ s[i] = ‘9’)
digit++;
else
other++;
i++;
}
printf(“大寫字母:%d, 小寫字母:%d, 空格:%d, 數字:%d, 其他:%d\n”,
upper, lower, space, digit, other);
return 0;
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/242871.html