Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

Can anyone tell me that how can I compare a value of char array?

My code sample:

C
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);
}


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.
Posted
Updated 31-May-10 21:46pm
v4
Comments
Moak 1-Jun-10 3:48am    
Updated subject, code and tags. In the future please be so kind and format your source code before you post it.

Use the strcmp function; its prototype is:

C++
int strcmp(const char *string1, const char *string2);



The returned value could be:


    • < 0 if string1 is less than string2

    • = 0 if string1 is equal to string2

    • > 0 if string1 is greater than string2

 
Share this answer
 
Poonamol wrote:
if (pResponse->pBlocRepSRR[k].pDocId_PDF < pResponse->pBlocRepSRR[j].pDocId_PDF)


Change to:
C
long lk, lj;
lk = atol(pResponse->pBlocRepSRR[k].pDocId_PDF);
lj = atol(pResponse->pBlocRepSRR[j].pDocId_PDF);
if ( lk < lj )


:)
 
Share this answer
 
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.
 
Share this answer
 
Thanks for the reply srtcmp is working fine :-O
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900