作业:
1.输入一个字符串,计算空格的个数
#include
#include
#include
int main(int argc, const char *argv[])
{
char a[20];
gets(a);
int i=0,j=0,count=0;
while(a[i]!=' ')
{
while(a[j]!=' '&&a[j]!='\0')
{
j++;
}
while(a[j]==' ')
{
j++;count++;
}
i++;
}
printf("空格个数:%d\n",count);
return 0;
}
ubuntu@ubuntu:homework$ gcc 1.c
1.c: In function ‘main’:
1.c:7:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
gets(a);
^~~~
fgets
/tmp/ccNsqk5h.o:在函数‘main’中:
1.c:(.text+0x2b): 警告: the `gets' function is dangerous and should not be used.
ubuntu@ubuntu:homework$ ./a.out
poor study for day8
空格个数:3
2.输入一个字符串,计算单词的个数
#include
#include
#include
int main(int argc, const char *argv[])
{
char a[20];
gets(a);
int i=0,j=0,count=0;
while(a[i]!='\0')
{
while(a[j]!=' '&&a[j]!='\0')
{
j++;
}
while(a[j]==' ')
{
j++;
}
i=j;
count++;
}
printf("单词个数:%d\n",count);
return 0;
}
ubuntu@ubuntu:homework$ gcc 2.c
2.c: In function ‘main’:
2.c:7:2: warning: implicit declaration of function ‘gets’; did you mean ‘fgets’? [-Wimplicit-function-declaration]
gets(a);
^~~~
fgets
/tmp/ccddazMG.o:在函数‘main’中:
2.c:(.text+0x2b): 警告: the `gets' function is dangerous and should not be used.
ubuntu@ubuntu:homework$ ./a.out
poor study for day8
单词个数:4
ubuntu@ubuntu:homework$
3.矩阵相乘:前提条件A矩阵的列数需要和B矩阵的行数相同
#include
#include
#include
int main(int argc, const char *argv[])
{
int a[][2]={1,4,6,5,2,3};
int b[][3]={1,8,4,5,2,3,7,2,3};
int a1,b1,a2,b2;
a1=sizeof(a)/sizeof(a[0]);
b1=sizeof(b)/sizeof(b[0]);
a2=sizeof(a[0])/sizeof(a[0][0]);
b2=sizeof(b[0])/sizeof(b[0][0]);
int i,j,k,t[b2][b1];
for(i=0;i
ubuntu@ubuntu:homework$ gcc 3.c
ubuntu@ubuntu:homework$ ./a.out
21 16 16
31 58 39
17 22 17
ubuntu@ubuntu:homework$
4.定义有参无返函数实现杨慧三角
#include
#include
#include
void trangle(int a[][20],int n)
{
int i,j;
for(i=0;i
ubuntu@ubuntu:homework$ gcc 4.c
ubuntu@ubuntu:homework$ ./a.out
输入层数:8
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
ubuntu@ubuntu:homework$
5.定义有参无返函数实现二维数组转置
#include
#include
#include
void zhuanzhi(int a[2][3],int b,int c)
{
int i,j,out[c][b];
for(i=0;i
ubuntu@ubuntu:homework$ gcc 5.c
ubuntu@ubuntu:homework$ ./a.out
1 4
2 5
3 6
ubuntu@ubuntu:homework$
6.输入一个字符串实现单词的逆置
#include
#include
#include
int main(int argc, const char *argv[])
{
char str[20]="";
gets(str);
int i=0,j=strlen(str)-1;
while(i
ubuntu@ubuntu:homework$ ./a.out
yduts
study
ubuntu@ubuntu:homework$
7 .输入n个字符串每个字符串20字节,并实现输出
#include
#include
#include
int main(int argc, const char *argv[])
{
printf("输入字符串数量n:");
int n;
scanf("%d",&n);
char a[n][20];
printf("输入字符串\n");
int i=0;
while(i<=n)
{
gets(a[i]);
i++;
}
int j=sizeof(a)/sizeof(a[0]);
for(i=0;i<=j;i++)
{
puts(a[i]);
}
return 0;
}
ubuntu@ubuntu:homework$ ./a.out
输入字符串数量n:6
输入字符串
123
541
648
666
721
921
123
541
648
666
721
921
ubuntu@ubuntu:homework$