導航:首頁 > 編程語言 > 一行文字的編程題

一行文字的編程題

發布時間:2022-09-18 21:47:21

1. 編程題:輸入一行文字,分別統計出其中英文大寫字母、小寫字母、空格、數字和其它字元的個數。(用指針和

#include <stdio.h>
#include <string.h>
void fun(char *p);
int main(){
char values[100];
int i;
gets(values);
fun(values);
return 0;
}
void fun(char *p){
int i;
int daxie=0;
int xiaoxie=0;
int kongge=0;
int shuzi=0;
int other=0;
//英文大寫字母、小寫字母、空格、數字和其它字元
for(i=0;i<strlen(p);i++){
if(p[i]>='A'&&p[i]<='Z')
daxie++;
else if(p[i]>='a'&&p[i]<='z')
xiaoxie++;
else if(p[i]==' ')
kongge++;
else if(p[i]>='0'&&p[i]<='9')
shuzi++;
else other++;
}
printf("大寫字母:%d\n小寫字母:%d\n空格:%d\n數字:%d\n其他字元:%d\n",daxie,xiaoxie,kongge,shuzi,other);

}

2. c語言編程題:輸入一行字元,並以回車結束,將其中的小寫字母轉換成大寫字母,其他字元不變。

/*功能:輸入一行字元,並以回車結束,將其中的小寫字母轉換成大寫字母,其他字元不變。*/
/*說明:改變N的大小可以改變輸入字元串的范圍大小*/

#include<stdio.h>
#include<string.h>
#define
N
100

int
main(void)
{
int
i,len;
char
str[N];
printf("Please
input
a
string:
");
gets(str);
len
=
strlen(str);
for(i=0;i<len;i++)
{
if(str[i]
>=
97
&&
str[i]
<=
122)
str[i]
=
str[i]
-
32;
else
str[i]
=
str[i];
}
printf("The
result
is
:
");
puts(str);

return
0;
}

給分吧。。。嘿嘿!!!!

3. c++編程題,讓用戶輸入一行字元(空格分隔的多個單詞),然後輸出每一個單詞(每行一個單詞),求解

#include<iostream>

using namespace std;

void main() { char s[256],*p,*q; int b;

gets(s); p=s; while ( *p==' ' ) p++; //跳過前導空格

q=p;

while ( 1 ) {

b=0; while ( (*q!=' ')&&(*q!=0) ) q++;

if ( *q==0 ) b=1;

*q=0; q++; cout<<p<<endl;

if ( b ) break; else p=q;

}

}

4. c語言指針編程題:輸入一行字元,將其中的每個字元從小到大排列後輸出。

#include
#include

int main()
{
char str[5][80];
char temp[80];
int i,j;
printf("Please input 5 strings:\n");
for (i=0;i<5;i++)
{
gets(str[i]);
}
for (i=0;i<5;i++)
{
for (j=i+1;j<5;j++)
{
if (strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
}
printf("The strings:\n");
for (i=0;i<5;i++)
{
puts(str[i]);
}
return 0;
}
完美運行。好像我剛剛就做了一遍??
望採納!!

5. 編程題:從鍵盤輸入一行字元,分別統計字元中英文大寫,小寫,數字及其它字數的個數

在字元串後加'/'結束回車....
調試通過...
#include<stdio.h>
void main()
{
int i,zifu_x=0,zifu_d=0,shuzi=0,other=0;
char ch,p[100];
i=0;
printf("input strings(/ is end):");
do{
ch=getchar();
if(ch=='/')break;
p[i++]=ch;
}while(1);
p[i]=0;
i=0;
while(p[i++])
{
if(p[i-1]>'a'&& p[i-1]<'z')
{
zifu_x++;
continue;
}
if(p[i-1]>'A'&& p[i-1]<'Z')
{
zifu_d++;
continue;
}
if(p[i-1]>'0'&& p[i-1]<'9')
{
shuzi++;
continue;
}
other++;

}
printf("zifu_xiao=%d zifu_da=%d shuzi=%d other=%d",zifu_x,zifu_d,shuzi,other);
}

6. 編程題 2.輸入一行字元,分別統計出其中英文字母、空格、數字和其他字元的個數。

#include"stdio.h"
void main()
{
char c;
int Zifu=0,Shuzi=0,Kongge=0,Others=0;
while((c=getchar())!='\n')
{
('A'<=c&&c<='Z'||'a'<=c&&c<='z')&&++Zifu;
'0'<=c&&c<='9'&&++Shuzi;
c==' '&&++Kongge;
!(('A'<=c&&c<='Z'||'a'<=c&&c<='z')||('0'<=c&&c<='9')||c==' ')&&++Others;

}
printf("字元數量為:%d,數字數量為:%d,空格數量為:%d,其他字元數量為:%d",Zifu,Shuzi,Kongge,Others);
return 0;
}

更好看點呢就這樣
#include"stdio.h"
void main()
{
char c;
int Zifu=0,Shuzi=0,Kongge=0,Others=0;
while((c=getchar())!='\n')
{
('A'<=c&&c<='Z'||'a'<=c&&c<='z')&&++Zifu;
'0'<=c&&c<='9'&&++Shuzi;
c==' '&&++Kongge;
!(('A'<=c&&c<='Z'||'a'<=c&&c<='z')||('0'<=c&&c<='9')||c==' ')&&++Others;

}
printf("字元數量為:%d\n數字數量為:%d\n空格數量為:%d\n其他字元數量為:%d\n",Zifu,Shuzi,Kongge,Others);
return 0;
}

7. c語言編程:輸入一行字元,分別統計出其中英文字母,空格,數字和其他字元的個數,用while語句~~謝謝

#include <stdio.h>

int main()

{

int i=0, space=0, num=0, n=0, ch=0;

char s[20];

printf("請輸入一串字元 ");

gets(s);

while(s[i] != '')

{

if(s[i]==' ')

space++;

else if(s[i]<='9' && s[i]>='0')

num++;

else if(s[i]<='z' && s[i]>='a' || s[i]<='Z' && s[i]>='A')

ch++;

else

n++;

i++;

}

printf("剛才輸入的字元中英文字元個數為 %d ", ch);

printf("剛才輸入的字元中空格個數為 %d ", space);

printf("剛才輸入的字元中數字個數為 %d ", num);

printf("剛才輸入的字元中其他個數為 %d ", n);

return 0;

}

(7)一行文字的編程題擴展閱讀:

while 循環的格式:while (表達式){語句;}

while 循環的執行順序:當表達式為真,則執行下面的語句,語句執行完之後再判斷表達式是否為真,如果為真,再次執行下面的語句,然後再判斷表達式是否為真……就這樣一直循環下去,直到表達式為假,跳出循環。

例:

inta=NULL;

while(a<10){

a++;//自加

if(a>5)//不等while退出循環,直接判斷循環

{break;//跳出循環}

}

結果: 結束後 a的值為6 。

8. C語言題目輸入一行字元,分別統計出其中英文字母,空格,數字和其他字元的個數。

錯誤代碼:

if('a'<=nextchar<='z'||'A'<=nextchar<='Z')

else if('0'<=nextchar<='9')

修改後:

#include <stdio.h>

int main()

{

int letter=0,space=0,number=0,others=0;

char nextchar;

printf("Input your string ");

for(;nextchar!=' ';)

{

scanf("%c",&nextchar);

if('a'<=nextchar&&nextchar<='z'||'A'<=nextchar&&nextchar<='Z')

letter++;

else if(nextchar==' ')

space++;

else if('0'<=nextchar&&nextchar<='9')

number++;

else

others++;

}

printf("letter=%d,space=%d,number=%d,others=%d ",letter,space,number,others);

}

(8)一行文字的編程題擴展閱讀

c++輸入一行字元,分別統計出其中英文字母、空格、數字和其他字元的個數。

#include<cstdio>

int main()

{

char x[999];

int i,a=0,b=0,c=0,d=0;

gets(x);

for(i=0;i<=x[i];i++)

{

if('A'<=x[i]&&x[i]<='z')

a++;

else if('0'<=x[i]&&x[i]<='9')

b++;

else if(x[i]==' ')

c++;

else

d++;

}

printf("%d %d %d %d ",a,b,c,d);

return 0;

}

9. c語言編程題 .鍵盤輸入一行字元,每一個字元存放在一個鏈表節點,按反序把這行字元列印出來

#include<stdio.h>
#include<string.h>
void printit(char *str,int length) //返序輸出函數
{

int i;
for(i=length-1;i>=0;i--) //從給定的字元串的最後一位依次向前遍歷各字元
putchar(*(str+i)); //每向前一個字元即列印該字元,直至第一個字元為止。
}
int main()
{ char str[80]=""; //定義一個長度為80位元組的字元串數組,並初始化
gets(str); //從鍵盤中輸入一個字元串(遇回車鍵結束)
printit(str,strlen(str)); //調用上面定義的函數反序輸出字元串
printf("\n"); //輸入一個回車換行符,使後續輸出能另起一行
return 0;
}

10. 編程題: 輸入一行字元,分別統計出其中英文字母,空格,數字和其他字元的個數。(分別使用wh

1 while語句:

#include<stdio.h>
int main(void)
{
//輸入一行字元,分別統計出其中英文字母、空格、數字和其他字元的個數。
char ch;
int char_num=0,kongge_num=0,int_num=0,other_num=0;
while((ch=getchar())!='\n')//回車鍵結束輸入,並且回車符不計入
{
if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
{
char_num++;
}
else if(ch==' ')
{
kongge_num++;
}
else if(ch>='0'&&ch<='9')
{
int_num++;
}
else
{
other_num++;
}
}
printf("字母= %d,空格= %d,數字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);
return 0;
}

2 ,do while語句:

#include<stdio.h>
int main(void)
{
//輸入一行字元,分別統計出其中英文字母、空格、數字和其他字元的個數。
char ch;
int char_num=0,kongge_num=0,int_num=0,other_num=0;
do
{
if(ch>='a'&&ch<='z'||ch<='z'&&ch>='a')
{
char_num++;
}
else if(ch==' ')
{
kongge_num++;
}
else if(ch>='0'&&ch<='9')
{
int_num++;
}
else
{
other_num++;
}
} while((ch=getchar())!='\n')//回車鍵結束輸入,並且回車符不計入
printf("字母= %d,空格= %d,數字= %d,其它= %d\n",char_num,kongge_num,int_num,other_num);
return 0;
}

閱讀全文

與一行文字的編程題相關的資料

熱點內容
什麼樣的app上買機票最便宜 瀏覽:985
安卓如何查看異常重啟 瀏覽:715
解壓音樂排名 瀏覽:383
安卓手機瀏覽器怎麼掃二維碼 瀏覽:715
通達信成本均線源碼 瀏覽:614
可以下載的解壓音頻 瀏覽:564
海賊王怎麼換伺服器 瀏覽:318
計算機上的共享文件夾映射 瀏覽:940
榮耀安裝包在文件夾哪裡 瀏覽:196
機票php源碼 瀏覽:233
linux共享mac 瀏覽:924
中國沒有國外的伺服器地址 瀏覽:759
為什麼退款伺服器連接錯誤 瀏覽:557
android簡訊存儲位置 瀏覽:972
unix網路編程卷4 瀏覽:808
找靚機app下單什麼時候發貨 瀏覽:413
android一個應用兩個進程 瀏覽:803
linux硬碟復制 瀏覽:808
php圖片伺服器搭建 瀏覽:801
下載壓縮文件怎麼打開 瀏覽:194