Hey, i was reading up on find function and i came across this line.

size_t find ( const char* s, size_t pos, size_t n ) const;

Correct me if im wrong, but it basically allows us to search a string based on the information inside an array right?

After searching online , i realize that alot of people use it to check how many times a certain character which they input will be in the array.

for example.

array = ["ttt ttt ttt"]
basically , they will input in the character they want to find ,(in this case 't) , and the prog will count how many times 't' appears in the array.


What i am curious to know if this function can be used like the way i did below. i tried typing out this code but it dosnt seem to work. What i am trying to do is to input in a string and then using the array to find if any of the characters in the array exist.

int main()
{
   string str = "BE";

   int b = 0 ;

   const int arraySize2 = 19;
   char array2[arraySize2] ={'A','B','E','F','G','H','J','K','N','O','P','Q','R','S','T','U','W','Y','Z'};

   for (int i = 0 ; i < str.length() ; i ++)
     int b = str.find(array2[i]);

   cout << b << endl;

Is it possible to be used this way and if it is, what is the problem with my coding?

Recommended Answers

All 8 Replies

variable b is declared twice. Remove the declaration on line 11. And it should be size_t, not int

Instead of iterating over the string, you should be iterating over the array. Replace < str.length() with < arraySize2

Otherwise you are looking for only A & B.

Also follow AD's point of multiple declaration.

@gregarion, I'm not sure what you're trying to do with the array str.. But I guess your goal was to find the number of occurence of certain characters in the array str. But that won't be given by find function.
size_t find ( const char* s, size_t pos, size_t n ) const;
find function basically returns the position of the first occurence of the sequence s(only first n characters considered) in the string str(starting from position pos).. If no occurence, it returns -1.
so, str("Hello").find("ll") will return 2(coz ll is at position 2), str("Hello").find("ll", 3) will return -1(coz ll is not present starting from position 3, i.e. in "lo") and str("Hello", 3).find("ll", 3, 1) will return 3(coz l is at position 3 in "***lo").
http://www.cplusplus.com/reference/string/string/find/

If you want to find out number of occurences, do this:

int _occurences(const string& str, char s, size_t start)
{
	if (start == str.length())
		return 0;
	else
		return _occurences(str, s, start+1) + ((str[start] == s) ? 1 : 0);
}

int occurences(const string& str, char s)
{
	// assuming str, s are non-null
	return _occurences(str, s, 0);
}

int main()
{
   string str = "BE";

   /*int b = 0 ;*/

   const int arraySize2 = 19;
   char array2[arraySize2] ={'A','B','E','F','G','H','J','K','N','O','P','Q','R','S','T','U','W','Y','Z'};

   for (size_t i = 0 ; i < arraySize2 ; i ++)
   {
     //int b = str.find(array2[i]);
	 cout << "Occurences of " << array2[i] << " = " << occurences(str, array2[i])
		 << endl;
   }


   /*cout << b << endl;*/
}

@tintin,

Recommending the use of a recursive solution is rather an obscure method of helping someone out. Since everyone knows that a recursive function isn't exactly the most efficient solution by any means, although in normally provides a neater looking solution that is easier to code. 99 times out of 100 they turn out to be resource hungry machines.

Chris

commented: Yup +27

@Freaky_Chris, I agree that recursion is a path best avoided unless you're too suicidal or too good. But I wasn't encouraging it in any way, plus in this scenario I was more than sure that recursion is a pretty safe, even though lucrative, way to go.
@gregarion, please take note that as Freaky_Chris pointed out, recursion is something we should avoid if we can.. So please don't just believe in copying others' codes( or methods in general), evaluate things for yourself; in this case, work out the non-recursion based occurance() function.

commented: Yup +27

i cant use the string.find as it only finds one for one char at a time. Thus if i were to use it, i would have to type it out 19 times? i am thinking if there is a shorter way to do it... I tried doing this...

bool testRoman(string r) {
        const int arraySize2 = 19;
        char array2[arraySize2] ={'A','B','E','F','G','H','J','K','N','O','P','Q','R','S','T','U','W','Y','Z'};
       
         
  

         for(int c = 0 ; c < arraySize2 ; c++)
                    for(int e = 0; e < r.length(); e++)
                        if(array2[c] == r[e])
                        {
                            return true ;
                            
                        }
         
              
              else {
                  return false ;
              }
        }





       int main()
    {
           string r = "DDA";

     

           testRoman(r);
  
          if ( testRoman(r)){
             cout << "got number" << endl;

         }
          else{
              cout << "romans" << endl;
          }



}

When my input string contain a single alphabet, it is able to detect if the character is in the array or not. But when i input something like "DDR" , it displays the output saying "roman" when it should be displaying "got number" as there is a 'R' in the array.

What i am trying to do is to search the entire string for any character in the array. If such a character is detected, the program will then output a message and stop the check.

Is there a way to correct my array so it can do such a function?

This :

char array2[arraySize2] ={'A','B','E','F','G','H','J','K','N','O','P','Q','R','S','T','U','W','Y','Z'};
   for (int i = 0 ; i < str.length() ; i ++)
     int b = str.find(array2[i]);

can be replaced with this :

if( str.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZ") == string::npos){
 letter not found.
}

but it seems that you are trying to find the number of occurrence of a
letter right ? If so then what you should is just loop over the string
and count how many letters occur there of certain type.

Oh read your last post. What you need to use is string.find_first_not_of().
Here is another example :

string romanCharacters = "IVXLCDM";
string str1= "XXIID";
string str2= "21341";
if( str1.find_first_not_of(romanCharacters) == string::npos){
  cout <<str1 << " is roman numerials\n";
}
 else cout << str1 << " is not roman numerials\n";

if(str2.find_first_not_of("0123456789") == string::npos){
 cout << st2 << " is not a number\n";
}
else cout << str2 << " is a number\n";
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.