Sunday, 22 January 2017

Finding all occurrences of a given word in a string using a C program(c programming examples)(c program examples)

Finding all occurrences of a given word in a string using a 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,len1,posi=0;
printf("enter the string\n");
gets(string);
len1=strlen(string);
printf("enter the word to identify its last position\n");
gets(word);
len=strlen(word);
j=len-1;
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++;
}
printf("the all occurrence of the word \'%s\' indexes are given below\n", word);
for(i=len1;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])
{
if(j>0)
j--;
if(j==0 && len==1)
{
posi=index+1;
printf("%d\t",posi );
}
if(j==0 && len!=1)
{
posi=index;
printf("%d\t",index );
}
}
if(j==0)
j=len-1;
}
if(posi==0)
printf("the word you enetered is not in the string\n");
return 0;
}


The output is as follows:



ABOUT STDIO.H
STANDARD INPUT AND OUTPUT HEADER FILE
     '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. The following function is written in the stdio.h file.

#include <string.h>

ABOUT STRING.H

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

char string[1000],word[100];

Declaring two arrays, one is to store the string entered by the user and second one is to store the desired word to find its occurrences in the string.

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

The variable 'count' is used to count the number of occurrences of the word.  The variable 'i'  and 'j' are used in for loops for iterations on characters in the string. The variable 'index' is used to find the total number of words in the string.  The variables 'len' and 'len1' are used to find the length of the string entered by the user and the length of the word which is entered to find its total number of occurrences in the string.  The variable 'posi' is used to find the position of the word in the string.

printf("enter the string\n");

A message to the user to give input.

 gets(string);

To store the given input string into an array named as the string.

len1=strlen(string);

To find the length of the array using a string function.

printf("enter the word to count its number of occurrences\n");

A message to the user to give a word to find its number of occurrences.

gets(word);

Command to store the word into 'word[]'.

len=strlen(word);

Finding the length of the word using a string function.

j=len-1;

To assign the size of 'word[]' into j for further use in the for loop.

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


A for loop to find the total number of words in the given string.

printf("the all occurrence of the word \'%s\' indexes are given below\n", word);

A message to the user about the values printed below.

  for(i=len1;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])
        {
         if(j>0)
            j--;
        if(j==0 && len==1)
        {
            posi=index+1;
            printf("%d\t",posi );
        }
        if(j==0 && len!=1)
        {
            posi=index;
            printf("%d\t",index );
        }
    }
    if(j==0)
     j=len-1;

}

This for loop is about to find the position of the word desired by the user to find its occurrence in the string. In this loop there are several conditions to get the result.  If the word is found once then the value of 'j' becomes zero. After then the if condition checks the value of 'j' and reassigns the value of 'len-1' to 'j' to continue its iteration to find the occurrence of the word if any.

if(posi==0)
  printf("the word you entered is not in the string\n");

If the word is not in the string the value of 'posi' cannot be zero. So, this condition helps to find whether the word occurs in the string or not.



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)

12 comments :

  1. Replies
    1. Big data is a term that describes the large volume of data – both structured and unstructured – that inundates a business on a day-to-day basis. big data projects for students But it’s not the amount of data that’s important.Project Center in Chennai

      Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Corporate TRaining Spring Framework the authors explore the idea of using Java in Big Data platforms.

      Spring Training in Chennai

      The new Angular TRaining will lay the foundation you need to specialise in Single Page Application developer. Angular Training

      Delete
  2. Top Technologies to learn
    Excellent blog with lots of information. I have to thank for this. Do share more.

    ReplyDelete
  3. It's very useful article with inforamtive and insightful content and i had good experience with this information. We, at the CRS info solutions ,help candidates in acquiring certificates, master interview questions, and prepare brilliant resumes.Go through some helpful and rich content Salesforce Admin syllabus from learn in real time team. This Salesforce Development syllabus is 100% practical and highly worth reading. Recently i have gone through Salesforce Development syllabus and Salesforce Admin syllabus which includes Salesforce training in USA so practically designed.

    ReplyDelete
  4. I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next! keep it up
    angular js training in chennai

    angular js online training in chennai

    angular js training in bangalore

    angular js training in hyderabad

    angular js training in coimbatore

    angular js training

    angular js online training

    ReplyDelete
  5. Top Courses to learn

    I am glad that I have visited this blog. Really helpful, eagerly waiting for more updates

    ReplyDelete