Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. strcmp for Last five characters

strcmp for Last five characters

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
10 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Anu_Bala
    wrote on last edited by
    #1

    Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code

    mi.cbSize = sizeof(mi);
    GetMonitorInfo(hMonitor, &mi);

    mix.cbSize = sizeof(mix);
    GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
    CString display;
    display.Format("%s" ,(LPSTR)mix.szDevice);

    if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
    {
    DualRect = mi.rcMonitor;
    display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
    AfxMessageBox(display);
    }

    Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.

    Anu

    S L C A 5 Replies Last reply
    0
    • A Anu_Bala

      Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code

      mi.cbSize = sizeof(mi);
      GetMonitorInfo(hMonitor, &mi);

      mix.cbSize = sizeof(mix);
      GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
      CString display;
      display.Format("%s" ,(LPSTR)mix.szDevice);

      if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
      {
      DualRect = mi.rcMonitor;
      display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
      AfxMessageBox(display);
      }

      Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.

      Anu

      S Offline
      S Offline
      Sarath C
      wrote on last edited by
      #2

      CString::ReverseFind[^] or if you're too much into C++, string::rfind or wstring::rfind can be used. I suggest you to use the CString version. You can find lot of string parsing functions in CString class. To find whether a monitor is a primary or secondary, you can use dwFlags parameter in the MONITORINFO structure [^]

      -Sarath.

      My blog - Sharing My Thoughts

      Rate the answers and close your posts if it's answered

      1 Reply Last reply
      0
      • A Anu_Bala

        Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code

        mi.cbSize = sizeof(mi);
        GetMonitorInfo(hMonitor, &mi);

        mix.cbSize = sizeof(mix);
        GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
        CString display;
        display.Format("%s" ,(LPSTR)mix.szDevice);

        if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
        {
        DualRect = mi.rcMonitor;
        display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
        AfxMessageBox(display);
        }

        Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.

        Anu

        C Offline
        C Offline
        chandu004
        wrote on last edited by
        #3

        since display is a CString, the = overload may be used. just try out this. if(display=="\\.\DISPLAY1") { ....... } and make sure, since you have \ as a character in a string, perhaps, you will have to use one more \ preceeding it.

        -------------------------------------------- Suggestion to the members: Please prefix your main thread subject with [SOLVED] if it is solved. thanks. chandu.

        1 Reply Last reply
        0
        • A Anu_Bala

          Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code

          mi.cbSize = sizeof(mi);
          GetMonitorInfo(hMonitor, &mi);

          mix.cbSize = sizeof(mix);
          GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
          CString display;
          display.Format("%s" ,(LPSTR)mix.szDevice);

          if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
          {
          DualRect = mi.rcMonitor;
          display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
          AfxMessageBox(display);
          }

          Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.

          Anu

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          Anu_Bala wrote:

          if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE

          Well this will obviously not compile so I assume your copy and paste does not work. I am presuming that it should read:

          if(strcmp(display, "\\\\.\\DISPLAY1")==0)    //PROBLEM IS HERE
          

          but since the '\' character in a string is used to escape the next character this is still wrong, the resultant string passed to strcmp() will be "\.dISPLAY1" It should read:

          if(strcmp(display, "\\\\\\\\.\\\\DISPLAY1")==0)
          

          It's time for a new signature.

          1 Reply Last reply
          0
          • A Anu_Bala

            Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code

            mi.cbSize = sizeof(mi);
            GetMonitorInfo(hMonitor, &mi);

            mix.cbSize = sizeof(mix);
            GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
            CString display;
            display.Format("%s" ,(LPSTR)mix.szDevice);

            if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
            {
            DualRect = mi.rcMonitor;
            display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
            AfxMessageBox(display);
            }

            Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.

            Anu

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            Hi Anu, Richard's hit the reason on the head as to why your code doesn't work. If you have this sort of problem again it always pays to see that the string you're doing the comparison with is correct - something as simple as:

            ::AfxMessageBox( "\\.\DISPLAY1" );

            would have shown you something was wrong with the string you're comparing against. Cheers, Ash PS: Three other quick point: - You probably don't need to use CString::Format to copy the string from the structure, a constructor would have done the job. - While I don't use CString that often doesn't it have a comparison operator you can use instead of strcmp? - Instead of using CString why not use the string type that comes with the language? You only need CString when you're interfacing with MFC, the rest of the time std::string is a bit more flexible and with fewer conversion operators is less likely to blow up on you.

            1 Reply Last reply
            0
            • A Anu_Bala

              Hi, I want to compare last five character.Is there any direct syntax for that. Im geting secondary monitor size, to get that im using the follwing code

              mi.cbSize = sizeof(mi);
              GetMonitorInfo(hMonitor, &mi);

              mix.cbSize = sizeof(mix);
              GetMonitorInfo(hMonitor, (MONITORINFO*)&mix);
              CString display;
              display.Format("%s" ,(LPSTR)mix.szDevice);

              if(strcmp(display,\\.\DISPLAY1)==0) //PROBLEM IS HERE
              {
              DualRect = mi.rcMonitor;
              display.Format(" Res[%d,%d,%d,%d]",DualRect.left,DualRect.right,DualRect.top,DualRect.bottom);
              AfxMessageBox(display);
              }

              Here display get string as "\\.\DISPLAY1" but in stringcomparison,it doesnot get passed. Whats my mistake? Like this i will strcmp with DISPLAY2 string and then i will get that second monitor screen size.

              Anu

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              If you wish to compare only specific string parts in C/C++ you could just use strncmp. :)

              Life is a stage and we are all actors!

              D 1 Reply Last reply
              0
              • L Lost User

                If you wish to compare only specific string parts in C/C++ you could just use strncmp. :)

                Life is a stage and we are all actors!

                D Offline
                D Offline
                David Crow
                wrote on last edited by
                #7

                Not quite. The strncmp() function lexicographically compares, at most, the first N characters in both strings. That is vastly different from which characters (e.g., middle 5, last 5) to compare.

                "One man's wage rise is another man's price increase." - Harold Wilson

                "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                "Man who follows car will be exhausted." - Confucius

                L 1 Reply Last reply
                0
                • D David Crow

                  Not quite. The strncmp() function lexicographically compares, at most, the first N characters in both strings. That is vastly different from which characters (e.g., middle 5, last 5) to compare.

                  "One man's wage rise is another man's price increase." - Harold Wilson

                  "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                  "Man who follows car will be exhausted." - Confucius

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #8

                  Yes you are right but of course, but actually with a bit of simple logic you could compare any substrings using the same function, even the last n symbols for example:

                  char* a="Demo string";
                  char *b="Another string";

                  if(strncmp((char*)(a+strlen(a)-6),(char*)(b+strlen(b)-6),6)==0)
                  {
                  printf("Last 6 symbols are equal.\n");
                  }
                  else
                  {
                  printf("Last 6 symbols aren't equal\n");
                  }

                  It's not the best or even recommended practise, but It's possible.

                  Life is a stage and we are all actors!

                  D A 2 Replies Last reply
                  0
                  • L Lost User

                    Yes you are right but of course, but actually with a bit of simple logic you could compare any substrings using the same function, even the last n symbols for example:

                    char* a="Demo string";
                    char *b="Another string";

                    if(strncmp((char*)(a+strlen(a)-6),(char*)(b+strlen(b)-6),6)==0)
                    {
                    printf("Last 6 symbols are equal.\n");
                    }
                    else
                    {
                    printf("Last 6 symbols aren't equal\n");
                    }

                    It's not the best or even recommended practise, but It's possible.

                    Life is a stage and we are all actors!

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Of course you can, since they are just addresses.

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "Man who follows car will be exhausted." - Confucius

                    1 Reply Last reply
                    0
                    • L Lost User

                      Yes you are right but of course, but actually with a bit of simple logic you could compare any substrings using the same function, even the last n symbols for example:

                      char* a="Demo string";
                      char *b="Another string";

                      if(strncmp((char*)(a+strlen(a)-6),(char*)(b+strlen(b)-6),6)==0)
                      {
                      printf("Last 6 symbols are equal.\n");
                      }
                      else
                      {
                      printf("Last 6 symbols aren't equal\n");
                      }

                      It's not the best or even recommended practise, but It's possible.

                      Life is a stage and we are all actors!

                      A Offline
                      A Offline
                      asadullah ansari
                      wrote on last edited by
                      #10

                      simple and excellent logic...

                      Truth Can'nt be changed

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups