Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > squeeze function

Reply
Thread Tools

squeeze function

 
 
sara
Guest
Posts: n/a
 
      10-19-2009
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?
 
Reply With Quote
 
 
 
 
Tim Love
Guest
Posts: n/a
 
      10-19-2009
sara <(E-Mail Removed)> 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;
}
 
Reply With Quote
 
 
 
 
sara
Guest
Posts: n/a
 
      10-19-2009
On Oct 19, 12:52*am, (E-Mail Removed) (Tim Love) wrote:
> sara <(E-Mail Removed)> 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++
 
Reply With Quote
 
vj
Guest
Posts: n/a
 
      10-19-2009
On Oct 19, 10:12*am, sara <(E-Mail Removed)> wrote:
> On Oct 19, 12:52*am, (E-Mail Removed) (Tim Love) wrote:
>
>
>
> > sara <(E-Mail Removed)> 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.
 
Reply With Quote
 
SG
Guest
Posts: n/a
 
      10-19-2009
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
 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-19-2009
sara wrote:

> On Oct 19, 12:52 am, (E-Mail Removed) (Tim Love) wrote:
>> sara <(E-Mail Removed)> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
squeeze function sara C Programming 35 10-26-2009 05:57 AM
Major Short Squeeze Likely / FDA Drug Approval thekingofthestreet2006@yahoo.com Digital Photography 1 06-21-2006 09:04 PM
Feds put squeeze on Internet firms Imhotep Computer Security 0 05-31-2006 05:28 AM
Can I squeeze this down any more please? Big Bill HTML 7 05-07-2004 07:27 PM
squeeze and package imports Mathias Waack Python 2 09-27-2003 08:27 AM



Advertisments