Saturday, 21 January 2017

Removing all repeated occurrences of a characters in a string using a C program (c programming examples)(c program examples)

Removing all repeated occurrences of a characters in a string using a C program 

The program is about to find the characters which are repeated only one time. So, we can solve this by finding the number of repetitions of each character.


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;
}
removefirst(char * a,char c)
{
int i,len;
len=strlen(a);
for(i=len;i>=0;i--)
if(c==a[i])
for(i=i;a[i]!='\0';i++)
a[i]=a[i+1];
a[i]='\0';
}
int main()
{
char string[1000],countc[1000];
int i,j=0,k=0,l=0,res,count[100];
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(count[i]!=1)
removefirst(string,countc[i]);
printf("after removing all the repeated characters:\n");
printf("\n\n%s\n",string );
return 0;
}

The output is as follows:





#include <stdio.h>

 '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.

In this example, we used functions in C program. Function are very flexible to use. We can use the function whenever we want by calling it. By using functions we can get a clean code without any clumsiness.

We are using two functions in this problem to solve.  Some basic C programming, conditions, and loops this code is simple to write.

char string[1000],countc[1000];


The data type 'char' is used to carry the character values in the variable or in the arrays. We declared two arrays 'string[1000]', and 'countc[1000]'. The first array 'string[1000]' is used to carry the string given by the user as an input. The second array 'countc[1000]' is used to store the unique characters in the given string.

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

The data type 'int' is used to declare the variables to hold integer values. The variable 'i' is used for iteration purpose in the for loop. The variable 'res' is used in the function 'allid()' to return the value of the count of repetitions of each character. The array 'count[100]' is used to carry the number of repetitions of each unique character in the main string.

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

The 'printf()' function is used to print the message and the function 'gets()' is used to store the given input by the user.

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;
}

The logic used in the above for loop is used to find all the unique characters in the main string and to save them in the array 'countc[]' and also the repetitions of each unique character into the array 'count[]'. In this loop, we are calling the function by giving the value of individual character and the entire string. After getting the value from the function, the execution process continuous. Again there is a loop to check whether the character is repeated up to that position. If the character is repeated the value of 'l' is increased by one. Then the value of 'l' is checked by the 'if condition'. If the condition is satisfied the body of 'if condition' executes. In the body of 'if condition' the unique character is assigned into the compartment of the array 'countc[]' and the number of repetition of that particular character is assigned to the array 'count[]'. After assigning the value of 'l' should be assigned with '0' for the next iteration. The function is as below:

int allid(char c, char * string)
{
int i,j=0;
for(i=0;string[i]!='\0';i++)
{
if(c==string[i])
j++;
}
return j;
}

This function returns the value of repetitions of character. Inside this function, there is a loop to check all the characters in the string with the character which is given of the main function.

for(i=0;i<k;i++)
if(count[i]!=1)
removefirst(string,countc[i]);

This for loop removes the repeated characters in the string by calling the function 'removefirst()'. The function is as below:


removefirst(char * a,char c)
{
int i,len;
len=strlen(a);
for(i=len;i>=0;i--)
if(c==a[i])
for(i=i;a[i]!='\0';i++)
a[i]=a[i+1];
a[i]='\0';
}

The two arrays 'countc[]' and 'count[]' are connected with the information inside that. For every element in the array 'countc[]' has an information in the same element in 'count[]'. If the character is repeated more than one time the for loop deletes the character in the main string which is saved in the array 'string[]'.

printf("after removing all the repeated characters:\n");
printf("\n\n%s\n",string );

After deleting all the repeated characters the string is printed using the above lines.




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