赞
踩
#include <stdio.h> #include <stdlib.h> int strCompare(char *p1, char *p2); //定义strcmp函数 int main(){ char s1[20], s2[20]; printf("Please enter s1 string: "); //输入字符串s1 gets(s1); printf("Please enter s2 string: "); //输入字符串s2 gets(s2); printf("Result: %d\n", strCompare(s1, s2)); //输出返回值 system("pause"); return 0; } //strCompare函数 int strCompare(char *p1, char *p2){ for(; *p1 == *p2 && (*p1 != '\0' || *p2 != '\0'); p1++, p2++); return *p1 - *p2; }
#include <stdio.h> #include <stdlib.h> #define N 80 void initialStr(char **str1, char **str2, int n){ *str1 = (char*)malloc(n * sizeof(char)); *str2 = (char*)malloc(n * sizeof(char)); } void inputStr(char *str1, char *str2){ printf("Enter string 1: "); gets(str1); printf("Enter string 2: "); gets(str2); } int strCompare(char *p1, char *p2){ for(; *p1 == *p2 && (*p1 != '\0' || *p2 != '\0'); p1++, p2++); return *p1 - *p2; } void freeStr(char **str1, char **str2){ free(*str1); free(*str2); } int main(){ char *str1 = NULL; char *str2 = NULL; initialStr(&str1, &str2, N); inputStr(str1, str2); printf("Value = %d\n", strCompare(str1, str2)); freeStr(&str1, &str2); system("pause"); return 0; }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。