473,839 Members | 1,494 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to remove a character from a string

Hello,
my problem is quite simple to explain.
I have the following string:

"table+came ra"

and I want to remove the + sign:
"tablecamer a".

How do i do that ?
Thanks for your help.
Nov 14 '05
18 55878
"Arthur J. O'Dwyer" wrote:
On Wed, 7 Apr 2004, Toto wrote:

I have the following string:
"table+came ra"
and I want to remove the + sign:
"tablecamer a".

How do i do that ?
#include <string.h>

char s[] = "table+came ra";
char *p;

while ((p = strchr(s,'+')) != NULL)
strcpy(p, p+1);


from N869:

7.21.2.3 The strcpy function

Synopsis
[#1]
#include <string.h>
char *strcpy(char * restrict s1,
const char * restrict s2);
Description

[#2] The strcpy function copies the string pointed to by s2
(including the terminating null character) into the array
pointed to by s1. If copying takes place between objects
that overlap, the behavior is undefined.
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^
try:
#include <string.h>

char s[] = "table+came ra";
char *p, *pp;

while ((p = strchr(s, '+'))) {
pp = p + 1;
while (*p++ = *pp++) continue;
}

Make sure to read the FAQ on string manipulation, too.


Here we agree :-)

--
A: Because it fouls the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Nov 14 '05 #11
Toto wrote:
Hello,
my problem is quite simple to explain.
I have the following string:

"table+came ra"

and I want to remove the + sign:
"tablecamer a".

How do i do that ?
Thanks for your help.


#include <stdio.h>
int
main(void)
{
char *s="table+camer a+chair";
char *p,*temp,c;
for(p=s,temp=s; c=*temp;temp++)
{
if(c != '+') *p++=c;
}
*p=0;
printf("%s",s);
return 0;
}

Nov 14 '05 #12
In article <news:40******* ********@sun.co m>
Eric Sosman <Er*********@Su n.COM> wrote:
Also, the Little Tin God might be better propitiated
by replacing the `while' with

p = s;
while ((p = strchr(p, '+')) != NULL)

... thus not searching and re-searching and re-re-searching
parts of `s' already known to be free of '+' characters.


Of course, if one is bowing to this Little Tin God, one might want
to consider removing "+" characters from the back end first, so
that in:

this+that++some +other+stuff

you never copy any of the "+" characters. But then it might be
important to move every character at most once, which requires
counting up all the "+" characters up front, and ... :-)

Seriously though, one "backwards loop" version:

size_t len = strlen(s);
size_t i, j, n;

for (i = len; i > 0;) {
for (j = i; i > 0 && s[i - 1] == '+'; i--)
continue;
n = j - i;
if (n) {
/*
* There were some '+' characters (n in a row) that we
* would like to remove. Once they are removed, the
* string will be n bytes shorter. The last such '+'
* character is at s[i], i.e., we wish to keep s[i-1]
* unchanged (if it even exists) but clobber s[i].
*
* s[j] is not a '+' -- either it is a wanted character
* or it is the '\0' byte (j==len). Copying (len-j)+1
* bytes copies the '\0' too; we could copy just len-j
* but then we would have to re-terminate the string
* eventually (see below).
*/
memmove(&s[i], &s[j], len - j + 1);
len -= n;
}
}
/* s[len] = 0; -- do this only if we memmove() len-j without the +1 */

The code above is quite untested. :-)
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #13
Martin Dickopp <ex************ ****@zero-based.org> wrote:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:
#include <string.h>

char s[] = "table+came ra";
char *p;

while ((p = strchr(s,'+')) != NULL)
strcpy(p, p+1);
7.21.2.3#2 (strcpy): "[...] If copying takes place between objects that
overlap, the behavior is undefined."


I don't like this (although I'm sure it is correct). I have always
been in the habit of feeling safe about strcpy, strncpy, *printf,
memcpy, etc. as long as dst < src , and using memmove or some other
technique if dst > src.
I suggest the following: [snip] while ((*dst++ = *src++) != '\0');


Some C-teaching books have used exactly that as the
implementation of strcpy (with some extra code to return the
correct return value, of course). It is unfortunate that it
is OK to write this out, but not OK to call strcpy !
Nov 14 '05 #14
ol*****@inspire .net.nz (Old Wolf) wrote:
Martin Dickopp <ex************ ****@zero-based.org> wrote:
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes: <snip>
> while ((p = strchr(s,'+')) != NULL)
> strcpy(p, p+1);


7.21.2.3#2 (strcpy): "[...] If copying takes place between objects that
overlap, the behavior is undefined."


I don't like this (although I'm sure it is correct). I have always
been in the habit of feeling safe about strcpy, strncpy, *printf,
memcpy, etc. as long as dst < src , and using memmove or some other
technique if dst > src.


Bad habit. Bad, bad habit. ;-)
I suggest the following:

[snip]
while ((*dst++ = *src++) != '\0');


Some C-teaching books have used exactly that as the
implementati on of strcpy (with some extra code to return the
correct return value, of course). It is unfortunate that it
is OK to write this out, but not OK to call strcpy !


The rationale however is clear: the standard imposes no detailed
requirements on the internals of strcpy to allow RealWorld[tm]
implementations to use the underlying platform's most efficient way
to copy the data. To stay on the safe side use memmove, which
is explicitly required to copy data between overlapping objects.

Just to add that Martin's solution avoids multiple data movement in
case the string contains more than one instance of the character to
delete.

That said, the following version _might_ perform _slightly_ better,
if the passed string has a long initial (or consists entirely of a)
sequence not containing the character to delete:

char *igStrsqz( char *str, int ch )
{
char *dst = strchr( str, ch );

if ( dst != NULL )
{
const char *src = dst;
while ( *dst != '\0' )
{
while ( *src == ch )
src++;
*dst++ = *src++;
}
}
return str;
}

Regards
--
Irrwahn Grausewitz (ir*******@free net.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html
Nov 14 '05 #15
"(2B) || !(2B)" <sp**@spam.ne t> wrote in message news:<40******* *******@spam.ne t>...
Toto wrote:
Hello,
my problem is quite simple to explain.
I have the following string:

"table+came ra"

and I want to remove the + sign:
"tablecamer a".

How do i do that ?
Thanks for your help.
#include <stdio.h>
int
main(void)
{
char *s="table+camer a+chair";


ITYM:
char s[]="table+camera+ chair";
char *p,*temp,c;
for(p=s,temp=s; c=*temp;temp++)
{
if(c != '+') *p++=c;
}
*p=0;
printf("%s",s);
return 0;
}


regards,
goose
Nov 14 '05 #16
goose wrote:
ITYM:
char s[]="table+camera+ chair";
regards,
goose

Thanks for correcting me.

Nov 14 '05 #17
Chris Torek wrote:

In article <news:40******* ********@sun.co m>
Eric Sosman <Er*********@Su n.COM> wrote:
Also, the Little Tin God might be better propitiated
by replacing the `while' with

p = s;
while ((p = strchr(p, '+')) != NULL)

... thus not searching and re-searching and re-re-searching
parts of `s' already known to be free of '+' characters.


Of course, if one is bowing to this Little Tin God, one might want
to consider removing "+" characters from the back end first, so
that in:

this+that++some +other+stuff

you never copy any of the "+" characters. But then it might be
important to move every character at most once, which requires
counting up all the "+" characters up front, and ... :-)

.... snip ...

An untested nocount version, returning revised strlen, might be:

size_t str_strip(char *s, char x)
{
char *si, *d;

si = d = s;
while ((*d = *s++))
if (*d != x) d++;
return (d - si);
} /* str_strip */

--
Chuck F (cb********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
Nov 14 '05 #18
may be use sed....

sed 's/+//g' file

where file is the file containing your input strings like table+camera etc
Nov 14 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
36094
by: Ren? M?hle | last post by:
I have a psp script with a procedure just to run an update on one table. The Problem occurs when I try to compile this script with pspload: ORA-20006: "frsys_updatereport.psp": compilation failed with the following errors. ORA-06502: PL/SQL: numeric or value error: character string buffer too small Here the whole script:
1
3476
by: rusttree | last post by:
I'm working on a program that manipulates bmp files. I know the offset location of each piece of relevent data within the bmp file. For example, I know the 18th through 21st byte is an integer value representing the width of the bmp image. So far, I have been able to use fstream's seek, write, and read to pull out chucks of bytes and convert them to usable integers straight out of the binary file. It works well, but over the course of...
2
13219
by: jt | last post by:
Looking for an example how to convert and CString to an ASCII character string. Any examples on how to do this? Thank you, jt
0
2184
by: MLH | last post by:
Is an apostrophe a character of special significance to MySQL in a way that would cause "Bob's dog" to become translated into a 12-character string when typed into a MySQL memo field? If I type Bob's dog into an Access memo field, I get a string that is 9-characters long. When I read "Bob's dog" from a memo field in a MySQL table attacted to MS Access via MyODBC driver, it displays as "Bob's dog" - a twelve character string. the '...
2
9299
by: Roy Rodsson via .NET 247 | last post by:
Hi all! I am using a stored procedure in SQL2000 for retrieving fileinformations from a db. the table as an uniqueidentifier for the file information. The stored procedure has a variable called fileIds that is oftype varchar. I am putting in a comma separated string with ids (such as:'9B176B0C-CA03-49C9-A2E7-063038E7CF20','9B176B0C-CA03-49C9-A2E7-063038E7CF22','9B176B0C-CA03-49C9-A2E7-063038E7CF23') However, when I execute the sp via...
7
2516
by: Justin | last post by:
i need to build the unsigned character string: "PR0N\0Spam\0G1RLS\0Other\0Items\0\0\0" from the signed character string: "PR0N Spam G1RLS Other Items" Tokeninzing the character string is not a problem. I can't solve my concatenation problem. I've researched this topic extensively and I've found nothing to help. Failure resulted when I used memcpy,_mbscat, and various other methods. If anyone knows how to build a unsigned
5
4855
by: Karthik | last post by:
Hello! I am not a wizard in this area! Just need some help out in this. I am trying to convert bstr string to new character string. Here is the snippet of my code. **** Code Start**** GlobalInterfacePtr->GetName(bstr, bstrTNamePtr, &retVal);
25
6557
by: lovecreatesbeauty | last post by:
Hello experts, I write a function named palindrome to determine if a character string is palindromic, and test it with some example strings. Is it suitable to add it to a company/project library as a small tool function according to its quality? I will be very happy to get your suggestion from every aspect on it: interface design, C language knowledge or algorithm efficient. Sincerely,
8
11893
by: Brand Bogard | last post by:
Does the C standard include a library function to convert an 8 bit character string to a 16 bit character string?
3
3090
by: Gmuchan | last post by:
Hi, Help needed please. Basically I am wanting to update a 16 digit character string which is in a table called say accounts. If the account no. field is say 0502562389568745 I want to amend every record by taking away the leading zero of the account no. and replace it with a 9. So the result would be 9502562389568745 and I want to run this so that it will update every account no. in the accounts table.
0
9854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9696
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10903
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10584
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7827
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7015
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5681
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3131
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.