Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,971 questions

51,913 answers

573 users

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

Related questions

1 answer 142 views
2 answers 147 views
1 answer 163 views
1 answer 118 views
118 views asked Apr 26, 2024 by avibootz
...