myatoi, mystcmp, mystrcasecmp,mystrncmp 迷南。 2022-09-20 15:28 23阅读 0赞 /**************************************** int atoi(char *s):不能转换则返回0 将字符串s转为int整数 atoi(“123”) :结论为123 atoi(“12ab”):结论为12 atoi(“ab”):结论为0 *****************************************/ //***********myatoi********************** #include <stdio.h> #include <ctype.h> int myatoi(char *str) { int sum = 0, i; for(i = 0; str[i]; i++ ){ if(isdigit(str[i])){ sum = sum * 10 + (str[i] - '0'); }else { break; } } return sum; } int main(void) { char src[1024]; gets(src); printf("%d\n", myatoi(src)); return 0; } /********************************************** akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./myatoi 5689 5689 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./myatoi 59da 59 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./myatoi ad 0 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./myatoi ad56 0 ***********************************************/<pre name="code" class="cpp"> //************************mystrcmp*************** #include <stdio.h> int mystrcmp(char *s1, char *s2) { int i; for(i = 0; (s1[i] == s2[i]); i++){ if(!s1[i]){ // !s1[i] // ( s1[i] == '\0') return 0; } } return s1[i] > s2[i] ? 1: -1; } int main(void) { char src1[1024]; char src2[1024]; gets(src1); gets(src2); printf("%d\n", mystrcmp(src1, src2) ); return 0; } /* akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrcmp abc abc 0 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrcmp ab abc -1 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrcmp abc ab 1 */ //************************mystrncmp******************** //***************************************************** #include <stdio.h> int mystrncmp(char *s1, char *s2, int n) { int i; for(i = 0; (s1[i] == s2[i]); i++){ n--; if((n == 0) || !s1[i]){ // !s1[i] // ( s1[i] == '\0') return 0; } // printf("i:%d n:%d\n", i, n); } return s1[i] > s2[i] ? 1: -1; } int main(void) { char src1[1024]; char src2[1024]; gets(src1); gets(src2); printf("%d\n", mystrncmp(src1, src2, 3) ); return 0; } /* akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrncmp hello helwz i:0 n:2 i:1 n:1 0 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrncmp hewl hell i:0 n:2 i:1 n:1 1 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrncmp heel hewl i:0 n:2 i:1 n:1 -1 */ //*********************mystrcasecmp******************** //***************************************************** #include <stdio.h> #include <ctype.h> int mystrcasecmp(char *src1, char *src2) { int i; for(i = 0; ( toupper(src1[i]) == toupper(src2[i]) ); i++){ if(!src1[i]){ return 0; } } return src1[i] > src2[2] ? 1: -1; } int main(void) { char str1[1024]; char str2[1024]; gets(str1); gets(str2); printf("%d\n", mystrcasecmp(str1, str2)); return 0; } /* abc AB 1 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrcasecmp sd SD 0 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrcasecmp sss AAAAA 1 akaedu@akaedu-G41MT-D3:~/lin/722_str$ ./mystrcasecmp fg FGG -1 */
相关 myatoi, mystcmp, mystrcasecmp,mystrncmp / int atoi(char s):不能转换则返回0 将字符串s转为int整数 atoi(“123”) :结论为123 atoi(“1 迷南。/ 2022年09月20日 15:28/ 0 赞/ 24 阅读
还没有评论,来说两句吧...