Thread: String manipulation

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    String manipulation

    Hi,
    I have recently started programming in C and would like so guidance.
    What I would like is I have read a string infor eg.
    ' the brown cow jumped over the fox'
    the string will vary what I would like to do is read the string till I come across a space and change the space to an underscore if at all possible so the string would read
    ' the_brown_cow_jumped_over_the_fox'
    and then back again to the spaces.
    Thank you very much
    fergy

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Welcome to the boards!

    Code:
    while (char at pos does not equal '\0') /* Loop till end of string */
        if (char at pos equals ' ') make char at pos equal '_';
        pos = pos plus one; /* Move to next character */
    loop;
    You'll need to convert the abstract idea into real code.
    If you need further help, consider posting what you have tried.
    Last edited by anonytmouse; 03-30-2004 at 05:02 AM.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    6

    string reply

    while (char at pos does not equal '\0') /* Loop till end of string */
    if (char at pos equals ' ') make char at pos equal '_';
    pos = pos plus one; /* Move to next character */
    loop;


    while (people[t].address2!= '\0') Loop till end of string */
    {
    if( *people[t].address2 == ' ') /*replace a space in the string with an _*/
    *people[t].address2 == '_';
    pos = pos++; /* Move to next character */
    } /*end while loop*/

    where pos is equal to the & *people[t].address2
    not sure if that is rightas the program hangs on the last item before this loop.

    Thank you for all your help
    fergy

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    53
    I'am assuming you have an array of a structure that looks something like this:

    Code:
    struct human {
    char address1[20]; char address2[20]; . . .
    }; int main(void) {
    struct human people[5]; //creates an array of 5 humans int i = 0, t = 0; while (people[t].address2[i]!= '\0') Loop till end of string */ {
    if( *people[t].address2[i] == ' ') /*replace a space in the string with an _*/ *people[t].address2[i] == '_'; i++; /* Move to next character */
    } /*end while loop*/
    }
    This isn't anywhere near a complete code. The thing is, strings are actually arrays of characters, and as such, when doing a comparison character by character, treat it like how you would compare an array element by element.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    You could simplify the syntax a bit by doing:

    Code:
    char * t = people[t].address2;
    - or -

    Code:
    char * t = &people[t].address2[0];
    ...then...

    Code:
     while(*t != 0)
    {
         if(*t == ' ')
       {
         *t = '_';
       }
     
     t++;
    }
    - or -

    Code:
    int a = 0;
    
     while(t[a] != 0)
    {
         if(t[a] == ' ')
       {
         t[a] = '_';
       }
     
     a++;
    }
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. string manipulation
    By SPEKTRUM in forum Linux Programming
    Replies: 3
    Last Post: 01-26-2002, 11:41 AM