Comparing strings

Discussion in 'Computer Science & Culture' started by invert_nexus, Jun 15, 2004.

Thread Status:
Not open for further replies.
  1. invert_nexus Ze do caixao Valued Senior Member

    Messages:
    9,686
    What's the best way to compare two strings (char*) in c++?

    Here's the scenario. I have an array of strings that contain Vorbis comments. They each start with TITLE= or ARTIST= and the like. I need a way to examine the array and locate the particular string I'm looking for and output the rest of the string in a buffer. The book that I have been teaching myself C++ with is limited in this regard, and I'm having trouble finding examples on the net.

    I guess I could use strcmp to test the strnlen of the key string then take the rest of the string from that point on if it matches. Yeah, that might work.

    I've been working in circles trying to figure this out. Please help if you know a better way.
     
  2. Google AdSense Guest Advertisement



    to hide all adverts.
  3. invert_nexus Ze do caixao Valued Senior Member

    Messages:
    9,686
    Alright, I figured out how to do it using strncmp. Testing the first of the comment string up to the strlen of the key. Now, I need to figure out a way to copy the end of the comment string to a buffer. strncpy seems to copy the initial part of the string not the end. What function can I use?
     
  4. Google AdSense Guest Advertisement



    to hide all adverts.
  5. MiTo filosofos Registered Senior Member

    Messages:
    100
    find the definition and implementation of the 'string' class on the net, take a look at the operators, theres '==', adn btw strlen and strcmp are C functions, as I said just fint the string class, study it(read it) and you'll know everything you want to know.
    good luck
     
  6. Google AdSense Guest Advertisement



    to hide all adverts.
  7. invert_nexus Ze do caixao Valued Senior Member

    Messages:
    9,686
    I have been perusing several, but haven't found quite what I needed. I ended up just incrementing the pointer by the keylength longhand. So, another mystery solved.

    Now, i'm wrestling with toupper.

    Please Register or Log in to view the hidden image!

    One thing after another. I have the string "Artist" and I want to make it all upperclass and then strncat an "=" on the end. MSDN is poor on giving good examples I've noticed. Any good sites anyone would recommend that gives a good tutorial of all these functions? I've got a Visual C++ 6 book, but of course it concentrates on MFC and ignores a lot of the basic functions.

    I shouldn't even have posted this thread.

    Please Register or Log in to view the hidden image!

    I figured it out almost right after I posted it. I'd been trying this and that for a few days and not finding the right solution, then I give up and try to get help and immediately find the answer myself. Funny how it works.
     
  8. Kunax Sciforums:Reality not required Registered Senior Member

    Messages:
    2,385
    reads: "MSDN is poor on giving good examples" and "MFC", then runs away arms flaying in the air laughing maniacal.

    hehe your proberly almost bad enougth to join my advanced crap programming contest.
     
  9. invert_nexus Ze do caixao Valued Senior Member

    Messages:
    9,686
    Ouch, thanks Kunax. That makes me feel better.

    Please Register or Log in to view the hidden image!



    May I ask why you say this? Because I can't find good examples in MSDN or because I'm using MFC? I'm afraid that I'm only an extreme novice to programming. Of course, I was programming basic back on the old Apple IIe, (personally, I think we would have been better off learning Apple assembly, which isn't that complicated) but I took a 20 year hiatus and am now catching up. I've piddled around with C++ for a while, but this is the first time that I've actually found something to program and I've learned much in doing it. Reading books doesn't do any good without any practical application. I have a book I've been learning from, Beginning Visual C++, but as I said it concentrates on MFC and gives only a cursory examination of the simpler procedures. I think I'm going to need a different book to bone up on the basics. Any recommendations?

    So, I'll have to bow out of the "advanced" crap programming contest. Is there a "beginner" crap programming division?

    Please Register or Log in to view the hidden image!

     
  10. Kunax Sciforums:Reality not required Registered Senior Member

    Messages:
    2,385
    MSDN is know for being less then willing to give the correct info, or planly wrong info.
    MFC is nice for quick and easy apps, but i llke to make things for my self, and not be a "slave" to MFC standards, in the end i find it highly anoying that everything seem to be infect with MFC.

    The Advanced crap programming contest is just me and my brother making useless programs, with in a given time frame. The one with the best program at the end wins.
    Last contest was a to makle a ICQ client, I ended up with a semi functional taskmanager, but since i actual made something i won.

    notics it's advanced crap, NOT advanced programming.
     
    Last edited: Jun 15, 2004
  11. invert_nexus Ze do caixao Valued Senior Member

    Messages:
    9,686
    Yeah, working on this plugin, I've come to the conclusion that MFC tends to add bloat to the program. I do like the concept of OOP. But I think it would be helpful to understand what MFC is doing behind the scenes.

    And MSDN sucks, but at least it's thorough. And in one place. The biggest problems I've had with things of this nature is rounding up information. Sometimes google searches just refuse to give the golden package that you're looking for. But, I figure if I hang in there I'll figure it out eventually.

    I suppose I want to get a book on the windows api. That's where a lot of the problems I ran into with this dll came from. And also a book on C. Is that where strncat and all that come from or is it C++ as well? And now there's C#. What's the difference between C++ and C#?

    I suppose it helps if you go to school for it and study algorythms and the like. Without that teaching it's like grasping for straws in the dark in a hurricane.
     
  12. Kunax Sciforums:Reality not required Registered Senior Member

    Messages:
    2,385
  13. Kunax Sciforums:Reality not required Registered Senior Member

    Messages:
    2,385
    char *name = "ARTIST=Crap artist";
    char *newname = "";

    MessageBox(NULL, name, name, MB_OK);
    newname = &name[sizeof("ARTIST=")-1];
    MessageBox(NULL, newname, newname, MB_OK);

    2nd msg box says "Crap artist", while the first ofcause says "ARTIST=Crap artist"
     
  14. invert_nexus Ze do caixao Valued Senior Member

    Messages:
    9,686
    Here's what I settled on.

    Code:
      int keylength=strlen(buf);    // buf contains ARTIST=, TITLE=, etc...
      int found=0;    // marker if proper field is found
      int i;              // counter
      for(i=0; i < count;i++)
      {
          if(strncmp(buf, pVC[i], keylength)==0)   // pVC is pointer to vorbis_comment array.
          {                           // vorbis_comment struct, user_comment array actually
              found=1;
              break;
          }
      }
      if(found)
      {
          char* pFound=pVC[i];
          pFound+=keylength;    // increments pointer to point after the key
          SF.set_string_var(command->svalue1, pFound);  // saves info in command struct.
          strncpy(status, "Field found", statuslen);
      }
    
    Don't know if it's the best way, but it seems to work and is relatively simple. Now, I need to get toupper to work. I suppose I could always just test the value of each character and if it's lower case add whatever number I need to it to make it uppercase. I would think there'd be a simple function to do it though. toupper seems to work differently. It says that it will make it uppercase if it's relevant, but it doesn't seem to work like that, it seems to add the value between A and a to everything.

    BTW, posting this code in here pointed out an error that had slipped in, there was an extra pair of brackets and it wasn't working dependably. Now I think it should be fine.


    Edit: Your way removes one step. I wonder if one is better than the other necessarily. I suppose every step helps in the long run.

    Actually, you're dealing with just a string, while I'm dealing with an array of strings. So, it'd have to be differently coded to set it at the assignment stage.
     
  15. Kunax Sciforums:Reality not required Registered Senior Member

    Messages:
    2,385
  16. invert_nexus Ze do caixao Valued Senior Member

    Messages:
    9,686
    Yeah, I think that just might work. Thanks, Kunax.

    Ogg Vorbis is an audio compression scheme like mp3. I'm working on a plugin for girder (remote control software). There's another plugin that gets the filename from winamp then it'll pass it to my plugin, my plugin extracts the user_comment information and sends it back to girder which displays it on screen through an On Screen Display plugin.
     
  17. klmbrt Registered Member

    Messages:
    3
    I am attempting to write a C++ program that has the results of a T/F test. It takes the information from a data file and then it compares the students answers to the correct answers. I have two problems. First the data file has the first 8 characters as the Student ID(Letters and Numbers), then it has a blank space followed by the student answers. Can someone give me some ideas about how I would maybe use strcmp or would I use a method of comparing Arrays?
     
Thread Status:
Not open for further replies.

Share This Page