Wednesday 18 January 2017

Finding the index of last occurrence of a character in a string using C programming(c programming examples)(c program examples)

Finding the index of last occurrence of a character in a string using C programming

The example is based on using a function in C programming. And this example is to find the index of the last occurrence of a character in a given string.


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 index(char c, char * string)
{
int i,j=-1;
for(i=0;string[i]!='\0';i++)
{
if(string[i]==c)
j=i;
}
return j;
}
int main()
{
char c,string[1000];
int res;
printf("enter any string\n");
gets(string);
printf("enter any character to find\n");
scanf("%c",&c);
res=index(c,string);
if(res==-1)
printf("the character you have entered is not in the string\n");
else
printf("the last occurance index of the entered character %c is %d\n",c,res+1 );
return 0;
}


The output is as follows:





#include <stdio.h>


The '#include' is a preprocessor directive which includes the source files inside the code. The 'stdio.h' is a standard input and output header file with several functions written in it. The functions like 'printf()''gets()' and 'scanf()' are work when we include 'stdio.h' header file. 


char c,string[1000];

The data type 'char' is used determine to the computer to hold the character type information in the variables which declared with the data type 'char'. The array 'string[]' is used to store the string which is entered by the user. The variable 'c' is used to carry the character which is to be found in the string.


int res;

The index value is an integer so to hold the integer value we declare a variable named it as 'res'. And 'int' is a data type used to carry integer type data in the variables.


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

The functions 'printf()', 'gets()', and 'scanf()' are predefined functions which are saved in the header file 'stdio.h'. The first line is a message to the user to enter the input into the computer. The 'gets()' function stores the input given by the user into the array 'string[]'. Again the program needs a character to find in the string. Again a message is printed on the screen to give the input of character which is to be found in the string. the function 'scanf()' assigns the input into the variable 'c'.

res=index(c,string);

Here the command 'index()' send the value of character and string into the function for further execution process. And the value which is returned by the function is stored in the variable 'res'.

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

The arguments which are written in the parathesis of the function are assigned with the values given by the main function. In the body of the function, we declare two variables 'i' and 'j'. The variable 'i' is used in iteration processes in for loop and the variable 'j' is used to store the index value of the character. If the character is not found the function returns the value '-1'. If the character is found the value of 'i' is assigned to 'j'.

if(res==-1)
printf("the character you have entered is not in the string\n");
else
printf("the last occurance index of the entered character %c is %d\n",c,res+1 );


If the value of 'res' is found to be '-1' the 'if condition' is satisfied and the message "the character you have entered is not in the string" is printed on the screen. Else the next line is executed and the index value is shown to the user by printing on the screen by the computer. Hence the problem is solved by using a program. And another example program 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