Saturday 21 January 2017

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

Removing all occurrences of a character in a string using a C program

This program is to remove all the character in the whole string.  A simple function is used in the program to get the output. And we use some Conditions and basic C programming.


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>
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';
}
main()
{
char string[1000],remove;
printf("enter any string:\n");
gets(string);
printf("enter the character to remove:");
scanf("%c",&remove);
removefirst(string,remove);
printf("%s\n",string );
return 0;
}




The output is as follows:






#include <stdio.h>
#include <string.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.


     This header file 'string.h' includes the string handling functions.  For several activities on strings, there are several predefined functions in C source files to do an operation in code with less number of lines.

char string[1000],remove;

Our problem is to find a particular character given by the user and to find all the occurrences of that character in the string and remove it. So we use the data type 'char' to hold the array variable 'string[]' and variable 'remove' to hold the character which is to be removed.

printf("enter any string:\n");
gets(string);
printf("enter the character to remove:");
scanf("%c",&remove);

These are the common messages for the user to enter the input into the program to get the desired output.  The 'printf()' function is used to print the message and the 'gets()' function is used to store the given string in the character array 'string[]'. The function 'scanf()' is used to store the character in the variable 'remove'.

removefirst(string,remove);

This step calls the function which is written before the main function. This line sends the arguments to the function for the output result.

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

In this function, 'char * a' and 'char a' are the arguments. When we call the function, we should give the function some values to solve them.  In the first line, we declared the data type 'int' to declare the variables 'i' and 'len'. The function 'strlen()' is used to find the length of the array. After finding the length there is loop in the next step. In this loop computer checks whether the character matches the characters in the string. Here we use a nested for loop. It is a slight tough to write without any practice. If you have any doubt in that please, practice two to three times then you will get an idea that how a nested loop works. When it is found that the two characters are equal the position where the value get equal is replaced by the character succeeding to it. And it is repeated up to the end. At the end, the string is placed with a null character to print the string without any garbage values.


printf("%s\n",string );


This line prints the string after modification what the user want. The output is shown as in the figure. Hence the problem is solved and new 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