473,397 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to remove chars from a string pointer

Hi

Does any1 know of a method where you can search through a string and remove chars from a specific pointer until the new line. For example:

"This is a test @ A test this is"

how do make it so u search for a pointer (e.g.@) and make it this:

"This is a test"

Thanks for your help in advanced!!!
Jul 28 '09 #1
19 10609
JosAH
11,448 Expert 8TB
Have a look at the strchr() function.

kind regards,

Jos
Jul 28 '09 #2
@JosAH
I know of dat function but, how do i delete the rest of the chars after using it is the problem. that will only point me to it, it wont delete or erase anything.
Jul 28 '09 #3
JosAH
11,448 Expert 8TB
@imLVcrazy
If that function return a non-NULL pointer set its value (i.e. to where it points) to '\0'; that effectively sets the end of the string; no need to erase anything at all.

kind regards,

Jos
Jul 28 '09 #4
weaknessforcats
9,208 Expert Mod 8TB
The easiest way to remove chars from the middle of a string is to use strcpy() and copy the string into itself.

Suppose your string ro remove starts at address A and ends at address B. All you do is:

Expand|Select|Wrap|Line Numbers
  1. strcpy(A, B+1);
strcpy() will terminate using the \0 of the string.

Removing the end of a string is just a special case where B+1 is the \0 of the original string.
Jul 28 '09 #5
donbock
2,426 Expert 2GB
@imLVcrazy
Strictly speaking, you don't remove chars from a pointer, you remove them from a string pointed to by the pointer.

Not all strings can be modified, see examples below. The string pointed to by p1 is a string literal. Whether or not such a string can be modified is implementation-defined; and you should never rely on implementation-defined behavior. The const keyword in the declaration of pointer p2 prevents you from using p2 to modify the string it points at.
Expand|Select|Wrap|Line Numbers
  1. char *p1 = "foo";
  2.  
  3. char s[80] = "bar";
  4. const char * p2 = s;
If the string cannot be modified then you need to make a second copy that you can modify.
Jul 28 '09 #6
Hi
Please let me know, you want to remove the '@' character at every line , make it each line like below.

this is test1@ this is test1
this is test2 @this is test2
this is test3 @this is test3

After removing the '@' character
this is test1
this is test2
this is test3
Jul 29 '09 #7
JosAH
11,448 Expert 8TB
@weaknessforcats
Better don't do that; this is what the Standard has to say about overlapping areas:

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.
memmove() is the only function that can handle overlapping areas.

kind regards,

Jos
Jul 29 '09 #8
donbock
2,426 Expert 2GB
@sridhard2406
So you want each string to end at the point where there is currently an '@' character. Suppose a newline appears somewhere after the '@" in the input string ... do you want the newline to appear in the output string? Suppose there isn't a newline at the end of the input string ... do you want to add a newline to the output string anyway?

Look back at response #4. It provides the simplest solution for you; with the following provisos:
  • You need to do a little more if newlines are a special case;
  • You need to operate on a copy of the input string if the input string cannot be modified.
Jul 29 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
Better don't do that; this is what the Standard has to say about overlapping areas:
Yes, but this is not an overlap situation. The original string memory allocation does not change. Only the characters are moved to the left. Works fine. Done it for years.

Doing anything other than moving characters to the left is the overlap case and that produces undefined results.
Jul 29 '09 #10
JosAH
11,448 Expert 8TB
@weaknessforcats
I think it is an overlapping problem: suppose you want to remove the character 'e' from the string "abcdefghij". You use a pointer to the character 'e' and a pointer to the character 'f' and do a strcpy(pe, pf); now suppose the strcpy() function starts copying from the end towards the beginning. You end up with the wrong string. (most (if not all) strcpy() implementations copy from the start to the ending '\0' character so there is no problem there).

kind regards,

Jos
Jul 29 '09 #11
Banfa
9,065 Expert Mod 8TB
I have to agree with Jos on this. You may have done this overlapped copy for years but in all that time you have actually been relying on the specific implementation rather than on the standard defined behaviour.

It just happens that strcpy is a simple function and on the whole is implemented in the same way everywhere but that is not a requirement of the standard.
Jul 29 '09 #12
donbock
2,426 Expert 2GB
@sridhard2406
Is it necessary to support a method to escape the '@' character so there is a way for it to get into the output string? For instance,
Expand|Select|Wrap|Line Numbers
  1. Inputs:
  2. This is a string: "foobar @ more stuff" @ text to be ignored
  3. Special character \@ is important @ more text to be ignored
  4.  
  5. Outputs:
  6. This is a string: "foobar @ more stuff" 
  7. Special character \@ is important 
If so then your job is more complicated because you need to detect escaped '@' so that you can ignore it.
Jul 29 '09 #13
JosAH
11,448 Expert 8TB
@donbock
How about Occam's razor? I bet the OP hadn't even thought about your additional requirements yet ;-)

kind regards,

Jos
Jul 29 '09 #14
donbock
2,426 Expert 2GB
@JosAH
I went back and edited my post to emphasize that I'm supplying questions to help the OP clarify the requirements; not recommending what the answers to those questions should be. I want the OP to see the importance of thinking about the corner cases.
Jul 29 '09 #15
JosAH
11,448 Expert 8TB
@donbock
Which is AGT (A Good Thing (tm)); I'm just a lazy bastard: I give them what they asked for, no more no less ;-)

kind regards,

Jos
Jul 29 '09 #16
@sridhard2406
That is exactly what I'm looking for. To remove anything after ther @ symbol until a new line appears and then repeats the same thing on the new line.
Jul 30 '09 #17
Hi,
If your data is in the file, read the all the information into buffer.and check how many bytes you have in your file (say For Example:Your file has the 1024 bytes).
Try to traverse the buffer until u reach the '@' character, if reached change the character as ' '(space) until u reach '\n' then traverse again until '@' ,
do same operation, until you reached all the bytes(1023).

let me know you got the logic.
Jul 30 '09 #18
weaknessforcats
9,208 Expert Mod 8TB
now suppose the strcpy() function starts copying from the end towards
This contradicts K&R The ANSI C Programming Language Section 5.5 page 105.

I realize K&R is not the standard but by the same token, no C string library will implement a copy where you have to march the source string to the right just to copy to the left. That would be an implied strlen inside strcpy and that's not going to happen.

Also, remember that your pe and pf are addresses of char arrays and in C there is no way to tell the length of the target array (pe) so there is no way to copy into it from the right to the left.

I say again, this is not an overlap.
Jul 30 '09 #19
JosAH
11,448 Expert 8TB
@weaknessforcats
Ok have it your way then but your way contradicts the Standard. btw there is no need to tell the length of the target, i.e. if it is too short undefined behaviour occurs (a sigsegv will be raised usually).

kind regards,

Jos
Jul 30 '09 #20

Sign in to post your reply or Sign up for a free account.

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
4
by: grocery_stocker | last post by:
#include <iostream> //for cin and cout #include <iomanip> // for setw() #include <string> // for strlen() strcmp() strrev() #include <fstream> //ifstream and ofstream: file (input & output)...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
9
by: Durgesh Sharma | last post by:
Hi All, Pleas help me .I am a starter as far as C Language is concerned . How can i Right Trim all the white spaces of a very long (2000 chars) Charecter string ( from the Right Side ) ? or how...
3
by: andrewkl | last post by:
hi, I have the following Perl code that inserts a string to an Oracle DB via a stored procedure: #!/usr/local/bin/perl ## Perl v5.8.6 built for sun4-solaris use strict; BEGIN...
10
by: Mike Copeland | last post by:
I have data I need to normalize - it's "name" data. For example, I have the following: "Watts, J.C." I wish to (1) parse the "first name" ("J.C.") and adjust it to "JC". Essentially, I want to...
26
by: Brad | last post by:
I'm writing a function to remove certain characters from strings. For example, I often get strings with commas... they look like this: "12,384" I'd like to take that string, remove the comma...
3
by: .rhavin grobert | last post by:
this is m$ vc++, but the question itself is c++ specific... TCHAR is a 'char' in non-unicode. functions return... TCHAR* CString::GetBuffer(int); int CString::GetLenght(int);...
10
by: Olaf Dietrich | last post by:
I may just be temporarily (hopefully ...) stupid, but how can I pass a function pointer between functions using an array of (signed/unsigned) chars (in a standard-conforming way)? E.g.: I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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...

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.