12月2日, 2014 2,760 次查看次
C语言复制字符串、比较字符串、合并字符串(自定义函数、指针实现)
段段使用了指针与自定义函数来实现,环境为VC++6.0,供大家交流分享。
代码如下,截图在后:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | /**************************************************** * 东北大学 软件工程 1401班 段育凯 * 学号:20144569 * 网址:https://duan.sh * 邮箱:a@neu.la * 时间:2014/12/01 18:00 ****************************************************/ /**************************************************** *推荐一些相关知识的链接 *关于数组名与指针的有关见解: *http://tech.163.com/05/0823/11/1RR94CAS00091589.html *类似问题: *http://stackoverflow.com/questions/9413046/how-to-get-the-length-of-array-in-c-is-sizeof-is-one-of-the-solution *有关_getch()(——读取其他按键值时要调用两次,第一次返回0或0xE0,第二次是按键值) *http://msdn.microsoft.com/en-us/library/078sfkak.aspx ****************************************************/ #include<stdio.h> #include<conio.h> #include<windows.h> void copyString(char *target, char *source); int compareString(char *target, char *source); void stringConcatenate(char *string1, char *string2); void main(){ char string1[100],string2[100]; int caseNum=0; int exitFlag=0; int result2=0; int character=0; system("color F0"); //SetConsoleTitle("自定义函数作业—BY段育凯 东北大学 20144569"); while(!exitFlag){ system("cls"); SetConsoleTitle("自定义函数作业—BY段育凯 东北大学 20144569"); printf("请输入以下数字来操作:\n\n1.复制字符串;\n2.比较字符串;\n3.首尾相接字符串;\n4.结束本程序。\n\n请输入指令:"); fflush(stdin); scanf("%d",&caseNum); switch(caseNum){ case 1:{ while(1){ system("cls"); SetConsoleTitle("复制字符串——自定义函数作业—BY段育凯 东北大学 20144569"); printf("请输入源字符串:\n"); fflush(stdin); gets(string1); copyString(string2, string1); printf("复制后的字符串为“%s”\n\n", string2); printf("按home键返回上一级,其他键继续本子程序...\n"); fflush(stdin); character = _getch(); if(character==0 ||character==224){ if(_getch()==71){ break; } } } break; } case 2:{ while(1){ system("cls"); SetConsoleTitle("比较字符串——自定义函数作业—BY段育凯 东北大学 20144569"); printf("请输入第一个字符串:\n"); fflush(stdin); gets(string1); printf("请输入第二个字符串:\n"); fflush(stdin); gets(string2); result2=compareString(string1,string2); if(result2==0){ printf("两字符串相同。\n"); }else{ printf("两字符串不同,第一位差异项相差%d\n\n",result2); } printf("按home键返回上一级,其他键继续本子程序...\n"); fflush(stdin); character = _getch(); if(character==0 ||character==224){ if(_getch()==71){ break; } } } break; } case 3:{ while(1){ system("cls"); SetConsoleTitle("追加字符串——自定义函数作业—BY段育凯 东北大学 20144569"); printf("请输入第一个字符串:\n"); fflush(stdin); gets(string1); printf("请输入第二个字符串:\n"); fflush(stdin); gets(string2); stringConcatenate(string1,string2); printf("%s",string1); printf("\n\n按home键返回上一级,其他键继续本子程序...\n"); fflush(stdin); character = _getch(); if(character==0 ||character==224){ if(_getch()==71){ break; } } } break; } case 4:{ exitFlag=1; break; } } } } void copyString(char *target, char *source){ while(*source){ *target = *source; source++; target++; } *target = '\0'; } int compareString(char *string1, char *string2){ while(*string1==*string2){ if ( *string1 == '\0' || *string2 == '\0' ){ break; } string1++; string2++; } if( *string1 == '\0' && *string2 == '\0'){ return 0; }else{ return (*string1 - *string2); } } void stringConcatenate(char *string1, char *string2){ while(*string1!='\0'){ string1++; } while(*string2!='\0'){ *string1=*string2; string1++; string2++; } *string1='\0'; } |
截图如下: