Wednesday, December 31, 2014

Program in C , to print the longest line in an input text. (without the use of functions , except main())



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 :



Program in C to make a horizontal , frequency distribution , histogram for various characters as present in the input text.

The following program in C draws a horizontal , frequency distribution , histogram for the various characters as present in the input text , except the 'space' character.

CAUTION : As i have declared variables within the beginning for loop parentheses , hence ,  Please compile the following program in C99 mode.If you are on Linux/Unix then you may compile it in C99 mode by typin “ -std=gnu99” or “ -std=c99 “.

Program follows :


/*Program in C to print a horizontal histogram of the frequencies of different characters(except 'space') in its input*/ 
#include<stdio.h> 
int main(){ 
int i,j; 
char text[1000]; 
char characters[128]; 
for(i=0;i<=127;++i){characters[i]=0;} 
printf("Enter the text hither : \n"); 
scanf("%[^\n]",text); 
i=0; 
while(text[i]!='\0'){ 
j=text[i]; 
characters[j]=characters[j]+1; 
    ++i; 
                   } 
printf("THe frequency distribution Histogram of alphabats :\n"); 
i=0; 
printf("Alphabat\n"); 
for(;i<=127;++i){ 
if((characters[i]!=0)&&(i!=32)){ 
printf("     %c | ",i); 
for(int k=1;k<=characters[i];++k){printf("#");} 
printf("\n"); 
                               } 
                } 
printf("        --------------------------------\n"); 
printf("         1   5    10   15    20         30 \n"); 
return 0; 
}




Sample Output


Program in C to make a vertical , frequency distribution , histogram for various characters as present in the input text.

The following program in C draws a Vertical , frequency distribution , histogram for the various characters as present in the input text .

CAUTION : As i have declared variables within the beginning for loop parentheses , hence ,  Please compile the following program in C99 mode.If you are on Linux/Unix then you may compile it in C99 mode by typin “ -std=gnu99” or “ -std=c99 “.

Program follows :

/*Program in C to print a vertical histogram of the frequencies of different characters in its input*/ 

#include<stdio.h> 
int main(){ 
int i,j,t,q,a,flag; 
char text[1000]; 
char collector[128]; 

for(i=0;i<=127;++i){collector[i]=0;} 

printf("Enter the text hither : \n"); 
scanf("%[^\n]",text); 
i=0; 
while(text[i]!='\0'){ 
j=text[i]; 
collector[j]=collector[j]+1; 
    ++i; 
                 } 
t=0; 
for(int q=0;q<=127;++q){if(collector[q]>0){++t;}} 
char cart[t]; 
char ascii[t]; 

i=0; 
q=0; 
for(;q<127;++q){if(collector[q]>0){ 
                   cart[i]=collector[q]; 
                             ascii[i]=q; 
                                    ++i; 
                                  } 
               } 

printf("\n\nThe frequency distribution Histogram of characters :\n"); 
printf("\nfrequency\n"); 

for(int y=t+5;y>=1;--y){ 

if(y%2==0){    

        if(y>8){ 

                printf("        %d|",y); 
            
               } 
            else{           
               printf("        %d |",y); 
                } 
            
for(int r=0;r<t;++r){ 

if(cart[r]==y){ 

if(r==0){ 

printf("^"); 

        }else{ 

              printf(" ^"); 

             } 

cart[r]=cart[r]-1; 
              }else{ 

                  printf("  "); 

                   } 
                    }printf("\n"); 

          } else{ 
                printf("          |"); 

flag=0; 
for(int r=0;r<t;++r){ 

if(cart[r]==y){ 

if(r==0){ 

printf("^"); 

        }else{ 

              printf(" ^"); 

             } 

cart[r]=cart[r]-1; 
              }else{ 

                  printf("  "); 

                   } 
                    } 
printf("\n"); 

               } 
                       } 
printf("          -------------------------------------------\n"); 

printf("          "); 

for(int a=0;a<t;++a){ 

printf(" %c",ascii[a]); 
                     } 

printf("\n"); 
return 0; 
} 





Sample Output

Wednesday, December 17, 2014

Program in C to count the number of alphabats , digits and whitespaces in the input text.

The following program works to count the individual occurrences of each digit from 0-9 , each alphabet a-z and A-Z .

Program follows :


#include<stdio.h>
int main(){
/*Program in C to count the number of whitespaces ,  number of respective alphabats and the number of repective digits */
int i =0;
char s[1000];
int d[10];
int sp=0;
int alpha[128];

for(i=0;i<10;++i){d[i]=0;}
for(i=0;i<=127;++i){alpha[i]=0;}

printf("Enter thy text hither : ");
for(i=0;i<1000;++i){
                scanf("%[^\n]s",s);
                    }
i=0;

while(s[i]!='\0'){

if(s[i]==' '){
          ++sp;
             }else{

if(s[i]>='0' && s[i]<='9'){
 
 ++d[s[i]-'0'];
                        }else{
int j=0;

if((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z') ){
     
     j=s[i];
     
     ++alpha[j];
     
          }
                               } }  
++i;
}

printf("THE TOTAL NUMBER OF WHITESPACES ARE : %d\n\n",sp);
printf("THE TOTAL NUMBER OF RESPECTIVE DIGITS ARE :  \n\n");
for(i=0;i<=9;++i){
printf("DIGIT %d occured %d times \n\n",i,d[i]);
                 }
printf("THE TOTAL NUMBER OF REPECTIVE ALPHABATS ARE : \n\n");
for(i=65;i<=90;++i){

printf("THE ALPHABAT %c OCCURED %d TIMES. \n",i,alpha[i]);

                  }


for(i=97;i<=122;++i){

printf("THE ALPHABAT %c OCCURED %d TIMES. \n",i,alpha[i]);

                  }

return 0;
}
Sample Output :
hemant@hemant-desktop:~/code$ clear

hemant@hemant-desktop:~/code$ gcc testa.c
hemant@hemant-desktop:~/code$ a.out
Enter thy text hither : My name is Hemant KUmar and my Dell badge ID is 123456789
THE TOTAL NUMBER OF WHITESPACES ARE : 11

THE TOTAL NUMBER OF RESPECTIVE DIGITS ARE :

DIGIT 0 occured 0 times

DIGIT 1 occured 1 times

DIGIT 2 occured 1 times

DIGIT 3 occured 1 times

DIGIT 4 occured 1 times

DIGIT 5 occured 1 times

DIGIT 6 occured 1 times

DIGIT 7 occured 1 times

DIGIT 8 occured 1 times

DIGIT 9 occured 1 times

THE TOTAL NUMBER OF REPECTIVE ALPHABATS ARE :

THE ALPHABAT A OCCURED 0 TIMES.
THE ALPHABAT B OCCURED 0 TIMES.
THE ALPHABAT C OCCURED 0 TIMES.
THE ALPHABAT D OCCURED 2 TIMES.
THE ALPHABAT E OCCURED 0 TIMES.
THE ALPHABAT F OCCURED 0 TIMES.
THE ALPHABAT G OCCURED 0 TIMES.
THE ALPHABAT H OCCURED 1 TIMES.
THE ALPHABAT I OCCURED 1 TIMES.
THE ALPHABAT J OCCURED 0 TIMES.
THE ALPHABAT K OCCURED 1 TIMES.
THE ALPHABAT L OCCURED 0 TIMES.
THE ALPHABAT M OCCURED 1 TIMES.
THE ALPHABAT N OCCURED 0 TIMES.
THE ALPHABAT O OCCURED 0 TIMES.
THE ALPHABAT P OCCURED 0 TIMES.
THE ALPHABAT Q OCCURED 0 TIMES.
THE ALPHABAT R OCCURED 0 TIMES.
THE ALPHABAT S OCCURED 0 TIMES.
THE ALPHABAT T OCCURED 0 TIMES.
THE ALPHABAT U OCCURED 1 TIMES.
THE ALPHABAT V OCCURED 0 TIMES.
THE ALPHABAT W OCCURED 0 TIMES.
THE ALPHABAT X OCCURED 0 TIMES.
THE ALPHABAT Y OCCURED 0 TIMES.
THE ALPHABAT Z OCCURED 0 TIMES.
THE ALPHABAT a OCCURED 5 TIMES.
THE ALPHABAT b OCCURED 1 TIMES.
THE ALPHABAT c OCCURED 0 TIMES.
THE ALPHABAT d OCCURED 2 TIMES.
THE ALPHABAT e OCCURED 4 TIMES.
THE ALPHABAT f OCCURED 0 TIMES.
THE ALPHABAT g OCCURED 1 TIMES.
THE ALPHABAT h OCCURED 0 TIMES.
THE ALPHABAT i OCCURED 2 TIMES.
THE ALPHABAT j OCCURED 0 TIMES.
THE ALPHABAT k OCCURED 0 TIMES.
THE ALPHABAT l OCCURED 2 TIMES.
THE ALPHABAT m OCCURED 4 TIMES.
THE ALPHABAT n OCCURED 3 TIMES.
THE ALPHABAT o OCCURED 0 TIMES.
THE ALPHABAT p OCCURED 0 TIMES.
THE ALPHABAT q OCCURED 0 TIMES.
THE ALPHABAT r OCCURED 1 TIMES.
THE ALPHABAT s OCCURED 2 TIMES.
THE ALPHABAT t OCCURED 1 TIMES.
THE ALPHABAT u OCCURED 0 TIMES.
THE ALPHABAT v OCCURED 0 TIMES.
THE ALPHABAT w OCCURED 0 TIMES.
THE ALPHABAT x OCCURED 0 TIMES.
THE ALPHABAT y OCCURED 2 TIMES.
THE ALPHABAT z OCCURED 0 TIMES.
hemant@hemant-desktop:~/code$

Tuesday, December 9, 2014

Program in C to replace each string of one or more blanks by a single blank.

The following program strives to give a solution to the problem of replacing each string of one or more blanks , as present in the inputted text, by a single blank.




Program Without Comments :


/*Program in C to replace each string of one or more blanks by a single blank.*/
#include<stdio.h>
int main(){
char s[200];
int t=0;
int i=0;
printf("Enter your text :\n");
scanf("%[^\n]s",s);
while(s[i]!='\0'){
if(s[i]==' '){
   ++t;
          }else{
          if(t>0){
              t=0;
              printf(" ");
              printf("%c",s[i]);
                 }else{
                    printf("%c",s[i]);
                      }
               }
++i;
               }
return 0;
}



Program with comments :

/*Program in C to replace each string of one or more blanks by a single blank.*/
#include<stdio.h>
int main(){
char s[200]; /*Declare a char array , big enough to hold the inputted text*/
int space=0;     /*to count the number of spaces*/
int i=0;     /*The index value holder ,to traverse the char array*/
printf("Enter your text :\n");
scanf("%[^\n]s",s);  /*We'll scan the text till the ENTER Key is pressed.*/
while(s[i]!='\0'){   /*Check to determine the end of char array*/
if(s[i]==' '){      
   ++space;         
          }else{
          if(space>0){  /*space>0 would signify that we just traversed more than 1 'space' characters */
              space=0; /*we'll put space=0*/
              printf(" "); /*Since we just traversed more than 1 space , we print a single space character instead of all of the traversed spaces*/
              printf("%c",s[i]); /*Next we print the current non-space character*/
                 }else{
                    printf("%c",s[i]); /*Else we just print the current character as we did not encounter one or more than one space  characters*/
                      }
               }
++i;
               }
return 0;
}


Monday, December 8, 2014

Program in C to count the number of words.


The problem to find the number of words as present in a piece of inputted text is one of the basic problems encountered while learning the C language.

The internet is flooded with probable programs written in C to solve it , but , they fail miserably when tested with conditions like :

1)Presence of multiple ,  unwanted spaces between any two consecutive words.
2)Presence of trailing spaces after the end of text ,that might have crept in erroneously.

As a result of the above two , the output of most such programs goes awry .

The following program , however , does not fall into that trap and gives accurate results for the above two and other such similar cases.


Program without Comments:


/*Program to count the number of words in an inputted text.*/
#include<stdio.h>
int main(){
int words=0;
int space=0;
int i=0;
char s[200];
printf("ENTER YOUR TEXT , TO END THE INPUT HIT ENTER KEY \n");
scanf("%[^\n]s",s);
while(s[i]!='\0'){
if(s[i]==' ' && space==0){
      ++space;    
     ++words;
          }else{
          if(s[i]==' '){;}else{
          space=0;
          }
++i;
          }
          }
if(s[i-1]==' '){--words;}         
printf("THE NUMBER OF WORDS IS : %d",words+1);
return 0;
}




Program with Comments:


/*Program to count the number of words in an inputted text.*/
#include<stdio.h>
int main(){
int words=0;
int space=0;
int i=0;
char s[200];
printf("ENTER YOUR TEXT , TO END THE INPUT HIT ENTER KEY \n");
scanf("%[^\n]s",s); 
/*We'll scan the text till the ENTER Key is pressed.*/ while(s[i]!='\0'){    /*Check to determine the end of char array*/
if(s[i]==' ' && space==0){ /*Determine if this is the first encountered space*/
      ++space;       /*If yes , then increase the space counter by 1 */
     ++words;            /*We just passed by a word , hence increase word count by 1*/
          }else{
          if(s[i]==' ')/*If s[i] was a space AND space>0 , that means we are still passing by more and more spaces ,hence do nothing till we reach a non-space character.*/{;}else{
          space=0; /*If not ,then that means we are traversing inside a word :),hence simply put space=0  , this word's presence would be counted once we encounter a space after this , as done in the top part of the loop*/
          }
++i; /*Increment the Index counter to traverse the next character */
          }
          }
if(s[i-1]==' '){--words;}   /*Check presence of any trailing spaces after the last word in the inputted text , as in that case the second last element would contain a space , hence check if s[i-1]==' ' , if it is , then , for sure our loop would have increased the count of words by 1 , hence reduce it by 1 to get correct result.*/      
printf("THE NUMBER OF WORDS IS : %d",words+1); /*Finally print the number of words.*/
return 0;
}