squeeze function

Discussion in 'C++' started by sara, Oct 19, 2009.

  1. sara

    sara Guest

    Hi,

    I have implemented the following method to remove all the occurences
    of c from string s.

    void squeeze(char* s, char c)
    {
    char string[]="";
    strcpy(string,s);

    int src=0;
    int dst=0;

    while(string[src]!='\0')
    {
    while (string[src]!='c')
    {
    src++;
    if (string[src]!='\0')
    dst++;
    }

    if (string[src]=='\0')
    break;

    while(string[src]=='c')
    src++;
    string[dst]=string[src];
    }

    string[dst+1]=0;

    return;
    }

    How can I change the fucntion to return the string variable? If I make
    the function 'char[] squeeze(char* s, char c)' it does not compile and
    if I make it 'char* squeeze(char* s, char c)' I got some weird error.
    Also, is it possible to write the functon more efficiently?
     
    sara, Oct 19, 2009
    #1
    1. Advertising

  2. sara

    Tim Love Guest

    sara <> writes:

    >I have implemented the following method to remove all the occurences
    >of c from string s.


    > ...


    >Also, is it possible to write the functon more efficiently?

    You can use this approach


    #include <iostream>
    #include <string>
    #include <algorithm>
    using namespace std;

    int main() {
    string s="spaces in text";
    cout << s << endl;
    s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
    cout << s << endl;
    }
     
    Tim Love, Oct 19, 2009
    #2
    1. Advertising

  3. sara

    sara Guest

    On Oct 19, 12:52 am, (Tim Love) wrote:
    > sara <> writes:
    > >I have implemented the following method to remove all the occurences
    > >of c from string s.
    > > ...
    > >Also, is it possible to write the functon more efficiently?

    >
    > You can use this approach
    >
    > #include <iostream>
    > #include <string>
    > #include <algorithm>
    > using namespace std;
    >
    > int main() {
    > string s="spaces in text";
    >    cout << s << endl;
    >    s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;  
    >    cout << s << endl;
    >
    > }
    >
    >


    thanks but I want to implement in C not C++
     
    sara, Oct 19, 2009
    #3
  4. sara

    vj Guest

    On Oct 19, 10:12 am, sara <> wrote:
    > On Oct 19, 12:52 am, (Tim Love) wrote:
    >
    >
    >
    > > sara <> writes:
    > > >I have implemented the following method to remove all the occurences
    > > >of c from string s.
    > > > ...
    > > >Also, is it possible to write the functon more efficiently?

    >
    > > You can use this approach

    >
    > > #include <iostream>
    > > #include <string>
    > > #include <algorithm>
    > > using namespace std;

    >
    > > int main() {
    > > string s="spaces in text";
    > >    cout << s << endl;
    > >    s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;  


    Nice solution.

    > >    cout << s << endl;

    >
    > > }

    >
    > thanks but I want to implement in C not C++


    Haha very good! Take a look at the NG you posted.
     
    vj, Oct 19, 2009
    #4
  5. sara

    SG Guest

    On 19 Okt., 09:27, sara wrote:

    > void squeeze(char* s, char c)
    > {
    >     char string[]="";
    >     strcpy(string,s);


    What do you think is the exact type of string and what is the type's
    interface, its semantics? Hint: If s points to a non-empty character
    string this code will result in a buffer overflow error (undefined
    behaviour).

    > How can I change the fucntion to return the string variable?
    > [...]
    > If I make
    > the function 'char[] squeeze(char* s, char c)' it does not compile and
    > if I make it 'char* squeeze(char* s, char c)' I got some weird error.


    The fact that you ask these questions indicates that you don't know
    how arrays and pointers work in C and C++. Check your favorite
    tutorial and/or book and brush up these C/C++ basics.

    > thanks but I want to implement in C not C++


    Then, you picked the wrong group.

    Cheers,
    SG
     
    SG, Oct 19, 2009
    #5
  6. sara

    Rolf Magnus Guest

    sara wrote:

    > On Oct 19, 12:52 am, (Tim Love) wrote:
    >> sara <> writes:
    >> >I have implemented the following method to remove all the occurences
    >> >of c from string s.
    >> > ...
    >> >Also, is it possible to write the functon more efficiently?

    >>
    >> You can use this approach
    >>
    >> #include <iostream>
    >> #include <string>
    >> #include <algorithm>
    >> using namespace std;
    >>
    >> int main() {
    >> string s="spaces in text";
    >> cout << s << endl;
    >> s.erase(remove(s.begin(), s.end(), ' ' ), s.end() ) ;
    >> cout << s << endl;
    >>
    >> }
    >>
    >>

    >
    > thanks but I want to implement in C not C++


    Then you should really consider asking in a C newsgroup, not a C++ one.
     
    Rolf Magnus, Oct 19, 2009
    #6
    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. Big Bill
    Replies:
    7
    Views:
    420
    Big Bill
    May 7, 2004
  2. Mathias Waack

    squeeze and package imports

    Mathias Waack, Sep 25, 2003, in forum: Python
    Replies:
    2
    Views:
    272
    Mathias Waack
    Sep 27, 2003
  3. arnuld

    K&R2 section 2.8 (exercise 2.4) "squeeze"

    arnuld, Mar 13, 2007, in forum: C Programming
    Replies:
    5
    Views:
    457
    Barry Schwarz
    Mar 14, 2007
  4. luser-ex-troll

    META: relax topicality to squeeze the spam?

    luser-ex-troll, Mar 14, 2009, in forum: C Programming
    Replies:
    6
    Views:
    874
    luser-ex-troll
    Mar 17, 2009
  5. sara

    squeeze function

    sara, Oct 19, 2009, in forum: C Programming
    Replies:
    35
    Views:
    1,937
    Tim Rentsch
    Oct 26, 2009
Loading...

Share This Page