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.
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.
#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)
I love this!!The blog is very nice to me. Im always keeping this idea in mind. I will appreciate your help once again. Thanks in advance.
ReplyDeletecore java training in chennai
core java course
core java Training in Adyar
clinical sas training in chennai
Spring Training in Chennai
QTP Training in Chennai
Manual Testing Training in Chennai
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
DeleteSpring 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
Well written post with worthy information. It will definitely be helpful for all. Do post more like this.
ReplyDeleteEthical Hacking course in Chennai
Ethical Hacking Training in Chennai
Hacking course
ccna course in Chennai
Salesforce Training in Chennai
Angular 7 Training in Chennai
Web Designing course in Chennai
Ethical Hacking course in Thiruvanmiyur
Ethical Hacking course in Porur
Ethical Hacking course in Adyar
An awesome blog. The author has really outdone himself this time. Great reading it.
ReplyDeleteSpoken English Classes in Chennai
Spoken English in Chennai
Top 10 Spoken English Classes in Chennai
Best IELTS Coaching in Chennai
IELTS Coaching Centre in Chennai
English Classes in Mumbai
English Speaking Classes in Mumbai
Best IELTS Coaching in Mumbai
IELTS Coaching in Mumbai
Spoken English Classes in Anna Nagar
The article is so informative. This is more helpful for our
ReplyDeleteselenium training in chennai
selenium online courses best selenium online training
selenium testing training
selenium classes
Thanks for sharing.
This is the first & best article to make me satisfied by presenting good content. I feel so happy and delighted. Thank you so much for this article.
ReplyDeleteLearn Best Digital Marketing Course in Chennai
Digital Marketing Course Training with Placement in Chennai
Best Big Data Course Training with Placement in Chennai
Big Data Analytics and Hadoop Course Training in Chennai
Best Data Science Course Training with Placement in Chennai
Data Science Online Certification Course Training in Chennai
Learn Best Android Development Course Training Institute in Chennai
Android Application Development Programming Course Training in Chennai
Learn Best AngularJS 4 Course Online Training and Placement Institute in Chennai
Learn Digital Marketing Course Training in Chennai
Digital Marketing Training with Placement Institute in Chennai
Learn Seo Course Training Institute in Chennai
Learn Social Media Marketing Training with Placement Institute in Chennai
Top Technologies to learn
ReplyDeleteExcellent blog with lots of information. I have to thank for this. Do share more.
Nice blog, very informative content.Thanks for sharing,waiting for next update...
ReplyDeletePhotoshop Classes in Chennai
Best Place to Learn Photoshop in Chennai
Photoshop Training Classes in Chennai
Photoshop Training in Anna Nagar
Photoshop Training in Tnagar
Drupal Training in Chennai
Manual Testing Training in Chennai
LoadRunner Training in Chennai
QTP Training in Chennai
C C++ Training in Chennai
awesome article,the content has very informative ideas, waiting for the next update...
ReplyDeletecore java training in chennai
Best core java Training in Chennai
core java course
core java training in Velachery
core java training in Tambaram
C C++ Training in Chennai
javascript training in chennai
Hibernate Training in Chennai
LoadRunner Training in Chennai
Mobile Testing Training in Chennai
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.
ReplyDeleteI 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
ReplyDeleteangular 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
Top Courses to learn
ReplyDeleteI am glad that I have visited this blog. Really helpful, eagerly waiting for more updates
There is no point in partnering with Salesforce Consultant Companies that are comprised of inexperienced staff. The consultants might present their experiences of handling a project and achieving success in their past where as a lot matters on the individuals who will be taking up your project and the ones who will be representing the company you are partnering with. Salesforce training in Hyderabad
ReplyDelete