Saturday 21 January 2017

Finding the maximum occurrences of a character in a given string in C programming(c programming examples)(c program examples)

Finding the maximum occurrences of a character in a given string in C programming

This program is little tricky. I browse this program in the google search. I have found one code for this problem. But the program is used to print only one character if there so many characters with the same number of minimum repetitions. So, I write my own program to print the characters if they are repeated with same less number of times.


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>
int allid(char c, char * string)
{
int i,j=0;
for(i=0;string[i]!='\0';i++)
{
if(c==string[i])
j++;
}
return j;
}
int main()
{
char string[1000],countc[1000];
int i,j=0,k=0,l=0,res,count[100],max=0;
printf("enter any string\n");
gets(string);
for(i=0;string[i]!='\0';i++)
{
res=allid(string[i],string);
for(j=0;j<i;j++)
if(string[i]==string[j])
l++;
if(l==0)
{
countc[k]=string[i];
count[k]=res;
k++;
}
l=0;
}
for(i=0;i<k;i++)
if(max<count[i])
max=count[i];
printf("the maximum repeated characters in a given string are as below:\n");
for(i=0;i<k;i++)
if(max==count[i])
printf("the character \'%c\' is repeated %d times\n",countc[i],count[i]);
return 0;
}


The output is as follows:





#include <stdio.h>


The '#include' directive is generally used to include header files into the source code.  The preprocessor inserts the functions of the header files at the points where they are needed.

SYNTAX: #include <header_file>

In C programming there are several pre-defined functions to help programmers to understand the code and to write with an effective way so that if any user reads the program he'll understand what is happening in the program. The header files 'stdio.h' and 'string.h' contains pre-defined functions, and they are used for execution when they are needed by the compiler.


char string[1000],countc[1000];

int i,j=0,k=0,l=0,res,count[100],min;

'char'and 'int' are two data types in C use to carry data. The date type 'char' used to carry character data and the data type 'int' is used to carry the integer type data. Out task is to find the lowest frequency characters in the given string. So to hold the 'string' we declared an array and it is named as 'string[]'. The array 'countc[]' is used to store the unique characters in the string. 

The variable 'i''j''k' are used to control the iterations in the for loop. The variable 'res' is used to count the number of repetitions of each unique character in the string. The array 'count[]' is declared to store the number of repetitions of each unique variable. The variable 'min' is to find out the minimum occurrences of characters which are the main theme of the program.

printf("enter any string\n");
gets(string);

The computer prints the message "enter any string" as notification that to enter the string. After entering the string, computer stores the string in the array 'sting[]' by using the function 'gets()'

for(i=0;string[i]!='\0';i++)
{
res=allid(string[i],string);
for(j=0;j<i;j++)
if(string[i]==string[j])
l++;
if(l==0)
{
countc[k]=string[i];
count[k]=res;
k++;
}
l=0;
}

It is a nested for loop which is used in this program to find out all the unique characters and their repetitions and to store them in the two arrays named as 'countc[]' and 'count[]'.  In the step  res=allid(string[i],string); 
the function 'allid()' is called by the computer to find out the number of repetition the character holding in the address 'string[i]' in the whole string.  And then the for loop comes into action and it checks whether the character in the address 'string[i]' is repeated up to that mark previously. If it is repeated the value of 'l' is increased by one. If it is repeated it won't be assigned into the array 'countc[]'. If it not repeated up to that mark the character is assigned to the array and the number or repetitions of that character are assigned into the array 'count[]'. After filling a compartment of two arrays the value of 'k' is increased to store the another character. 

for(i=0;i<k;i++)
if(max<count[i])
max=count[i];

This loop is to find the minimum value among the whole elements in the array 'count[]'. Because by finding the value of 'min' only we can check to a corresponding address of both the arrays and to print them on the output screen.

for(i=0;i<k;i++)
if(max==count[i])
printf("the character \'%c\' is repeated %d times\n",countc[i],count[i]);

This for loop checks each value in the 'count[]' and prints the both corresponding character and the number of repetitions of that character on the output screen. Hence the problem solved and another code gets into my bucket.


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)

No comments :

Post a Comment