當前位置:首頁 » 編程語言 » c語言輸出字元中空格個數
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言輸出字元中空格個數

發布時間: 2022-12-07 03:04:37

A. 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())!=' ';)

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 ",letters,blanks,digits,others);

return 0;

}

B. 輸入一個字元串,統計出其中空格的個數 C語言

思路:統計字元串中的空格,所以該字元串中有空格,則輸入只能使用gets函數,再依次遍歷該字元串,判斷字元是否是空格,如果是,則空格個數自加1。

參考代碼:

#include<string.h>
#include<stdio.h>
#include<math.h>
intmain()
{
intsum=0,i;
chara[100];
gets(a);
for(i=0;a[i]!='';i++)
if(a[i]=='')
sum++;
printf("%d ",sum);
return0;
}
/*
輸出:
afadfasfd
4
*/

C. C語言,編寫程序,分別計算所輸入的字元中空格,換行符的個數,字元串以字元z為結束符號

#include<stdio.h>
intmain()
{
charc;
intspace=0;
intline_break=0;
while((c=getchar())!='z')
{
if(c=='')space++;
elseif(c==' ')line_break++;
}
printf("空格數:%d,換行符數:%d ",space,line_break);
return0;
}

D. 用c語言統計輸入空格字元數字的個數

結束的回車被計算了一次,才到while那裡判斷並推出。
改為:
void
main()
{int
x=0,c=0,b=0,n=0,k=0;
x=getchar();
while
(x!='\n')
{
if
(48<=x&&x<=57)
c++;
else
if
(65<=x&&x<=90||97<=x&&x<=122)
k++;
else
if
(x==32)
b++;
else
n++;
x=getchar();
}
printf("共有%d個數字\n%d個字母\n%d個字元\n%d個空格",c,k,n,b);
}

E. 輸入一串字元,計算其中空格的個數,用C語言

#include <stdio.h>
#include <string.h>
int main(){
char a[999];
int i,n=0;
gets(a);
for(i=0;i<strlen(a);i++){
if(a[i]==' ')
n++;
}
printf("%d個空格",n);
return 0;
}

F. C語言 編譯函數 輸入一段字元,求其中空格的個數

貌似幾個方面有問題:
1.接收有空格的字元串用gets(str);
scanf遇到空格自動結束接收。
2.scanf語句中,數組名就是它的地址,所以不要加&。
3.兩個函數中,getchar();這句是多餘的。
4.後一個函數中,for語句有問題(當你輸入的字元中有0時會出錯)。
5.下面代碼可以供你參考。
代碼一:
#include<stdio.h>
void main()
{
char str[30];
int i,a=0;
printf("Input string:\n");
gets(str);
for(i=0;str[i]!='0';i++)
{
if (str[i]==' ')
a++;
}
printf("%d\n",a);
}
代碼二:
#include<stdio.h>
void main()
{
char str[30];
int i,t,b=0;
printf("Input string:\n");
gets(str);
t=strlen(str);
for(i=0;i<t;i++)
{
if (str[i]>='a'&& str[i]<='z')
b++;
}
printf("%d\n",b);
}

G. c語言編程,輸入一串字元,統計其中字元個數、數字個數、空格個數與其他符號個數,並輸出對應的個數值。

#include<stdio.h>
#include<string.h>
main()
{chara[1000];
inti,l,m1,m2,m3,m4;
while(1){
gets(a);
m1=0;m2=0;m3=0;m4=0;
l=strlen(a);
for(i=0;i<l;i++){
if((a[i]>='a'&&a[i]<='z')||(a[i]>='A'&&a[i]<='Z'))m1++;
elseif(a[i]>='0'&&a[i]<='9')m2++;
elseif(a[i]=='')m3++;
elsem4++;
}
printf("字元%d個 數字%d個 空格%d個 其他%d個 ",m1,m2,m3,m4);
}
}

如圖所示,望採納。。。。。。

H. C語言,輸入一段字元,輸出字母個數,空格個數,

#include<stdio.h>
intmain()
{
inti,b,c;
chara;
i=0;
b=0;
c=0;
a=getchar();
while(a!=' ')//是a!=' '
{
if('z'>=a&&'a'<=a||'Z'>=a&&'A'<=a)i=i+1;
elseif(a=='')b=b+1;//是==
elsec=c+1;
a=getchar();
}
printf("字母數為%d,空格數為%d,其他字元數為%d ",i,b,c);
return0;
}

I. 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);

}

(9)c語言輸出字元中空格個數擴展閱讀

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;

}

J. C語言求助,從鍵盤輸入的字元中統計並輸出空格的個數,用換行符結束循環

#include <stdio.h>
void main()
{
int count = 0;
char c;
while((c = getchar()) && c != '\n')
{
if(c == ' ')count ++;
}
printf("count = %d", count);
}