Sunday 22 January 2017

Printing a Vertical Histogram of words in a string representing the size of each word using an example in C program(c programming examples)(c program examples)

Printing a Vertical Histogram of words in a string representing the size of each word using an example in C program

This is an example C program which is written on the base of basic C programming, If condition, for loop.


If you want to see this code with a perfect indentation, copy the code into "sublime text editor" and make sure that the type of code is set to 'c' at the bottom right of the window. After pasting the code press the command "ctrl+shift+P", you get a search box. Type "indentation" you get an option like this below the search box "Indentation: Reindent lines". Clink on that to get the code with indentation. 



The program is as follows:

#include <stdio.h>
#include <string.h>
int main()
{
char string[1000],hist[100];
int i,lc=0,max=0,j=0,nofwrd=1;
printf("enter any string\n");
gets(string);
for(i=0;string[i]!='\0';i++)
if(string[i]!='\n' && string[i]!=' ' && string[i]!='\t')
if(string[i+1]=='\n'||string[i+1]==' '||string[i+1]=='\t')
nofwrd++;
printf("%d\n",nofwrd);
for(i=0;string[i]!='\0';i++)
{
if(string[i]!='\n' && string[i]!=' ' && string[i]!='\t')
lc++;
if(string[i+1]=='\n'||string[i+1]==' '||string[i+1]=='\t')
{
hist[j]=lc;
lc=0;
j++;
}
}
hist[j]=lc;
hist[j+1]='\0';
for(i=0;hist[i]!='\0';i++)
if(max<hist[i])
max=hist[i];
for(max=max;max>0;max--)
{
for(i=0;hist[i]!='\0';i++)
{
if(max!=hist[i])
printf("_");
if(max==hist[i])
{
printf("*");
hist[i]--;
}
}
printf("\n");
}
return 0;
}


The output is as follows:





Detailed explanation about the program:

ABOUT STDIO.H
STANDARD INPUT AND OUTPUT HEADER FILE
     'Stdio.h' is a header file. Header files are the files with an extension '.h' after the name of the file.  Header files are the set of defined functions in C source files. These defined functions are very helpful in compiling and execution of the code. Including of these header files is same as copying the functions in the code. But, using of predefined source files with header files is very flexible to understand and write the code. It also helps in debugging also. The following functions are written in the stdio.h file.

printf (): - This function is used to print whatever the user wants to print.
scanf (): - This function is used to assign the input value given by the user into a variable or to an array.
getc (): - This function reads a character from the file.
Gets (): - This function reads the entire line given by the user.
getchar (): - This function reads the character given by the user.
Puts (): - This function prints the line on the output screen.
putchar (): - This function prints a character on the output screen.

Clearer (): - This function clears the error indicators.



--------------------------------------------------------------------------------------------------------------------------


char string[1000],hist[100];

Declaration of two arrays with sizes 1000 and 100. The first array is to store the string entered by the user. The second array is to store the number of letters in each word in an order from left to right.

int i,lc=0,max=0,j=0,nofwrd=1;

Declaration of integer type variables.  Integer 'i' is used in for loop to control the addressed of the each character in the string. 'lc' is used to find the number of letters in each word.  'j' is used in nested for loop. 'max' is used for finding the maximum value in the array. 'nofwrd' is used to count the number of words in the given string.

printf("enter any string\n");

Message to the user to give input to the program.

gets(string);

Command that stores the given input to the first array.

for(i=0;string[i]!='\0';i++)
       if(string[i]!='\n' && string[i]!=' ' && string[i]!='\t')
           if(string[i+1]=='\n'||string[i+1]==' '||string[i+1]=='\t')

               nofwrd++;

The for loop is to find the number of words in the given string. The conditions in both the if statements are used to find the end of the word and to increase the value of 'nofwrd' by one.

for(i=0;string[i]!='\0';i++)
           {
               if(string[i]!='\n' && string[i]!=' ' && string[i]!='\t')
                   lc++;
               if(string[i+1]=='\n'||string[i+1]==' '||string[i+1]=='\t')
               {
                   hist[j]=lc;
                   lc=0;
                   j++;
               }

           }
This loop is used to fill the second array with the sizes of each word in the given string by the user.  After every iteration the 'lc' value is sent into the second array for further use.  After counting the each word the value of 'lc' is assigned as zero to count from starting of the new word length.

 hist[j]=lc;

The condition we have written fails at the end of string because the end character in a string is null character. The condition fails and the last word length is not entered into the array. So, this line is added to store the length of last word in the string.

hist[j+1]='\0';

This line is to put a null character in the array after filling all the lengths of words in the string.

for(i=0;hist[i]!='\0';i++)
               if(max<hist[i])
                   max=hist[i];

This for loop is to find the maximum value in 'hist[i]'.

 for(max=max;max>0;max--)
               {
                   for(i=0;hist[i]!='\0';i++)
                   {
                       if(max!=hist[i])
                           printf("_");
                       if(max==hist[i])
                       {
                           printf("*");
                           hist[i]--;
                       }
                   }
                   printf("\n");
               }

This for loop is the main logic of the program.  This loop creates the histogram of lengths of words  in the given string.  The condition checks the value in each box of 'hist[]', when the value gets equal to it it prints the character '*' else it leaves a space. After printing the character '*' the value in the box is decreased by one to print the character again at the same place by decreasing the 'max' value by one. After checking all the boxes in 'hist[]' the computer prints a new line to get the picture as a histogram.


C is a general-purpose programming language. It has been closely associated with the UNIX system where is was developed, since both the system and most of the programs that run on it are written in C. The language, however, is not tied to any one operating system or machine; and although it has been called a “system programming language” because it is useful for writing compilers and operating systems, it has been used equally well to write major programs in many different domains.(c programming examples)(c program examples)
Many of the important ideas of C stem from the language BCPL, developed by Martin Richards. The influence of BCPL on C proceeded indirectly through the language B, which was written by Ken Thompson in 1970 for the first UNIX system on the DEC PDP-7.(c programming examples)
BCPL and B are “typeless” languages. By contrast, C provides a variety of data types. The fundamental types are characters, and integers and floating point numbers of several sizes. In addition, there is a hierarchy of derived data types created with pointers, arrays, structures, and unions. Expressions are formed from operators and operands; any expression, including an assignment or a function call, can be a statement. Pointers provide for machine-independent address arithmetic.(c programming examples)(c program examples)
C provides the fundamental control-flow constructions required for well-structured programs: statement grouping, decision making (if-else), selecting one of a set of possible cases (switch), looping with the termination test at the top (while, for) or at the bottom (do), and early loop exit (break).(c programming examples)(c program examples)
Functions may return values of basic types, structures, unions, or pointers. Any function may be called recursively. Local variables are typically “automatic,” or created anew with each invocation. Function definitions may not be nested but variables may be declared in a block-structured fashion. The functions of a C program may exist I separate source files that are compiled separately. Variables may be internal to a function, external but know only within a single source file, or visible to the entire program.(c programming examples)(c program examples)
A preprocessing step performs macro substitution on program text, inclusion of other source files, conditional compilation.(c programming examples)(c program examples)
C  is a relatively “low level” language. This characterization is not pejorative; it simply means that C deals with the same sort of object that most computers do, namely characters, numbers, and addresses. These may be combined and moved about with the arithmetic and logical operators implemented by real machines.(c programming examples)(c program examples)
C provides no operations to deal directly with composite objects such as character strings, sets, lists, or arrays. There are no operations that manipulate an entire array or string, although structures may be copied as a unit. The language does not define any storage allocation facility other than static definition and the stack discipline provided by the local variables of functions; there is n heap or garbage collection. Finally, C itself provides no input/output facilities; there are no READ or WRITE statements, and no built-in file access methods. All of these higher-level mechanisms must be provided by explicitly called functions. Most C implementations have included a reasonably standard collection of such functions.(c programming examples)(c program examples)

Counting the total number of occurrences of a given word in a string using a C program

No comments :

Post a Comment