advanced string manipulation

Discussion in 'C++' started by MC felon, Oct 8, 2006.

  1. MC felon

    MC felon Guest

    hello again..
    i want to design a program which takes in a string and replaces all 's'
    characters with 'sh'. how do i do it? thanks a lot!
    MC felon, Oct 8, 2006
    #1
    1. Advertising

  2. MC felon

    Ian Collins Guest

    MC felon wrote:
    > hello again..
    > i want to design a program which takes in a string and replaces all 's'
    > characters with 'sh'. how do i do it? thanks a lot!
    >

    What have you tried?

    --
    Ian Collins.
    Ian Collins, Oct 8, 2006
    #2
    1. Advertising

  3. MC felon

    MC felon Guest

    here's what i have tried:

    cout<< "enter a string\n\n";

    gets(take);

    //calculation

    int len = 0;

    char temp[2];

    len = strlen(take);

    for(int g=0;g<len;g++)
    {
    while(take[g] != ' \0 ')
    {
    if(take[g] == 's')
    {
    //dont know what to do
    }
    }
    }
    cout<< take;
    getch();
    }
    MC felon, Oct 8, 2006
    #3
  4. MC felon

    Ian Collins Guest

    MC felon wrote:

    In future, please a) quote some context in your reply and b) post
    something that compiles on its own.

    > here's what i have tried:
    >
    > cout<< "enter a string\n\n";
    >
    > gets(take);
    >

    gets is vile (the root or most buffer overflows), avoid at all cost.
    Use std::getline, which takes the maximum buffer size as a parameter.

    > //calculation
    >
    > int len = 0;
    >

    Introduce len where it is assigned.

    > char temp[2];
    >
    > len = strlen(take);
    >
    > for(int g=0;g<len;g++)
    > {
    > while(take[g] != ' \0 ')


    You don't want this inner loop, the for is already traversing take. Use
    one or the other.
    > {
    > if(take[g] == 's')
    > {

    You would be best to declare a second array, double the size of take and
    copy the characters to this. Where take[g] == 's', add an 'h'.
    > //dont know what to do
    > }
    > }
    > }
    > cout<< take;


    Should add << std::endl here.

    > getch();


    why?

    --
    Ian Collins.
    Ian Collins, Oct 8, 2006
    #4
  5. MC felon

    peter koch Guest

    MC felon wrote:
    > here's what i have tried:
    >
    > cout<< "enter a string\n\n";
    >
    > gets(take);
    >
    > //calculation
    >
    > int len = 0;
    >
    > char temp[2];
    >
    > len = strlen(take);
    >
    > for(int g=0;g<len;g++)
    > {
    > while(take[g] != ' \0 ')
    > {
    > if(take[g] == 's')
    > {
    > //dont know what to do
    > }
    > }
    > }
    > cout<< take;
    > getch();
    > }


    What I see above (disregarding cout) is C code. So you have basically
    two choices:

    a) switch to the more appropriate std::string and return if you have
    problems using that one.
    b) Remove the line that couts take and ask at comp.lang.c. That group
    will have all the expertise needed to deal with C and can give you a
    much better answer.

    /Peter
    peter koch, Oct 8, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jared in ecs

    Perl string manipulation

    jared in ecs, Oct 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    800
    Roy Johnson
    Oct 22, 2003
  2. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    807
  3. A.M
    Replies:
    2
    Views:
    518
    gene tani
    Jun 16, 2006
  4. Mike Cain
    Replies:
    2
    Views:
    432
    Eliyahu Goldin
    Oct 22, 2006
  5. Michele Simionato
    Replies:
    1
    Views:
    586
    Lacrima
    Mar 27, 2010
Loading...

Share This Page