In: Computer Science
In this program:
==================================================================
/* Program to count number of occurrences of a given string in
original string*/
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
const int SIZE = 40;
char str[SIZE];
char str1[SIZE];
char searchString[SIZE];
int n;
int l1, l2;
int count = 0;
printf("Enter a sentence: \n");
fgets(str,SIZE,stdin);
printf("Enter a search word: \n");
fgets(searchString,SIZE,stdin);
if (str1[strlen(str1) - 1] == '\n')
{
str1[strlen(str1)-1] = '\0';
}
if (str[strlen(str) - 1] == '\n')
{
str[strlen(str)-1] = '\0';
}
if (searchString[strlen(searchString) - 1] == '\n')
{
searchString[strlen(searchString)-1] = '\0';
}
printf("\n");
//Original string is contained in str
l1 = strlen(str);
l2 = strlen(searchString);
for(int i = 0; i < l1 - l2 +1; i++)
{
//Using strstr all the occurences are counted through iterating
each successive location in the original string
if(strstr(str + i, searchString) == str + i)
{
//When search string is found in original string, count is
increased and original string location to search is moved to the
length of search string to find next occurences
count++;
i = i + l2 -1;
}
}
cout << "Found "<< count << " words of "<<
searchString<<" in: \n" << str;
return 0;
}
=============================================================================
can someone explain to me what is going in in this loop i am so lost...The only thing i understand is the count++;
The part im having trouble understanding is why:
i < l1 - l2 +1 and also strstr(str + i, searchString) == str + i (like why the '+ i') and also why i = i + l2 -1;
for(int i = 0; i < l1 - l2 +1; i++)
{
if(strstr(str + i, searchString) == str + i)
{
count++;
i = i + l2 -1;
}
I have included comments explaining each important statement in this program. Please find the commented program below. Go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks
Note: The given program is written terribly. There is a lot of bad programming practises followed and is a mixture of C and C++. I have explained everything as much as I can. I am not sure you could follow that easily, So I have written a modified version of this program at the bottom, which is way more efficient and easy to understand and is in pure C++ format. Have a look at it if you want.
#include <iostream>
#include <cstring>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
//maximum size of input sentence
const int SIZE = 40;
//char array to store input sentence
char str[SIZE];
//there was no need for str1, so I removed it
//char array to store search term
char searchString[SIZE];
int n;
int l1, l2;
int count = 0;
//asking, reading input sentence
printf("Enter a sentence: \n");
fgets(str, SIZE, stdin);
//asking, reading search term
printf("Enter a search word: \n");
fgets(searchString, SIZE, stdin);
//if last character is a newline, replacing last char with null terminator ('\0')
//so that the newline char is not considered for anything.
if (str[strlen(str) - 1] == '\n') {
str[strlen(str) - 1] = '\0';
}
//doing the same to searchString
if (searchString[strlen(searchString) - 1] == '\n') {
searchString[strlen(searchString) - 1] = '\0';
}
printf("\n");
//finding length of input sentence
l1 = strlen(str);
//finding length of search term
l2 = strlen(searchString);
//looping from i = 0 to i = (l1 - l2)
//we iterate until i = l1-l2 because we are searching for searchString in str
//so we only need to traverse until the position in str where there could not be
//any more occurrences of searchString possible
//for example, if str is "abcdefh" and searchString is "abcd", we only need to
//move i from 0 to length("abcdefh") minus length("abcd"), or 7-4 = 3, after index
//3, it is guaranteed that there won't be another "abcd" possible, because the number
//of characters left after index 3 is 3 ("efh") which is less than length of search
//string "abcd".
for (int i = 0; i < l1 - l2 + 1; i++) {
//strstr will search for a target string in a given string.
//first parameter is the source string, second parameter is the string to be searched.
//it returns the first position of the searchString if found, else NULL.
//str is char array name, which is also pointer to the first position on the array
//so str+i points to the position i values away from the beginning.
//so if (strstr(str + i, searchString) returns the same position str+i, it means that
//one occurrence of the target string starts at current position
if (strstr(str + i, searchString) == str + i) {
//in that case we increment count
count++;
//advancing i l2 locations forward, to move into the next index after current
//occurrence of searchString, to continue search
//subtracting 1 from i because the for loop will increment i after each loop
//which will ultimately make i=i+l2 (not a very good programming here)
i = i + l2 - 1;
}
}
//finally displaying the count
cout << "Found " << count << " words of " << searchString << " in: \n" << str;
return 0;
}
/*MODIFIED PROGRAM (DIFFERENT, YET EASY TO UNDERSTAND APPROACH)*/
#include <iostream>
#include <string>
using namespace std;
int main()
{
//defining two string objects
string sentence, searchString;
//asking, reading sentence
cout<<"Enter a sentence: \n";
getline(cin, sentence);
//and the search string
cout<<"Enter search word: \n";
getline(cin, searchString);
//initializing count to 0
int count=0;
//we are using find method of string to search for a given string
//if found, the method returns the position of first occurrence, if not
//the method returns a value string::npos indicating the word is not found
int pos=sentence.find(searchString);
//we loop as long as pos is not string::npos
while(pos!=string::npos){
//incrementing count
count++;
//now we find next position of searchString if there's any
//find method can also take a second parameter which tells the system to
//search for a given string after that position. so here we search for
//searchString after pos+searchString.length() position
pos=sentence.find(searchString, pos+searchString.length());
}
//displaying count
cout << "Found " << count << " words of " << searchString << " in: \n" << sentence;
return 0;
}
/*OUTPUT*/
Enter a sentence:
some text containing some occurrences of the word 'some'
Enter search word:
some
Found 3 words of some in:
some text containing some occurrences of the word 'some'