Thread: Help with strings

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    118

    Help with strings

    Lets say I have many strings, I know all of the are 3 chars long, but if I want to compare ONLY the 3rd char with the 3rd char of another string how would I do it?

    like this:

    Code:
    string string1 = "c 3";
    string string2 = "r 3";
    string string3 = "c 4";
    comparing string1 and string2 should return true, but copmaring string1 and string3 should return false, see what I mean? thanks in advance
    Why drink and drive when you can smoke and fly?

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Code:
    #include <iostream>
    
    #include <string>
    using namespace std;
    
    int main()
    {
    string string1("a 3");
    string string2("a 3");
    string string3("a 4");
    if (string1[2]==string2[2])
      cout<<"string1[2]("<<string1[2]<<") == string2[2] ("<<string2[2]<<")"<<endl;
    if (string2[2]==string3[2])
       cout<<"string2[2]("<<string2[2]<<") == string3[2] ("<<string3[2]<<")"<<endl;
    else
       cout<<"string2[2]("<<string2[2]<<") != string3[2] ("<<string3[2]<<")"<<endl;
         
      
    
    }
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Sorry for not being more specific, but what I want to compare is actualy in an array of strings:
    Code:
    string sArray[5] = { "c 1",  "d 1", "c 2", "r 4",  "h 1" };
    
    for (int i = 0; i < 5; i++)
    {
        for (int j = 0; j < 5; j++)
        {
            if (i != j && sArray[i]/*The third char of this string here*/ == sArray[j]/*The third char of this string here*/)
                // Do something
        }
    }
    Why drink and drive when you can smoke and fly?

  4. #4
    Registered User
    Join Date
    Jan 2003
    Posts
    118
    Never mind I found out how to do it, Thanks for the help!
    Why drink and drive when you can smoke and fly?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM