Saturday, 21 January 2017

Replacing all occurrences of a character with another using a C program (c programming examples)(c program examples)

Replacing all occurrences of a character with another using a C program 

The given problem is about to replace a particular character in the string. It is a very simple task with some basic C programming and 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>
main()
{
char string[1000],remove,replace;
int i,len;
printf("enter any string\n");
gets(string);
printf("enter the character to remove and the character to replace\n");
scanf("%c %c",&remove,&replace);
len=strlen(string);
for(i=len;i>=0;i--)
{
if(remove==string[i])
{
string[i]=replace;
}
}
printf("%s\n",string);
return 0;
}


The output is as follows:





#include <stdio.h>
#include <string.h>

These are just the preprocessor directives to include the functions in those header files into the code when they are needed. We use the following functions in the above program.

printf() :- To print something that is needed for the user from the computer.
gets() :- To assign a string into an array.
strlen() :- To find out the length of the string.

char string[1000],remove,replace;

We are operating with strings. A string is a group of characters. To hold the character values we use the data type 'char'. According to the problem, we need just an array, and two other variables to check and replace. The variable 'remove' is removed and replaced by the variable 'replace'.

int i,len;

The integer data type 'int' holds the integer value in the variable. The variable 'i' is used in the for loop and the variable 'len' holds the length of the string. It is user's choice to use 'len'. Without this variable, you can write the program to get the desired output.


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

In the above steps the computer prints the message on the screen that to "enter any string". And it stores the string in the array 'string[]'. After entering the string there is another message to enter the character you want to remove and the character you want to replace the removed character. We use '%c' to scan the input from the user.



for(i=len;i>=0;i--)
{
if(remove==string[i])
{
string[i]=replace;
}
}

It is a simple for loop to find the character which the user want to remove and place the character entered by the user.In the loop, we initialize the variable 'i' with the value which is equal to the length of the string. It is a reverse process. The searching of the variable starts from the last and moves to the begin of the string. So we take the condition 'i>=0'. If the value of 'i' becomes negative the loop terminates and the execution starts after the loop. After every iteration the value of 'i' is decreased by one to move towards left. The 'if condition' helps in finding the removable character. When the character is found the body of 'if condition' will execute and the box of the array is placed by the character which is stored in the variable 'replace'.


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

After replacing we should print the output string to ensure that it is done perfectly. So, we use printf() function to print the string. 



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)

4 comments :