program to find the second largest word in a paragraph
amongst all words that repeat more thn twice

Answer Posted / kiran kumar

Hi Pavani,
My name is Kiran.

Iam posting the code for ur Question.

The code is given below.

/* Program to find the second larges word in a paragraph
amongst all words that repeat more than twice */

# include <stdio.h>
# include <stdlib.h>

int main(void)
{
char para[80],*temp1=NULL,*temp=NULL,*first_high,*second_high;
unsigned int first_high_len,second_high_len;
printf("Enter the Paragraph\n");
fgets(para,80,stdin);

temp=strchr((const char*) para, (int)' ');
temp=para;
temp=strchr((const char*)para,(int)' '); /* Assuming
first word as the first highest
word
and storing word and the number of

letters of that word */
first_high_len=temp-para;
first_high=malloc(first_high_len+1);
strncpy(first_high, (const char*) para, first_high_len);
*(first_high+first_high_len)='\0';

temp1=strchr((const char*)temp+1,(int)' '); /* Assuming
second word as the second highest
word
and storing the word and the number of

letters of that word */
second_high_len=temp1-temp-1;
second_high=malloc(second_high_len+1);
strncpy(second_high, (const char*) temp+1, second_high_len);
*(second_high+second_high_len)='\0';

while(1)
{
temp1=strchr((const char*)temp+1,(int)' ');
if(temp1 == NULL)
break;
if(temp1-temp-1 > first_high_len)
{
free(second_high);
second_high=malloc(first_high_len+1);
second_high_len=first_high_len;
strncpy(second_high, (const char*) first_high,
second_high_len);
*(second_high+second_high_len)='\0';
free(first_high);

first_high=malloc(temp1-temp);
first_high_len=temp1-temp-1;
strncpy(first_high, (const char*) temp+1, first_high_len);
*(first_high+first_high_len)='\0';
}

else if(second_high_len < (temp1-temp-1) && first_high_len
!= (temp1-temp-1))
{
free(second_high);
second_high=malloc(temp1-temp);
second_high_len=temp1-temp-1;
strncpy(second_high, (const char*) temp1+1, second_high_len);
*(second_high+second_high_len)='\0';
}

temp=temp1;
}


if(strlen((const char*)temp+1) > first_high_len)
{
free(second_high);
second_high=first_high;
first_high=temp+1;
}

else if(strlen((const char*)temp+1) > second_high_len &&
strlen((const char*)temp+1) != first_high_len)
{
free(second_high);
second_high=temp+1;
}

printf("\nFirst Highest word = %s , Len =
%d",first_high,first_high_len);
printf("\nSecond Highest word = %s , Len = %d",second_high,
second_high_len);
return 0;
}


Check this code if u have any queries please be free
to ask any queries.
Email : kirjony@gmail.com

Is This Answer Correct ?    7 Yes 2 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Why isn't any of this standardized in c? Any real program has to do some of these things.

630


The process of repeatedly running a set of computer instructions until some condition is specifed a) condition b) sequential condition c) global d) iteration

636


What will the code below print when it is executed?   int x = 3, y = 4;         if (x = 4)                 y = 5;         else                 y = 2;         printf ("x=%d, y=%d ",x,y);

1360


What Is The Difference Between Null And Void Pointer?

649


main() { printf("hello"); fork(); }

699






What are data structures in c and how to use them?

682


What is return in c programming?

521


What is function what are the types of function?

563


Explain low-order bytes.

625


What is a floating point in c?

607


If one class contains another class as a member, in what order are the two class constructors called a) Constructor for the member class is called first b) Constructor for the member class is called second c) Only one of the constructors is called d) all of the above

626


What should malloc() do? Return a null pointer or a pointer to 0 bytes?

628


Is there any possibility to create customized header file with c programming language?

630


What the advantages of using Unions?

677


What is extern storage class in c?

517