Jump to content
  • Advertisement
Sign in to follow this  
someone somewhere

strstr problems

This topic is 4492 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, i have to write that functio that takes astring (string1) as parameter. the function should ask the user to enter astring(string2)and then count and print the number of times that (string2) occurs in (string1). the function should count lower and upper cases??? .use strstr to slove this problem. this was my answer: void display_substring_frequency(char*str) { cout<<"enter astring"<<endl; int c=0; while(cin.get()!='\n') continue; char arr[20]; cin.getline(arr,20); while(*str!=NULL){ strstr(str,arr); c++; *str++;}// i just need away to let the strstr start searching from the last occurs of arr cout<<"the string\t"<<arr<<"\tis repeated\t"<<c<<"\ttimes"<<endl; }

Share this post


Link to post
Share on other sites
Advertisement
case sensitive

unsigned int strcount(char *h, char *n)
{
unsigned int c, l = strlen(n);

for (c = 0; h = strstr(h, n); ++c) h += l;

return c;
}



however, to ignore case, you could just do something like this

unsigned int strcount(char *h, char *n)
{
unsigned int c, l;

for (l = 0; n[l]; l++) n[l] = tolower(n[l]);
for (c = 0; h[c]; c++) h[c] = tolower(h[c]);
for (c = 0; h = strstr(h, n); ++c) h += l;

return c;
}

Share this post


Link to post
Share on other sites
Why aren't you using std::string? You're using cout, so you're obviously using c++. The string class has the find method:

size_type find (const string& str, size_type pos);

Search for the first occurrence of the substring str in the current string, starting at position pos. If found, return the position of the first character. If not, return a special value (called string::npos). The member function rfind does the same thing, but returns the position of the last occurrence of the specified string.
string str16 = "abcdefghi";
string str17 = "def";
string::size_type pos = str16.find (str17,0);
cout << pos << endl; // 3
pos = str16.find ("AB",0);
if (pos == string::npos) cout << "Not found" << endl;


With that said, here's how you might do it in c++

#include <string>
#include <iostream>
using namespace std;

int display_substring_frequency(const string &substring, const string &superstring)
{
int freq = 0;
string::size_type pos = 0;
while(pos !- string::npos)
{
pos = superstring.find(substring, pos);
if(pos != string::npos) { freq++; pos += substring.length()}
}
return freq;
}

int main()
{
cout << "Enter a string: ";
string first;
getline(cin, first);
cout << "Enter a string to search: ";
string second;
getline(cin, second);

int frequency = display_substring_frequency(second, first);
cout << "The number of occurances of " << second << " in " << first << "was " << frequency << endl;
return 0;
}



I haven't been able to test this out, but I think you get the idea in case it doesn't. Hope that helps.

Share this post


Link to post
Share on other sites
I just realized that my code might not behave identically to the way you may want it. take the string "aaaa" and the substring you're looking for is "aa" then mine will return a frequency of two, but you might want it to return 3, because you can have "AAaa" aAAa" and "aaAA" possible substrings. If you want the second just change pos+=substring.length() to pos+=1;

Share this post


Link to post
Share on other sites
Quote:

while(pos !- string::npos)
...
if(pos != string::npos) { freq++; pos += substring.length()}

while(pos != string::npos) //!- -> !=
...
if(pos != string::npos) { freq++; pos += substring.length();} //...length();

=)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!