如何在C中比较字符数组的值? [英] How to compare character array value in C?
问题描述
谁能告诉我如何比较char数组的值?
我的代码示例:
Hi,
Can anyone tell me that how can I compare a value of char array?
My code sample:
typedef struct
{
char pDocId[14 + 1];
} sBlocRepSRR;
typedef struct
{
sBlocRepSRR pBlocRepSRR[MAX_REPONSES + 1];
}sRepSRR;
void func(sRepSRR * pResponse)
{
for (i=0; i<icount; i++)
{
if (pResponse->pBlocRepSRR[k].pDocId_PDF < pResponse->pBlocRepSRR[j].pDocId_PDF)
{
printf("k is less than j\n");
k = j;
} else {
printf("k is greater than j\n");
k = k;
}
}
printf("Maximum value is %s\n", pResponse->pBlocRepSRR[k].pDocId_PDF);
}
其中pResponse-> pBlocRepSRR [k] .pDocId_PDF的值为"020000001110"/"025000000556"/"02500000204"等...
请帮我从字符数组中获取最大价值.
在此先感谢.
where values of pResponse->pBlocRepSRR[k].pDocId_PDF are "020000001110" / "025000000556" / "02500000204" etc...
Please help me out for getting maximum value from character array.
Thanks in advance.
推荐答案
使用strcmp
函数;它的原型是:
Use thestrcmp
function; its prototype is:
int strcmp(const char *string1, const char *string2);
返回的值可能是:
The returned value could be:
- <如果
string1
小于string2
- < 0 if
string1
is less thanstring2
- = 0 如果
string1
等于string2
- = 0 if
string1
is equal tostring2
- >如果
string1
大于string2
if(pResponse-> pBlocRepSRR [k] .pDocId_PDF < pResponse-> pBlocRepSRR [j] .pDocId_PDF)
if (pResponse->pBlocRepSRR[k].pDocId_PDF < pResponse->pBlocRepSRR[j].pDocId_PDF)
更改为:
Change to:
long lk, lj;
lk = atol(pResponse->pBlocRepSRR[k].pDocId_PDF);
lj = atol(pResponse->pBlocRepSRR[j].pDocId_PDF);
if ( lk < lj )
:)
:)
感谢回复,但我仍然没有得到正确的答案.
这是我的输出,
4是总数
k文档ID 025000000256
j文档ID 025000000556
k大于j
k文档ID 025000000256
j文档ID 020000000204
k大于j
k文档ID 025000000256
j文档ID 020000001110
k大于j
最大值是025000000256
但它不正确,应该是
最大值是025000000556"
请帮帮我.
Thanks for reply, but still I am not getting correct answer.
Here is my output,
4 is total count
k document id 025000000256
j document id 025000000556
k is greater than j
k document id 025000000256
j document id 020000000204
k is greater than j
k document id 025000000256
j document id 020000001110
k is greater than j
Maximum value is 025000000256
But its not correct it should be
"Maximum value is 025000000556"
Please help me out.
这篇关于如何在C中比较字符数组的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!