Saturday 21 January 2017

Finding the position of first occurrence of a word in a given string using C program(c programming examples)(c program examples)

Finding the position of first occurrence of a word in a given string using C program

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],word[100];
int i,j=0,index=1,len,posi=0;
printf("enter the string\n");
gets(string);
printf("enter the word to identify its position\n");
gets(word);
len=strlen(word);
for(i=0;string[i]!='\0';i++)
{
if(string[i]!='\n' && string[i]!=' ' && string[i]!='\t')
if(string[i+1]=='\n'||string[i+1]==' '||string[i+1]=='\t')
index++;
if(word[j]==string[i])
{
j++;
if(j==len-1)
posi=index;
}
}
if(posi==0)
printf("the word you enetered is not in the string\n");
else
printf("the word you entered contains the word index %d in the string\n",posi );
return 0;
}


The output is as follow:


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


'#include' is a preprocessor directive used to include header files like 'stdio.h', 'string.h' etc..  To execute the function 'printf()', 'gets()' and 'strlen()' we should include these header files. The computer calls the functions whenever they are needed in the execution process of the program.

char string[1000],word[100];

The given problem is to find the first occurrence of the word in a given string.  So, there are two strings both are the inputs given by the user. To hold the string characters the data type 'char' is used and to hold the strings, arrays should be declared. So we declare 'string[1000]' and 'word[100]'. The two arrays are very useful in the execution of code.


int i,j=0,index=1,len,posi=0;

'int' is an integer data type to declare integer holding variables. The variable 'i' is used in the iteration process in the for loop. The integer 'j' is used to check the occurrence of the word. The integer 'index' is used to count the total number of words in the string. The integer variable 'posi' is used to store the position of the word if it has an occurrence of the string.


printf("enter the string\n");
gets(string);
printf("enter the word to identify its position\n");
gets(word);


The printf function is to print the message to tell the user to enter the input into the computer. After entering the input the computer will take the input with the command 'gets()'. Again another message to enter the desired word to be searched in the string. And this input is stored in the array 'word[]' for further use.

len=strlen(word);

To find the length of the word we are using the string function 'strlen()'. This value is used in checking process of the word in the array[].



for(i=0;string[i]!='\0';i++)
{
if(string[i]!='\n' && string[i]!=' ' && string[i]!='\t')
if(string[i+1]=='\n'||string[i+1]==' '||string[i+1]=='\t')
index++;
if(word[j]==string[i])
{
j++;
if(j==len-1)
posi=index;
}
}

This is the main logic of the program to find the first occurrence of the word. The two 'if conditions' are used to find the indexes of the words in the string. Simultaneously the next 'if condition' will check the occurrence of characters in the array 'word[]' whether they are found in the array 'string[]'. If the first character of the word is found in the 'string[]' then the value of 'j' is increased to check the succeeding character of the word and in the string.


if(posi==0)
printf("the word you enetered is not in the string\n");
else
printf("the word you entered contains the word index %d in the string\n",posi );

If the word is found the value of 'posi' should not be zero. This condition will print the right statement for the output.


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)

1 comment :