Thread: strings and char's

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    8

    strings and char's

    I was wondering if anyone here perhaps know how to traverse the indivdual chars in a string. I am writing a cpp file that finds email in any valid txt file and writes them to another file of the users choosing. To start my i have an if statement

    for(int i = 0; i < meDeque.size(); i++)
    {
    if(myDeque[i] == '@')
    ...........................
    and all the rest

    the problem is when i compare the strings to a char character such as that @ symbol in single quates my compiler does not like it. Does anyone know the proper way to compare and traverse the individual char in the strings? Thank you.

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Wrong way to do it. Use one of the string functions, like this one:

    http://www.cppreference.com/cppstring/find.html

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Just to let you know: there is nothing wrong in comparing a character in a string returned by [] operators with another character. And from the code posted, I couldn't deduce if string::find is or isn't appropriate.

    The problem is elsewhere: may-be that you have a misspelling meDeque-myDeque.

    It is also very unlikely that the compiler would say: "Line 42: [Error] I don't like it". Reading, thinking about and posting (if you can't figure it out) what the compiler says is pretty important.

    Use the code tags for code next time.
    Last edited by anon; 05-21-2007 at 12:43 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Perhaps I shouldn't have said it's wrong per se. You are correct that it can be done, and produce the same results.

    I would argue, though, that the string functions, such as find(), would be better appropriate for telling you where, if at all, a given substring is located inside the current string. These functions are safer to use since you won't run the risk of writing the loop incorrectly. Besides all of that, you don't have to reinvent the wheel if you use the functions provided.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 03-19-2009, 10:56 AM
  2. Question about strings n chars
    By cszym001 in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2008, 05:09 PM
  3. Converting strings to chars
    By Suchy in forum C++ Programming
    Replies: 4
    Last Post: 05-06-2007, 04:17 AM
  4. writing strings chars from ints
    By deleeuw in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2003, 09:09 AM
  5. Strings and Chars
    By Scorpion-ice in forum C++ Programming
    Replies: 5
    Last Post: 09-30-2002, 07:17 AM