6-22 P字符串的比较 (5分)
P字符串是另一种字符串实现形式。它也采用char
数组来保存字符串中的字符,但是最后一个字符后面没有结尾的‘\0‘
。它使用另一个int
类型的变量来表示字符串中的字符的个数。
本题要求编写P字符串的比较函数。
int pstr_cmp(const char *s1, int len1, const char *s2, int len2);
pstr_cmp
比较两个字符串的大小,如果两个字符串完全相同,则返回0;否则,返回第一个不相同的字符的差值(s1的那个字符减s2的那个字符的差)。如果两个字符串的长度不同,但是短的字符串正好是长的字符串的开头的所有字符,则用长的字符串多余部分的第一个字符的值作为差值。如hell
和hellA
的差值是-65
。
#include <stdio.h>
const int SIZE = 80;
// 这两个函数由裁判程序提供
int pstr_scan(char* str, int size);
void pstr_print(const char* str, int length);
int pstr_cmp(const char *s1, int len1, const char *s2, int len2);
int main()
{
char line[SIZE];
char text[SIZE];
int len1 = pstr_scan(line, SIZE);
int len2 = pstr_scan(text, SIZE);
printf("%d\n", pstr_cmp(line, len1, text, len2));
return 0;
}
/* 请在这里填写答案 */
123A 123
65
int pstr_cmp(const char *s1, int len1, const char *s2, int len2)
{
int i;
for(i=0;i<len1&&i<len2;i++)
{
if(s1[i]!=s2[i])
{
return s1[i]-s2[i];
}
}
return s1[i]-s2[i];
}
原文:https://www.cnblogs.com/bigageyuan/p/13842854.html