1.在主函数中给定6个字符串,用另一个函数对他们按从小到大的顺序,然后在 主函数中输出这6个已经排好序的字符串。要求使用指针数组进行处理。2.将字符串a复制到字符串b.3、输入一个五位以内的正整数(1)判断它是一个几位数;(2)请按序输出其各位数字;(3)逆序输出其各位数字。如输入:56439,输出:5位数 5,6,4,3,9 9,3,4,6,51.输入一个10元素的一维数组,在主函数中用scanf函数输入。用指针的方式编程找出最大和最小数,并指出最大和最小数的位置。2.输入一段文字,统计其中所包含的数字字符和字母字符的个数。3.输入10个整数,将其中最小的数与第一个数对换,把最大的数与最后一个数对换,用指针实现。4.编写一个C函数,有三个参数,第一个参数输入一个字符串,第二个参数是一个长度值,第三个参数仍然是一个字符串,函数功能为从第一个字符串中复制第二个参数长度个字符到第三个参数的字符串中。然后编写main函数进行调用和输出。#include#include #define N 5#define LEETERNO 80int mystringCmp(char *a,char *b);int mystringCmp(char *a,char *b){ while (*a || *b)//等于空的话直接跳出循环 { if (*a - *b)//判断a 与 b 的是否相同 { return *a - *b;//如果不同返回a 与 b 的Ascii差 } else // 相同,让指针指向a 和 b 下一个字符比较 { a++; b++; } } return 0;}void sortString(char * str[],int count);void sortString(char * str[],int count){ char *temp; for (int i = 0 ; i < count - 1; i++) { for (int j = 0; j < count -1 -i; j++) { if ( mystringCmp(str[j], str[j+1]) > 0) { temp = str[j]; str[j] = str[j+1]; str[j+1] = temp; } } }}int getMaxLocation(int *p,int n);int getMaxLocation(int *p,int n){ int location = 0; int max = *p; for (int i = 0; i < n ; i++) { if (max < *(p+i)) { max = *(p+i); location = i; } } return location;}int getMinLocation(int *p,int n);int getMinLocation(int *p,int n){ int location = 0; int min = *p; for (int i = 0; i < n ; i++) { if (min > *(p+i)) { min = *(p+i); location = i; } } return location;}void changeNum(int *array);void changeNum(int *array){ int *max,*min,t; max=min=array; for (int *p = array; p < array +(N); p++) { if (*max < *p) { max = p; } } //最大值交换 t = *max;*max = array[(N-1)];array[(N-1)] = t; for (int *p = array; p < array +(N); p++) { if (*min > *p) { min = p; } } //最小值交换 t = *min;*min = array[0];array[0] = t;}void cpyChar(char *p1,int len,char *p2);void cpyChar(char *p1,int len,char *p2){ for (int i = 0; i < len; i++) { *p2 = *p1; p1++; p2++; }}int main(int argc, const char * argv[]){// char * str[6] = {"fun","zoo","dog","cat","boy","an"};// // //to do what// sortString(str,6);// //// for (int i = 0; i < 6; i++) {//// printf("%s\n",str[i]);//// }// // //第二题// char a[] = "hello";// int len = (int) strlen(a);// char b[len];// //to do what// char *p1,*p2;// p1 = a;// p2 = b;// for (int i = 0; i < len; i++) {// *p2 = *p1;//将p1所指的内容给p2// p1++;//p1偏移到下一个// p2++;// }// printf("复制后的b = %s\n",b);// //1// int arr[N];// int maxLocation = 0;// int minLocation = 0;// for (int i = 0; i < N ; i++)// {// scanf("%d",&arr[i]);// }// // for (int i = 0; i < N ; i++)// {// printf("%d\t",arr[i]);// }// //work// maxLocation = getMaxLocation(arr, N );// minLocation = getMinLocation(arr, N );// // printf("max[%d] = %d\n",maxLocation,arr[maxLocation]);// printf("min[%d] = %d\n",minLocation,arr[minLocation]); //2 /* char ch[LEETERNO]; int figureNumber = 0; int letter = 0; printf("请输入一段字符串");// for (int i = 0; i < LEETERNO; i++)// {// scanf("%c",&ch[i]);// if ( ch[i] == '\n') {// break;// }// } gets(ch); int len = (int) strlen(ch); printf("你输入的文字为\n"); for (int i = 0; i < LEETERNO; i++) { printf("%c",ch[i]); } for (int i = 0; i < len; i++) { if (ch[i] >= '0' && ch[i] <= '9') { figureNumber++; } if ((ch[i]>='a' && ch[i] <= 'z') || (ch[i]>='A' && ch[i] <= 'Z')) { letter++; } } printf("这段文字的数字的个数为:%d,字母个数为%d\n",figureNumber,letter); */ /* //3 int number[N]; printf("请输入10个整数:"); for (int i = 0; i < N ; i++) { scanf("%d",&number[i]); } printf("你输入的数组为:"); for (int i = 0; i < N ; i++) { printf("%4d",number[i]); } printf("\n"); // int *p; p = number; changeNum(p); for (int i = 0; i < N ; i++) { printf("%4d",number[i]); } */ //4 char firstCh[] = "hello,dear"; int firstLen =(int)(strlen(firstCh)); int len; printf("请输入你要转换的长度"); scanf("%d",&len); if (len < firstLen) { len = firstLen; } char second[len]; //work char *p1,*p2; p1 = firstCh; p2 = second; cpyChar(p1,len,p2); printf("%s\n",p2); return 0;}
#include#include #define N 5#define LEETERNO 80int mystringCmp(char *a,char *b);int mystringCmp(char *a,char *b){ while (*a || *b)//等于空的话直接跳出循环 { if (*a - *b)//判断a 与 b 的是否相同 { return *a - *b;//如果不同返回a 与 b 的Ascii差 } else // 相同,让指针指向a 和 b 下一个字符比较 { a++; b++; } } return 0;}void sortString(char * str[],int count);void sortString(char * str[],int count){ char *temp; for (int i = 0 ; i < count - 1; i++) { for (int j = 0; j < count -1 -i; j++) { if ( mystringCmp(str[j], str[j+1]) > 0) { temp = str[j]; str[j] = str[j+1]; str[j+1] = temp; } } }}int getMaxLocation(int *p,int n);int getMaxLocation(int *p,int n){ int location = 0; int max = *p; for (int i = 0; i < n ; i++) { if (max < *(p+i)) { max = *(p+i); location = i; } } return location;}int getMinLocation(int *p,int n);int getMinLocation(int *p,int n){ int location = 0; int min = *p; for (int i = 0; i < n ; i++) { if (min > *(p+i)) { min = *(p+i); location = i; } } return location;}void changeNum(int *array);void changeNum(int *array){ int *max,*min,t; max=min=array; for (int *p = array; p < array +(N); p++) { if (*max < *p) { max = p; } } //最大值交换 t = *max;*max = array[(N-1)];array[(N-1)] = t; for (int *p = array; p < array +(N); p++) { if (*min > *p) { min = p; } } //最小值交换 t = *min;*min = array[0];array[0] = t;}void cpyChar(char *p1,int len,char *p2);void cpyChar(char *p1,int len,char *p2){ for (int i = 0; i < len; i++) { *p2 = *p1; p1++; p2++; }}int main(int argc, const char * argv[]){// char * str[6] = {"fun","zoo","dog","cat","boy","an"};// // //to do what// sortString(str,6);// //// for (int i = 0; i < 6; i++) {//// printf("%s\n",str[i]);//// }// // //第二题// char a[] = "hello";// int len = (int) strlen(a);// char b[len];// //to do what// char *p1,*p2;// p1 = a;// p2 = b;// for (int i = 0; i < len; i++) {// *p2 = *p1;//将p1所指的内容给p2// p1++;//p1偏移到下一个// p2++;// }// printf("复制后的b = %s\n",b);// //1// int arr[N];// int maxLocation = 0;// int minLocation = 0;// for (int i = 0; i < N ; i++)// {// scanf("%d",&arr[i]);// }// // for (int i = 0; i < N ; i++)// {// printf("%d\t",arr[i]);// }// //work// maxLocation = getMaxLocation(arr, N );// minLocation = getMinLocation(arr, N );// // printf("max[%d] = %d\n",maxLocation,arr[maxLocation]);// printf("min[%d] = %d\n",minLocation,arr[minLocation]); //2 /* char ch[LEETERNO]; int figureNumber = 0; int letter = 0; printf("请输入一段字符串");// for (int i = 0; i < LEETERNO; i++)// {// scanf("%c",&ch[i]);// if ( ch[i] == '\n') {// break;// }// } gets(ch); int len = (int) strlen(ch); printf("你输入的文字为\n"); for (int i = 0; i < LEETERNO; i++) { printf("%c",ch[i]); } for (int i = 0; i < len; i++) { if (ch[i] >= '0' && ch[i] <= '9') { figureNumber++; } if ((ch[i]>='a' && ch[i] <= 'z') || (ch[i]>='A' && ch[i] <= 'Z')) { letter++; } } printf("这段文字的数字的个数为:%d,字母个数为%d\n",figureNumber,letter); */ /* //3 int number[N]; printf("请输入10个整数:"); for (int i = 0; i < N ; i++) { scanf("%d",&number[i]); } printf("你输入的数组为:"); for (int i = 0; i < N ; i++) { printf("%4d",number[i]); } printf("\n"); // int *p; p = number; changeNum(p); for (int i = 0; i < N ; i++) { printf("%4d",number[i]); } */ //4 char firstCh[] = "hello,dear"; int firstLen =(int)(strlen(firstCh)); int len; printf("请输入你要转换的长度"); scanf("%d",&len); if (len < firstLen) { len = firstLen; } char second[len]; //work char *p1,*p2; p1 = firstCh; p2 = second; cpyChar(p1,len,p2); printf("%s\n",p2); return 0;}