strlen

This is a discussion on strlen within the C++ Programming forums, part of the General Programming Boards category; I am trying to make a program that takes a scrambled word and checks it against a list of possible ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    36

    strlen

    I am trying to make a program that takes a scrambled word and checks it against a list of possible words. I am having trouble wusing the strlen function. Here's my code:

    Code:
    #include <cstring>
    #include <cstdlib>
    #include <iomanip>
    #include <fstream>
    #include <iostream>
    using namespace std;
    
    bool check(string, string);
    
    int main()
    {
        string scrambled;
        string PossibleWord [13]={"will", "check", "it", "against", "list", "and", "return", "hello", "True", "or", "False", "Swap"};
        cout << "Please enter scrambled word: ";
        cin >> scrambled;
        for (int i=0;i<sizeof(PossibleWord[i]);i++)
        {
            if (scrambled.size()==PossibleWord[i].size())
            {
           	   if(check(PossibleWord[i], scrambled))
           	   {
                    cout<< "the word is " <<  PossibleWord[i]<<endl;
               }
            else
                cout<< "the word is not found"<<endl;
            }
        }
        system ("pause");
    }
    
    bool check(string p, string s)
    {
            bool match;
            int count=0;
            int psize, ssize;
            psize=strlen(p);
            ssize= strlen(s);
            cout << "Size of p "<< psize <<endl;
            cout <<"Size of s "<< ssize <<endl;
            for (int i=0; i< psize; i++)
            {
                for (int y=0; y< ssize; y++)
                {
                    if (p[i]==s[y])
                    {
                        cout << "Match with possible's "<< p[i] << " and scrambled's " << s[y]<< endl; 
                        ++count;
                    }
                }
            }
            cout << "Count is "<< count <<endl;
            if (count==psize)
            {
                   match=true;
            }
            else
                    match=false;
                    return match;
    }
    My errors say:
    no matching function for call to `strlen(std::string&)'
    candidates are: size_t strlen(const char*)

    My code is exactly like that of the book I'm using. Can someone please tell me what is wrong?

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What is wrong is that a std::string is not the same as a char *. If you need a C-style string out of a std::string, you can use mystring.c_str(). OTOH, I don't know why you don't use mystring.size() just like every other place in your code.

    And don't you need to #include <string> ?

  3. #3
    coder
    Join Date
    Feb 2008
    Posts
    127
    By the way, this will work too:
    Code:
    string a, b;
    if (a == b) {
       ...
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    What will sizeof(PossibleWord[i]) return you think?

    do not pass strings by value:
    Code:
    bool check(const string& p, const string& s);
    use proper types
    Code:
            size_t count=0;
            size_t psize, ssize;
            psize=p.size();
            ssize= s.size();
            for (size_t i=0; i< psize; i++)
            {
                for (size_t y=0; y< ssize; y++)
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    coder
    Join Date
    Feb 2008
    Posts
    127
    Quote Originally Posted by vart
    do not pass strings by value:
    why not?

  6. #6
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by carlorfeo View Post
    why not?
    because you request to execute the constructor of the temp object including memory allocation and data move when you just need the read only access to the original data.

    const ref parameter gives you this access without calling constructor and passing only 4/8 bytes on stack
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  7. #7
    coder
    Join Date
    Feb 2008
    Posts
    127
    I see, thanks

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,663
    Efficiency, mate! Efficiency!
    That's what C++ is all about!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> do not pass strings by value
    I think in many programs it is perfectly acceptable to treat strings as a value type, especially for someone who is just learning. The string class will often be taught before references, so passing by reference adds unnecessary complexity.

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    I think I have this part figured out now. i thought this would be the easy part! Here's what i got:

    Code:
    #include <cstring>
    #include <cstdlib>
    #include <iomanip>
    #include <fstream>
    #include <iostream>
    #include <string>
    using namespace std;
    bool check(string, string);
    int main()
    {
        bool match;
        string scrambled;
        string PossibleWord[12]={"will", "check", "it", "against", "list", "and", "return", "hello", "True", "or", "False", "Swap"};
        cout << "Please enter scrambled word: ";
        cin >> scrambled;
              for (int i=0;i<13;i++)
              {
                  if (scrambled.size()==PossibleWord[i].size())
                  {
                       match=(check(PossibleWord[i], scrambled));
                  }
                  if (match)
                  {      
                   cout<< "Match!\nThe word is " << PossibleWord[i] << ".\n"; 
                   break;
                  }
              }
              system ("pause");
    }
    
    bool check(string p, string s)
    {
         bool match;
         int count=0;
         int ssize= s.size();
         char temp[ssize];
         temp[ssize]='\0';
         for (int r=0;r<ssize;r++)
         {
             temp[r]=s[r];
         }
         for (int i=0; i<ssize; i++)
         {
                for (int d=0; d< ssize; d++)
                {
                    if (temp[i]==p[d])
                    {
                       temp[i]='\0';
                       count++;
                    }
                }        
         }
         if (count==ssize)
         {      match = true;
                  return match;
    
         }
         else
                   match=false;
                   return match;
    }
    Step 2 in my asignment is to modify this to use a class called "scramble" that looks like this:

    Code:
    class Scramble
    {
    private:
    string PossibleWord[];
    int size;
    bool Check (string T,string S){} // Is this the word?
    public:
    Scramble (string List[], int Size); // constructor
    string Descramble(string Scrambled); // the game player
    };
    I am also supposed to make a "driver"? Does that just refer to the main()?

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> for (int i=0;i<13;i++)
    That should be 12, not 13.

    >> char temp[ssize];
    That is not legal in standard C++, although it might work on your compiler as an extension. The ssize isn't known until runtime, but C++ requires array sizes to be specified at compile time.

    >> temp[ssize]='\0';
    This is accessing an index out of the bounds of the array. When an array has a size ssize, then the indexes 0 to ssize-1 are valid and the index ssize is invalid. (Note that this is why the 13 should be a 12 above).

    Instead of your temp character array and the for loop that initializes it, you can do the same thing by just using a string variable:
    Code:
    string temp = s;
    Otherwise it looks fine.

    >> I am also supposed to make a "driver"? Does that just refer to the main()?
    Yes.

  12. #12
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    I have fixed more of the first program, and it is passable. I am more concerned with the second part, since I have never even heard of classes before and am having trouble understanding the concept as explained in my book. How am I supposed to convert my program to one that uses the outline as shown above?

  13. #13
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    >> I have fixed more of the first program, and it is passable.
    Maybe, but the errors I pointed out are the kind that work on your machine and then cause a crash when an instructor tries to run it.

    As for using the class, it shouldn't be too hard, but not knowing classes at all will make it more difficult for you.

    Do you have to implement all of those functions? Did your instructor give you that class outline (the declaration for the PossibleWord array worries me)?

    The Check function should be easy since you already have a check function, you just have to modify it to use the class's data instead of data passed in.

    Give it a go and if you get stuck on something feel free to ask specific questions about it here.

  14. #14
    Registered User
    Join Date
    Sep 2007
    Posts
    36
    We don't need to use all the functions, but nonetheless, I'm not even sure where to start.

  15. #15
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Do you understand what a constructor is? Do you see the constructor for that class? Do you understand how to create a new instance of a class?

    For example, string is a class. In your code you create an instance of the string class called scrambled. You use the string class' default constructor. If you were to use a different constructor for string it might look like this:
    Code:
    string example("Hello World");
    So, given the code you currently have, you should be able to create an instance of the Scramble class. You'll have to pass it two things in the constructor, but you have those two things in main so it should not be too hard. Give it a try and see if your code will compile.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Playing around strlen :)
    By audinue in forum C Programming
    Replies: 6
    Last Post: 06-13-2008, 03:22 PM
  2. strlen help
    By stewie1986 in forum C Programming
    Replies: 10
    Last Post: 12-04-2007, 11:15 AM
  3. strlen in expressions
    By justforthis1 in forum C++ Programming
    Replies: 4
    Last Post: 10-24-2006, 10:28 AM
  4. strlen()
    By exoeight in forum C Programming
    Replies: 9
    Last Post: 04-01-2005, 09:18 AM
  5. Just say NO to strlen.
    By anonytmouse in forum A Brief History of Cprogramming.com
    Replies: 24
    Last Post: 02-11-2005, 12:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21