Saturday 21 January 2017

Removing the last occurrence of a character in a string in C programming(c programming examples)(c program examples)

Removing the last occurrence of a character in a string in C programming

This example will help you to work with functions and sending values to functions and return the output from function to the main function. And you can get touch with loops. And basic C programming should necessary.


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])
break;
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>

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],remove;
printf("enter any string:\n");
gets(string);
printf("enter the character to remove:");
scanf("%c",&remove);

These steps are very easy to understand. The 'char' is data type in C which is useful to store the character data.  The array 'string[]' is declared to store the string entered by the user. The variable 'remove' stores the character which is to be removed in the string. In order to tell the user to give input to the computer the message "enter any string" is printed on the output screen by the computer using 'printf()' function. At the end of the message the character '\n', it represents new line character. It is not printed on the output screen but it is used to tell the computer to go for a new after printing the message. The computer waits for the user to give input. After giving some input and then pressing enter key, the computer then goes for execution of next line. In the next line, there is the 'gets()' function which assigns the given string into the array by placing a null character after filling all the required boxes. The assigning process will not be seen by the user. After we press the enter key within a nanosecond the computer prints the message "enter the character to remove:'.This message is a call to the user to give the input character to the computer. The computer waits for the input from the user. After giving the input value the execution goes on to the next line. The 'scanf()' function assigns the character entered by the user into the 'remove' variable.

removefirst(string,remove);

This a call to the function to do some operations with 'string' and 'remove'. The computer sends this values as arguments to the function 'removefirst()'

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

When the computer calls this function, it takes the values of 'string' and 'remove' into it. In the function, we declare the data type 'int' with two variable 'i' and 'len'.  The function 'strlen()' is used to find the length of the string. In the first for loop the computer finds whether the character is equal to the characters in the string. If it finds the character it breaks the loop and goes for the second for loop to delete that character from the string. The character is replaced by the succeeding character in the string. At the end, after finishing the last character a null character is placed to print the entire string without any garbage values.

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

The computer prints the output string after removing the last occurrence of the character.


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