473,609 Members | 1,930 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 #1
18 55821

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);

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

HTH,
-Arthur
Nov 14 '05 #2
Toto <ma*@nospam.com > spoke thus:
"table+came ra"
and I want to remove the + sign:
"tablecamer a".


Well, I hope this isn't a homework question, because I'll post an
answer:

void rem_char( char *s, char t )
{
if( !s || !*s ) return;

while( *s++ != t ) if( !*s ) return;
while( *(s-1)=*s++ );
}

I don't guarantee that it's conforming, but it produced correct
results for me.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cybers pace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
"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);


This is only guaranteed to work if the '+' is the
last character in the string. Otherwise the source and
destination overlap, which is a no-no for strcpy(). A
safe alternative is

memmove(p, p+1, strlen(p+1)+1);

.... which can be simplified to

memmove(p, p+1, strlen(p));

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.

--
Er*********@sun .com
Nov 14 '05 #4
"Arthur J. O'Dwyer" <aj*@nospam.and rew.cmu.edu> writes:
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);


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

(Your algorithm is also inefficient, because it scans `s' from the
beginning after replacing each '+' character.)

I suggest the following:

char s [] = "table+came ra";
const char *src = s;
char *dst = s;

do
{
while (*src == '+')
++src;
}
while ((*dst++ = *src++) != '\0');

Martin
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #5
Eric Sosman wrote:

"Arthur J. O'Dwyer" wrote:

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


This is only guaranteed to work if the '+' is the
last character in the string. [...]


And now that I think about it, not even then.

--
Er*********@sun .com
Nov 14 '05 #6

"Toto" <ma*@nospam.com > wrote in message
news:c5******** **@news-reader4.wanadoo .fr...
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.


#define MAX_LEN 1000

char input_string[MAX_LEN];
char output_string[MAX_LEN];

strcpy(input_st ring, "table+camera") ;
int len = strlen(input_st ring);
int j =0;
for(int i =0; i<=len; i++)/*we wanna copy the null terminator*/
{
if(input_string[i] != '+')
{
output_string[j] = input_string[i];
j++;
}
}
Nov 14 '05 #7
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>
#include <string.h>

int main(void) {
char string[] = "table+came ra";
char *s;
int i;
puts(string);
s = strchr(string, '+');
i = s - string + 1;
while ((*s++ = string[i++])) ;
puts(string);
return 0;
}

Try this.
--
Joe Wright mailto:jo****** **@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #8
Mac
On Wed, 07 Apr 2004 22:34:59 +0200, 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.


Use strchr() to find the character. Use memmove() to copy over it. If you
want to replace more than one instance of the character, do this
repeatedly until strchr() returns NULL.

If there is any chance that the string is long, be sure to avoid
re-searching the whole string for the character.

HTH

--Mac

Nov 14 '05 #9
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.


/* BEGIN new.c */

#include <stdio.h>

#define STRING "table+came ra"
#define WHITE "+"

char *str_squeeze(ch ar *, const char *);
char *str_tok_r(char *, const char *, char **);
size_t str_spn(const char *, const char *);
size_t str_cspn(const char *, const char *);
char *str_chr(const char *, int);
char *str_cpy(char *, const char *);

int main(void)
{
const char *const original = STRING;
char s1[sizeof STRING];

puts(str_squeez e(str_cpy(s1, original), WHITE));
return 0;
}

char *str_squeeze(ch ar *s1, const char *s2)
{
char *p3;
char const *const p2 = s2;
char *const p1 = s1;

s2 = str_tok_r(p1, p2, &p3);
while (s2 != NULL) {
while (*s2 != '\0') {
*s1++ = *s2++;
}
s2 = str_tok_r(NULL, p2, &p3);
}
*s1 = '\0';
return p1;
}

char *str_tok_r(char *s1, const char *s2, char **p1)
{
if (s1 != NULL) {
*p1 = s1;
}
s1 = *p1 + str_spn(*p1, s2);
if (*s1 == '\0') {
return NULL;
}
*p1 = s1 + str_cspn(s1, s2);
if (**p1 != '\0') {
*(*p1)++ = '\0';
}
return s1;
}

size_t str_spn(const char *s1, const char *s2)
{
size_t n;

for (n = 0; *s1 != '\0' && str_chr(s2, *s1) != NULL; ++s1) {
++n;
}
return n;
}

size_t str_cspn(const char *s1, const char *s2)
{
size_t n;

for (n = 0; str_chr(s2, *s1) == NULL; ++s1) {
++n;
}
return n;
}

char *str_chr(const char *s, int c)
{
while (*s != (char)c) {
if (*s == '\0') {
return NULL;
}
++s;
}
return (char *)s;
}

char *str_cpy(char *s1, const char *s2)
{
char *const p1 = s1;

do {
*s1 = *s2++;
} while (*s1++ != '\0');
return p1;
}

/* END new.c */

--
pete
Nov 14 '05 #10

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

Similar topics

1
36058
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
3466
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
13186
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
2169
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
9285
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
2501
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
4843
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
6528
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
11865
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
3077
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
8141
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
8093
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,...
1
8234
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8408
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6067
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
5525
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();...
1
2540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1686
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1404
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.