C++语言输入n行字符串,分别统计每行字串中的字母、数字字符个数。

作者&投稿:晏颖 (若有异议请与网页底部的电邮联系)
c语言输入一个数n,然后输入n行字符串,分别输出每个字符组中数字的个数~


#include #include int main(){int n;char str[200];char* cp;int i;int* ip;scanf("%d ", &n);ip=(int*)malloc(n*sizeof(int));for (i=0; i='0'&&*cp<='9')++ip[i];++cp;}}for (i=0; i<n;++i){printf("字符串%d中有%d个数字
", i+1, ip[i]);}free(ip);return 0;}

错误代码:
if('a'<=nextchar<='z'||'A'<=nextchar<='Z')
else if('0'<=nextchar<='9')
修改后:
#include
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);
}

扩展资料
c++输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数。
#include
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;
}

参考代码如下:

#include <iostream> 
using namespace std; 
int main () 

int i,n,cn,ca;
char t,str[100];
cin>>n;
while(n--){
cin>>str;
i=0;
cn=ca=0;
while((t=str[i])!=0){
if(t>='a'&&t<='z'||t>='A'&&t<='Z')
++ca;
else if(t>='0'&&t<='9')
++cn;
++i;
}
cout<<"字母有"<<ca<<"个  数字有"<<cn<<"个"<<endl;
}

}



C语言里如何控制输入多行,例如 输入数据有多行 第一行是数据N 下面是...
使用一个计数器,赋值为N,再在循环结构(for或者while或者do while)中调用scanf即可实现多行输入.或者是采用回调函数,实现自我调用,再使用N来控制调用次数,不过此方法有些变态.---珂昵贝儿

c语言定义了一个二维字符数组怎么赋值,要求输入n表示n行,然后每行输入...
首先用gets()取得字符串,然后用strcpy()函数进行赋值。我的编译器没有VLA的功能,所以我定义了4行,有这个功能的话把4改成n就可以 include<stdio.h> include<string.h> int main(void){ int i;char string[4][100];char str[100];for(i=0;i<4;i++){ gets(str);strcpy(string[i],...

c语言输入一个整数n,输出这n行"生蛋快乐",光标换行
思路:定义一个字符串并初始为“生蛋快乐”,接着while循环输出该字符串。参考代码:include <stdio.h>int main(){ char a[20]="生蛋快乐";\/\/定义并初始化字符串 int n; scanf("%d",&n);\/\/输入整数n,表示需要输出多行"生蛋快乐" while(n--)\/\/while循环 puts(a);\/\/...

输入自然数n,输出n行*,每行的字符依次递增.C语言,求解
include<stdio.h>void showStar(const int &star_num){ int i,j; for(i=0;i<star_num;i++) { for(j=0;j<star_num+1;j++) printf("*"); printf("\\n"); }}void main(){ int line_num; printf("Please input number of lines:\\n"); scanf("...

怎样用C语言输出一个整数n打印字符图形,总共n行,每行n个*组成平行四边...
include <stdio.h> include <math.h> int main(void){ int i,j,n=0;scanf("%d", &n);for (i = 0; i < n; i++){ for (j = 0; j < i; j++){ printf(" ");} for (j = 0; j < n; j++){ printf("*");} printf("\\n");} return 0;} ...

打印输出n行杨辉三角形,n值,由键盘输入.怎么编写c语言代码
include <stdio.h> int main(){ int yh[101][101]={{},{0,1}},i,n,m;scanf("%d",&m); \/*输入要打印的行数,,,不能太大。。我水平不高。。*\/ for(i=2;i<=m;i++){ yh[i][1]=yh[i][i]=1;for(n=2;n<i;n++){ yh[i][n]=yh[i-1][n-1]+yh[i-1][...

C语言:从键盘输入n(n=3,5,7,9),屏幕输出nxn矩阵,且每行每列及两对角线...
考虑到空间效率,代码用动态二维数组解决——代码文本:include "stdio.h"include <stdlib.h> int main(int argc,char *argv[]){ char *q,**p,n,i,j,k,t;int sum;printf("Enter n(int n=3,5,7,9)...\\nn=");if(scanf("%d",&n)==1 && (n==3 || n==5 || n==7 || ...

c语言编写代码,怎么输入若干行?
如果是输入三行三列的话,这样就可以了:源代码如下:include<stdio.h>void main(){ int A[3][3],n,m,Logo,i,j,k;int pd(int a,int b,int c);for(n=0;n<3;n++)for(m=0;m<3;m++)scanf("%d",&A[n][m]);for(n=0;n<3;n++){Logo=pd(A[n][0],A[n][1],A[...

C语言:输入\\n表示结束运行怎么用呀?
换行符在键盘上就是enter键,提示中的'\\n'是用来让你在程序中判断是否结束输入的 说的具体点就是 在程序中有这样一个循环体 char c;do { scanf("%c",&c);...}while(c!='\\n');\/*判断是否是换行符*\/ 但是在运行的时候,换行符输入,还是用的enter键 ...

c语言怎么写第一行是一个整数N,代表有N组测试数据,接下来是N行,每行有...
输入n 换行;for循环{ 利用随机函数获取两个整数\\n;} include <stdio.h> int main(){ int i,N;scanf("%d",&N);int *t=new int[N];for(i=0;i<N;i++)scanf("%d",&t[i]);for(i=0;i<N;i++)if(t[i]%2==1)printf("YES!\\n");else printf("NO!\\n");return 0;} ...

当涂县19454054175: C++语言输入n行字符串,分别统计每行字串中的字母、数字字符个数. -
晁贱易贝: 参考代码如下:#include <iostream> using namespace std; int main () { int i,n,cn,ca; char t,str[100]; cin>>n; while(n--){ cin>>str; i=0; cn=ca=0; while((t=str[i])!=0){ if(t>='a'&&t<='z'||t>='A'&&t<='Z') ++ca; else if(t>='0'&&t<='9') ++cn; ++i; } cout<<"字母有"<<ca<<"个 数字有"<<cn<<"个"<<endl; } }

当涂县19454054175: c++输入一行字符串,要求分别统计出其中英文大写字母、小写字母、数字、空格以及其他字符的个数.谁会啊? -
晁贱易贝: #include void main() { char sen[256]; int ben=0,men=0,spa=0,num=0,oth=0; int i; gets(sen); for(i=0;i<(int)strlen(sen);i++) { if(sen[i]>='a'&&sen[i]<='z') { ben++; } else if(sen[i]>='a'&&sen[i]<='z') { men++; } else if(sen[i]>=' ') { spa++; } else if(sen[i]>='0'&...

当涂县19454054175: c++编程实现输入一串字符,分别统计数字字符、大、小写字母、其它字符的个数 -
晁贱易贝: 遍历一次就够了,核心伪代码如下: while(没到字符串尾) { if(数字字符){数字字符数++;} else if(大写字母){大写字母数++;} else if(小写字母){小写字母数++;} else {其它字符数++;} }

当涂县19454054175: 用C++编程:编程输入字符串分别统计字符串中所包含的各个不同的字符及其各个字符的数量 -
晁贱易贝: 输入一个字符串,各个单词以空格分隔,然后按照Pascal命名法输出 输入:baidu空格com空格imAGes 结果:BaiduComImages 用C#如何编写代码,求教!!!

当涂县19454054175: c++输入一行字符,分别统计出其中英文字母,空格,数字字符和其它字符的个数. -
晁贱易贝: #include <iostream> using namespace std; int main() { char c; int el=0,sp=0,nu=0,other=0;while(cin.get(c)) { if(c=='\n') break; if((c>='A' && c<='Z')||(c>='a' && c<='z')) el++; else if(c>='0'&&c<='9') nu++; else if(c==' ') sp++; elseother++; } cout<<"英...

当涂县19454054175: c++:输入一串字符,分别统计出各字符出现的频率.例如: 输入This is a pencil. -
晁贱易贝: 我用的方法可能有的特别,不懂得问哈#include <iostream>#include <string> using namespace std; int main() { int arr[26][2]; int i=0,j=0; char str[100]; int strLen=0; arr[0][0]=65; //2维数组,1维存26个字符,1维存26个字符的数量 arr[0][1]=0; for(i=1;i...

当涂县19454054175: C ++ 输入一行字符,分别统计出其中英文字母,空格,数字和其它字符个数 -
晁贱易贝: #include<stdio.h>#include<string.h> void main() { int i,j,k; int chars=0,nums=0,spaces=0,elses=0; char a[100]=" ";printf("请输入一字符串:\n"); gets(a); for(i=0;i<strlen(a);i++) if(a[i]>='A'&&a[i]<='Z')chars++; else if(a[i]>='a'&&a[i]<='z')chars+...

当涂县19454054175: 用c++设计一程序,输入一串字符,分别统计出其中英文字母、空格、数字和其他字符的个数并打印统计结果. -
晁贱易贝: public static void main(String[] args) { int ch=0; int nu=0; int blank=0; int ot=0; //显示一个要求用户键入 String 的对话框://String inputValue = JOptionPane.showInputDialog("Please input a value"); String st = JOptionPane.showInputDialog(...

当涂县19454054175: c++ 输入一行字符,分别统计出其中 -
晁贱易贝: 应该和c=getchar()的返回值有关,只要读取字符成功,就执行完毕,(c=getchar())!='\n')是指读到''\n',也就是换行才会执行完毕.

当涂县19454054175: 用C++编写:输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数? -
晁贱易贝: #include<stdio.h>#include<stdlib.h> void main() { int letter=0,blank=0,num=0,other=0; //分别定义为字母,空格,数字,其他的字符个数 char* str=(char*)malloc(10); printf("input a string\n"); gets(str); while(*str!='\0') { if('a'<=*str<'z' || 'A'<=*str<'Z') ...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网