当前位置:网站首页>[pointer] find the largest string

[pointer] find the largest string

2022-07-06 14:36:00 |Light|

requirement

Make up a function , Find the largest string .( Use a pointer to achieve )

Code

#include<string.h>
#include<stdlib.h>
/* *  This function is used to find n The largest of the strings  * a Is a two-dimensional character array , Each line holds a string ,n Is the number of strings with lookup  */

char * find_max_string(char a[][300],int n)
 {
    
     char *pmax; 
    int i, j = 0, k;
    for (i = 0; i < n; i++)
    {
    
        k = strcmp(a[j],a[i]);
        if (k < 0)
        {
    
            j = i;
        }
    }
    pmax = &a[j-1][300];
    return pmax;
 } 

main function

int main()
{
      
    char a[10][300]={
    0},*p;
    int n=0;
    while(gets(a[n])!=NULL){
    
        n++;
    }
    p=find_max_string(a,n);
    puts(p);
    return 0;
 }   

test

Test input
I love China
I Love China
I have dream
Output
I love China

原网站

版权声明
本文为[|Light|]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060918541413.html