代码练习1:
用函数的方式实现9*9乘法表
void print_table(int n)
{int i = 0;int j = 0;for (i = 1; i<= n; i++){for (j = 1; j<= i; j++){printf("%d*%d=%-3d ", i, j, i * j);}printf("\n");}}int main()
{int n = 0;scanf("%d", &n);print_table(n);return 0;
}
代码练习2
//我的思路1 用循环实现
int my_strlen(char* pi)
{int count = 0;while (*pi != '\0'){count++;pi++;}return count;
}void reverse_string(char* str)
{int left = 0;int right = my_strlen(str)-1;while (left < right){char pa = str[left];str[left] = str[right];str[right] = pa;left++;right--;}}int main()
{char arr[] = "abcdef" ;reverse_string(arr);printf("%s\n", arr);return 0;
}
//我的思路2 用递归去实现 定义三个参数
void reverse_string(char* str,int x,int y)
{if (x<y){ char pa='0';pa = str[x];str[x] = str[y];str[y] = pa;reverse_string(str,x+1,y-1);}
}int main()
{char arr[] = "abcdef" ;int left = 0;int right = my_strlen(arr) - 1;reverse_string(arr,left,right);printf("%s\n", arr);return 0;
}
大佬思路
//大佬思路 用递归实现
int my_strlen(char* pi)
{int count = 0;while (*pi != '\0'){count++;pi++;}return count;
}void reverse_string(char* str)
{char tmp = *str;//1int len = my_strlen(str);//注释:这里必须多定义一个变量len,//而不能直接使用my_strlen(str)放入函数中,因为str表示指针,出现str+1,就表明地址往前+1*str = *(str + len - 1);//2*(str + len -1) = '\0';//3if (my_strlen(str + 1) >= 2){reverse_string(str + 1);//4}*(str + len - 1) = tmp;//5}int main()
{char arr[] = "abcdef";reverse_string(arr);printf("%s\n", arr);return 0;
}