字符数组

【1】把输入的字符按照反着顺序输出

输入:
I am a boy.
输出:
.yob a ma I

网上复制来的,应该用到了指针,看不懂。

#include<stdio.h>
#include<string.h>
int main()
{
    int i,s;char a[100];
    while(gets(a)!=NULL)
    {
    	s=strlen(a);
    	for(i=s-1;i>=0;i--)
    	{
	    	printf("%c",a[i]);
	    }
	    printf("\n") ;
    }
    return 0;
}

以下是自己照着书写的,一个一个取键盘上的字母,运行没发现错误,但oj报答案错误ac0% 

#include <stdio.h>

int main() {
	int i, k = 0;
	char line[100];
	while ((line[k] = getchar()) != '\n') {
		k++;
	}
	line[k] = '\0';

	for (i = k ; i >= 0; i--) {
		printf("%c", line[i]);
	}
	printf("\n");
	return 0;
}

[2]输入一串字符,统计这串字符里的字母个数,数字个数,空格字数以及其他字符(最多不超过100个字符)

样例输入 Copy

I am a student in class 1.
I think I can!

样例输出 Copy

18 1 6 1
10 0 3 1

 自己写的,答案错误

#include <stdio.h>

int main() {
	int i, k = 0, a = 0, b = 0, c = 0, e = 0;
	char line[100];
	while ((line[k] = getchar()) != '\n') {
		k++;
	}
	line[k] = '\0';
	for (i = 0; i < k; i++) {
		if (line[i] == ' ') {
			a++;
		} else if ('a' <= line[i] && line[i] <= 'z' || 'A' <= line[i] && line[i] <= 'Z') {
			b++;
		} else if ('1' <= line[i] && line[i] <= '9') {
			c++;
		} else {
			e++;
		}
	}
	printf("%d %d %d %d\n", b, c, a, e);
	return 0;
}

 运行错误

#include <stdio.h>
#include <string.h>

int main() {
	int i, s, a = 0, b = 0, c = 0, e = 0;
	char line[100];
	while (gets(line) != NULL) {
		s = strlen(line);
		for (i = 0; i < s; i++) {
			if (line[i] == ' ') {
				a++;
			} else if ('a' <= line[i] && line[i] <= 'z' || 'A' <= line[i] && line[i] <= 'Z') {
				b++;
			} else if ('1' <= line[i] && line[i] <= '9') {
				c++;
			} else {
				e++;
			}
		}
		printf("%d %d %d %d\n", b, c, a, e);
	}

	return 0;
}

找到原因了,是大于等于0,不是大于等于1!

#include <stdio.h>
#include <string.h>

int main() {


	char line[100];
	while (gets(line) != NULL) {
		int i, s, a = 0, b = 0, c = 0, e = 0;
		s = strlen(line);
		for (i = 0; i < s; i++) {
			if (line[i] == ' ') {
				a++;
			} else if (('a' <= line[i] && line[i] <= 'z' )||('A' <= line[i] && line[i] <= 'Z') ) {
				b++;
			} else if ('0' <= line[i] && line[i] <= '9') {
				c++;
			} else {
				e++;
			}
		}
		printf("%d %d %d %d\n", b, c, a, e);
	}

	return 0;
}

[3]输入

One line, the ACMer's name. The name is at most 8 characters.

输出

You should output one line like the output sample. Output "I am" then the acmer's name, then ",yes,I can!"

样例输入

Mary

样例输出 

I am Mary,yes,I can!
#include <stdio.h>

int main() {
	char name[10];
	scanf("%s", name);
	printf("I am %s,yes,I can!\n", name);
	return 0;
}

cppP63,为什么name前不加“&”?为什么要把name定义成数组,却一起打印?

ps:gets()与scanf()不同,gets()可以输入空格,scanf()不能输入空格,getchar()只能输入一个字符,可以是空格、换行,所以当用scanf()时,可以用getchar()保存换行符 

[4]统计每个元音字母在字符串中出现的次数。

输入

输入数据首先包括一个整数n,表示测试实例的个数,然后是n行长度不超过100的字符串,只由小写字母组成。

输出

对于每个测试实例输出5行,格式如下: a:num1 e:num2 i:num3 o:num4 u:num5 多个测试实例之间由一个空行隔开。 请特别注意:最后一块输出后面没有空行:)

样例输入 Copy

2
aeiou
my name is ignatius

样例输出 Copy

a:1
e:1
i:1
o:1
u:1

a:2
e:1
i:3
o:0
u:1
#include <stdio.h>
#include <string.h>

int main() {
	int n, i, j;
	//while (scanf("%d", &n) != EOF) {
	scanf("%d\n", &n);
	for (i = 1; i <= n; i++) {
		int a = 0, b = 0, c = 0, e = 0, f = 0, y;
		char x[100];
		gets(x);
		y = strlen(x);
		for (j = 0; j < y ; j++) {
			if (x[j] == 'a') {
				a++;
			}
			if (x[j] == 'e') {
				b++;
			}
			if (x[j] == 'i') {
				c++;
			}
			if (x[j] == 'o') {
				e++;
			}
			if (x[j] == 'u') {
				f++;
			}

		}
		printf("a:%d\ne:%d\ni:%d\no:%d\nu:%d\n", a, b, c, e, f);
		if (i != n) {
			printf("\n");
		}
	}
	return 0;
}
//}

[5]题目描述

英文单词,我们可以按照英语语法规则把单数变成复数。规则如下: (1)以辅音字母y结尾,则加es (2)以s,x,ch,sh结尾,则加es (3)以元音o结尾,则加es (4)其他情况加上s

输入

第一行输入一个数字n,表示有n组测试数据 后面跟n行,每行是一个英语单词

输出

对于每组测试数据,输出一行,要求将输入的英文单词变成复数

样例输入 Copy

2
book
fish

样例输出 Copy

books
fishes

 自己写的,答案错误

#include <stdio.h>
#include <string.h>

int main() {
	int n, i;
	scanf("%d\n", &n);
	for (i = 1; i <= n; i++) {
		char a[100];
		gets(a);
		int s = strlen(a);
		if ((a[s - 1] == 'y') || (a[s - 1] == 'x') || (a[s - 1] == 's') || (a[s - 1] == 'o') || (a[s - 2] == 'c'
		        && a[s - 1] == 'h') || (a[s - 2] == 's' && a[s - 1] == 'h')) {
			a[s] = 'e';
			a[s + 1] = 's';
            a[s + 2] = '\0';
		}  else {
			a[s] = 's';
            a[s + 1] = '\0';
		}
		puts(a);
	}
	return 0;
}

同学写的。哪都是差别。

#include <stdio.h>
#include <string.h>

void cout (char str_f[200])
{
	int i,j,t,k=0;
	int n_a=0,n_e=0,n_i=0,n_o=0,n_u=0;
	t=strlen(str_f);
	if ((str_f[t-2]=='c'||str_f[t-2]=='s')&&str_f[t-1]=='h')
	{
		str_f[t]='e';str_f[t+1]='s';t+=2;
	}else if(str_f[t-1]=='y'||str_f[t-1]=='s'||str_f[t-1]=='x'||str_f[t-1]=='o')
	{
		str_f[t]='e';str_f[t+1]='s';t+=2;
	}else
	{
		str_f[t]='s';t+=1;
	}
	printf("%s\n",str_f);
	return;
 } 

int main()
{
	int n=0;
	scanf("%d",&n);
	while(n>0)
	{
		n--;
		char str1[200]={""};
		scanf("%s",&str1);
		cout(str1);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值