思维导图
题目01:输入一个字符串,计算空格的个数
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char str[20]="";
puts("please enter a string below:");
gets(str);
int space_count=0;
for(int i=0;i<strlen(str);i++){
if(str[i]==' ') space_count++;
}
printf("the number of space is: %d\n",space_count);
puts(str);
return 0;
}
结果:
题目02:输入一个字符串,计算单词的个数
eg:“good good study”(空格的格式可能是多个)单词的个数是3
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
char str[90]="";
puts("please enter a string below:");
gets(str);
int word_count=0;
int i=0;
int j=0;
while(str[i]!='\0'){
while(str[i]==' ' && str[i]!='\0'){i++;}
j=i;
while(str[j]!=' ' && str[j]!='\0'){j++;}
if(j>i) word_count++;
i=j;
}
printf("the number of word is: %d\n",word_count);
return 0;
}
结果:
题目03:矩阵相乘:前提条件A矩阵的列数需要和B矩阵的行数相同
如下所示:
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int n,m;
printf("please enter the number of row:\n");
scanf("%d",&n);
printf("please enter the number of column:\n");
scanf("%d",&m);
int m1[n][m];
int m2[m][n];
printf("please enter the elements of m1 (n=%d)*(m=%d)\n",n,m);
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
scanf("%d",&m1[i][j]);
}
}
printf("please enter the elements of m1 (m=%d)*(n=%d)\n",m,n);
for(int i=0;i<m;i++){
for(int j=0;j<n;j++){
scanf("%d",&m2[i][j]);
}
}
//initialization
int m3[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
m3[i][j]=0;
}
}
//calculate
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
for(int k=0;k<m;k++){
m3[i][j]+=m1[i][k]*m2[k][j];
}
}
}
//output
printf("------------------\n");
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
printf("%-20d",m3[i][j]);
}
puts("");
}
return 0;
}
结果:
题目04:定义有参无返函数实现杨慧三角
参数:二维数组,行,列
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void Yanghui(int n,int arr[n][n]);
int main(int argc, const char *argv[])
{
//input square matrix rank n
int n;
printf("please enter the rank of the square matrix n=");
scanf("%d",&n);
printf("n=%d\n",n);
//initialize square matrix with 0
//int arr[n][n]={0,0};this initialization type will show error message below:
//"variable-sized object may not be initialized"
int arr[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
arr[i][j]=0;
}
}
//call function
Yanghui(n,arr);
return 0;
}
void Yanghui(int n,int arr[n][n]){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(j==0||i==j){
arr[i][j]=1;
}
else arr[i][j]=arr[i-1][j-1]+arr[i-1][j];
if(j<=i){
printf("%-6d",arr[i][j]);
}
}
puts("");
}
}
结果:
题目05:定义有参无返函数实现二维数组转置?
参数:二维数组,行,列
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void MatrixTransposition(int row,int column,int arr[][column]);
int main(int argc, const char *argv[])
{
int row,column;
printf("please enter the number of row=");
scanf("%d",&row);
printf("please enter the number of column=");
scanf("%d",&column);
int arr[row][column];
printf("please enter the elements of arr(row=%d)*(column=%d)\n",row,column);
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
scanf("%d",&arr[i][j]);
}
}
//print original matrix
printf("the matrix you entered is as below:\n");
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
printf("%-6d",arr[i][j]);
}
puts("");
}
//call function
MatrixTransposition(row,column,arr);
return 0;
}
void MatrixTransposition(int row,int column,int arr[][column]){
int arrt[column][row];
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
arrt[j][i]=arr[i][j];
}
}
//print
printf("the transposed matrix is as below:\n");
for(int i=0;i<column;i++){
for(int j=0;j<row;j++){
printf("%-6d",arrt[i][j]);
}
puts("");
}
}
结果:
题目06:输入一个字符串实现单词的逆置
eg:"good good study"
输出:“study good good"
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void WordReverse(int n,char str[]);
int main(int argc, const char *argv[])
{
char str[100]="";
puts("please enter one sentense to do reverse");
printf(">>");
gets(str);
puts("you entered the sentence as below");
printf(">>");
puts(str);
WordReverse(100,str);
return 0;
}
void WordReverse(int n,char str[]){
//first loop to reverse the whole sentence
for(int i=0;i<strlen(str)/2;i++){
char tmp=str[i];
str[i]=str[strlen(str)-i-1];
str[strlen(str)-i-1]=tmp;
}
puts("***1st loop ");
printf(">>");
puts(str);
//second loop to recover words
int word_bgn=0;
int word_end=0;
int i=0;
while(str[i]!='\0'){
while(str[i]==' '){i++;}
word_bgn=i;
while(str[i]!=' ' && str[i]!='\0'){
i++;
}
word_end=i-1;
//printf("str[%d]=%c str[%d]=%c\n",word_bgn,str[word_bgn],word_end,str[word_end]);
for(int k=0;k<(word_end-word_bgn)/2+1;k++){
char tmp=str[word_bgn+k];
str[word_bgn+k]=str[word_end-k];
str[word_end-k]=tmp;
}
}
puts("***2nd loop ");
printf(">>");
puts(str);
}
结果:
题目07:输入n个字符串每个字符串20字节,并实现输出
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, const char *argv[])
{
int n;
printf("number of row\n");
printf("n=");
scanf("%d",&n);
int m;
printf("number of column\n");
printf("m=");
scanf("%d",&m);
char str[n][m];
puts("------------input:");
for(int i=0;i<n;i++){
scanf("%s",str[i]);
//gets(str[i]);
}
puts("*************output:");
for(int i=0;i<n;i++){
//puts(str[i]);
printf("str[%d]=%s\n",i,str[i]);
}
return 0;
}
结果: