umm comparing string to char

This is a discussion on umm comparing string to char within the C++ Programming forums, part of the General Programming Boards category; K my question is. is it possible to compare string to char like so. string str; char character[16]; if (character==str) ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    57

    umm comparing string to char

    K my question is. is it possible to compare string to char like so.

    string str;
    char character[16];

    if (character==str)
    do so....

    or

    if (strcmp ( str,character ) == 0 )
    do so.....

    ok if not then how can i assign str to character?

    chracter=str
    That gives me error plz help tahnks guys

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    A real example of your question helps.
    Code:
    #include <iostream>
    #include <string>
    #include <cstring>
    
    int main()
    {
       std::string str = "A";
       char character[16] = "A";
       if ( str == character )
       {
          std::cout << "1: match\n";
       }
       if ( character == str )
       {
          std::cout << "2: match\n";
       }
       if ( strcmp(str.c_str(), character) == 0 )
       {
          std::cout << "3: match\n";
       }
       return 0;
    }
    
    /* my output
    1: match
    2: match
    3: match
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    57
    Thanks I got that lol it did not work when i compared 2 chars but this works thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 05:10 PM

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