How to input strings and find the longest string in C

1 Answer

0 votes
#include <stdio.h>
  
#define LEN 1024
#define N 3

int main(int argc, char **argv) 
{ 
    int len = 0, max = 0, i = 0;            
    char line[LEN];    
    char longest_line[LEN]; 
       
    while (i < N) 
    {
        gets(line); 
        len = strlen(line);  
        if (len > max)
        { 
            max = len; 
            strcpy(longest_line, line); 
        } 
        i++;
    }
    if (max > 0)  
        printf("longest line = %s len = %d", longest_line, len); 
    
    return 0; 

}


/*
  
run:
   
aaaaaa
bbbbbbbbbbbbbbbbbbbbbb
cccccccccccccc
longest line = bbbbbbbbbbbbbbbbbbbbbb len = 14

*/

 



answered Oct 27, 2015 by avibootz
...