Linux 系统编程实战——4.7习题

3.编写一个函数,从字符串s的第i个字符开始删除n个字符(注意要检查输入参数)

4.用递归方法求一个n个数的整形数组的最大值

 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<stdlib.h>
 4 
 5 void deleteStr(char *param, int startIndex, int number) {
 6    char *result = param;
 7    int i, length = startIndex + number;
 8 
 9    if(NULL == param || length  > strlen(param)) {
10      printf("ERROR");
11      return ;           
12   } else {
13      for(i = 0; i < strlen(param)-length; i++) {
14         *(result + startIndex + i) = *(result + length + i);       
15      }
16      *(result + length) = '\0';
17      printf("%s", result);
18   }
19 }
  int getMax(int *array, int n) {
    if(1 == n) {
      return *array;
    } else {
      if(*array > getMax(array+1, n-1)) {
        return *array;
      } else {
        return getMax(array+1, n-1); 
      }
    }
  }
20 21 int main() { 22 char str[] = "12345"; 23 int startIndex = 1; 24 int number = 2; 25 26 deleteStr(str, startIndex, number); 27 28 return 0; 29 }

猜你喜欢

转载自www.cnblogs.com/AI-Cobe/p/10250855.html