The following program in C , finds and prints the longest line in an input text , without using functions(except main()).
Program follows :
/*Program ic C to print the longest line in an input text.Without using functions */
#include<stdio.h>
int main(){
char currentline[300];
char longestline[300];
int current=0;
int longest=0;
char text[1000];
int i=0;
int t=0;
printf("ENTER THY TEXT HITHER : \n");
scanf("%[^\t]c",text);
while(text[i]!='\t'){
if(text[i]=='\n'){
if(current>longest){
longest=current;
int k =0;
while((longestline[k]=currentline[k])!='\0')++k;
current=0;
t=0;
}
}else{
++current;
currentline[t]=text[i];
++t;
}
++i;
}
if(current>longest){
longest=current;
int k =0;
while((longestline[k]=currentline[k])!='\0')++k;
}
printf("*****************************************************************************\n");
printf("THE LONGEST LINE WAS %d CAHARCTERS LONG :) , AND ITS PRINTED BELOW \n\n",longest);
printf(" ");
int y=0;
while(longestline[y]!='\0' && y<longest){
printf("%c",longestline[y]);
++y;
}
printf("\n\n");
return 0;
}
Sample Output :

No comments:
Post a Comment