Thread: How do I loop through a string of chars?

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    16

    Angry How do I loop through a string of chars?

    I have a private data member say (char *string1).

    I wrote the following code.
    for (char* temp=string1; *temp!='/0'; ++temp)

    When I try to run this code I get an access violation error!
    due to this *temp!='/0' part

    so how am I suppose to go through a cstring??

    Thanks

  2. #2
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    !='\0', or just ;*temp; is acceptable

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    35
    Code:
    temp!='\0'
    Want to learn? Then try to teach...

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708

    Re: How do I loop through a string of chars?

    Originally posted by silverfoxtp
    I have a private data member say (char *string1).

    I wrote the following code.
    for (char* temp=string1; *temp!='/0'; ++temp)

    When I try to run this code I get an access violation error!
    due to this *temp!='/0' part

    so how am I suppose to go through a cstring??

    Thanks

    That code is correct. I would guess that string1 points to invalid memory. Post some more code.

    >> fizisyen: temp != '\0'

    Nope. That just compares the pointer to 0 (NULL), which it shouldn't be if the code is working properly.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >That code is correct. I would guess that string1 points to invalid memory. Post some more code.
    The code is not correct, an escape character is '\c', not '/c'

    >That just compares the pointer to 0 (NULL)
    I'll let you off the hook this time, but '\0' and NULL should not be confused.
    My best code is written with the delete key.

  6. #6
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    try this:
    Code:
    ...
    #include <cstring>
    ...
    for(int i=0; i<strlen(stringName); i++)
    {...}
    ...
    unless you have something against using strlen() in this case...
    Last edited by major_small; 11-24-2003 at 10:47 AM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >unless you have something against using strlen() in this case...
    I do. You realize that this results in a quadratic time loop where not calling strlen as the condition would make it linear? For each iteration of the loop, strlen has to walk along the entire string to find the length. Why allow such inefficiency when you can easily fix the problem like so:
    Code:
    int len = static_cast<int> ( strlen ( stringName ) );
    for ( int i = 0; i < len; i++ ) {
      ...
    }
    My best code is written with the delete key.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    yeah yeah yeah... i was just trying to get it written fast... and since he can't figure out how to loop through a string, i'm guessing he's not too worried about speed for now...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i'm guessing he's not too worried about speed for now...
    Better to set good habits early. Calling strlen unnecessarily in a loop is most definitely a bad habit to get into whether you're concerned with speed or not. There's just no argument for using it.
    My best code is written with the delete key.

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Originally posted by Prelude
    There's just no argument for using it.
    did i ever say there was? jeebus...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  3. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 07:36 PM