473,396 Members | 2,147 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,396 software developers and data experts.

changing a string

Hi All,

I have a string represented by char* (I do not user string class):
char *s="abcd";

Assume I want to remove the last two elements of s. What is the
standard solution for this?
Can I just change the value of s[2] to '\0'?

Thanks a lot
Sara
Oct 12 '08 #1
9 1484
sa*******@gmail.com wrote:
Hi All,

I have a string represented by char* (I do not user string class):
char *s="abcd";
"abcd" is a string literal, it is a constant so you can't change it.
You can try, but the result is undefined.

If you want to manipulate strings, use std::string.

--
Ian Collins
Oct 12 '08 #2
sa*******@gmail.com wrote:
Hi All,

I have a string represented by char* (I do not user string class):
char *s="abcd";

Assume I want to remove the last two elements of s. What is the
standard solution for this?
Using std::string.
Can I just change the value of s[2] to '\0'?
No.

The reason is a little involved. For starters, if you had

char const * s = "abcd"; // (*)

it would be obvious that you are trying to modify something declared const.
This is undefined behavior.

Second, you have

char * s = "abcd"

and that happens to be roughly equivalent to (*). The right hand side is
const and the conversion to something non-const is only allowed for
compatibility with C. The fact remains that you are trying to modify
the "abcd" string, which is const. That is still undefined behavior, see
[7.1.5.1/4] and [2.13.4/1] or just [2.13.4/2].
Best

Kai-Uwe Bux
Oct 12 '08 #3
On Oct 12, 2:41*pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
saraja...@gmail.com wrote:
Hi All,
I have a string represented by char* (I do not user string class):
char *s="abcd";
Assume I want to remove the last two elements of s. What is the
standard solution for this?

Using std::string.
Can I just change the value of s[2] to '\0'?

No.

The reason is a little involved. For starters, if you had

* char const * s = "abcd"; * * *// (*)

it would be obvious that you are trying to modify something declared const.
This is undefined behavior.

Second, you have

* char * s = "abcd"

and that happens to be roughly equivalent to (*). The right hand side is
const and the conversion to something non-const is only allowed for
compatibility with C. The fact remains that you are trying to modify
the "abcd" string, which is const. That is still undefined behavior, see
[7.1.5.1/4] and [2.13.4/1] or just [2.13.4/2].

Best

Kai-Uwe Bux
What if I use:
char s[4];
s[0]='a';
s[1]='b';
s[2]='c';
s[3]='d';
instead?
Is there any built-in method to erase a part of string defined as
char[] instead of std::string?
Oct 12 '08 #4
sa*******@gmail.com wrote:
On Oct 12, 2:41*pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>saraja...@gmail.com wrote:
Hi All,
I have a string represented by char* (I do not user string class):
char *s="abcd";
Assume I want to remove the last two elements of s. What is the
standard solution for this?

Using std::string.
Can I just change the value of s[2] to '\0'?

No.

The reason is a little involved. For starters, if you had

char const * s = "abcd"; * * *// (*)

it would be obvious that you are trying to modify something declared
const. This is undefined behavior.

Second, you have

char * s = "abcd"

and that happens to be roughly equivalent to (*). The right hand side is
const and the conversion to something non-const is only allowed for
compatibility with C. The fact remains that you are trying to modify
the "abcd" string, which is const. That is still undefined behavior, see
[7.1.5.1/4] and [2.13.4/1] or just [2.13.4/2].

Best

Kai-Uwe Bux

What if I use:
char s[4];
s[0]='a';
s[1]='b';
s[2]='c';
s[3]='d';
instead?
Then you are not 0-terminated. Consequently, many function calls to standard
C-string functions will have undefined behavior.

Is there any built-in method to erase a part of string defined as
char[] instead of std::string?
Not that I would be aware of. But now you could do s[2] = \0. That, of
course, would not erase a block in the middle.
What is your reason not to use std::string?
Best

Kai-Uwe Bux
Oct 12 '08 #5
Kai-Uwe Bux wrote:
sa*******@gmail.com wrote:
[..]
>Is there any built-in method to erase a part of string defined as
char[] instead of std::string?

Not that I would be aware of. But now you could do s[2] = \0. That, of
course, would not erase a block in the middle.
Nit-pick: now you could do

s[2] = 0

or

s[2] = '\0'

(You could of course put a backslash between the space and the zero, but
there has to be a line break immediately following the backslash <g>)

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 13 '08 #6
On Sun, 12 Oct 2008 14:22:01 -0700 (PDT), sa*******@gmail.com wrote:
>Hi All,

I have a string represented by char* (I do not user string class):
char *s="abcd";

Assume I want to remove the last two elements of s. What is the
standard solution for this?
This kind of string manipulation is more C than C++, but there is
still a library for working with these kinds of strings.

#include <cstring>

It's a bad idea to manipulate a string assigned from a literal,
though. That is...

char *s="abcd";
s[2] = 0; // Don't do this!

Instead, use...

char* s = new char [BUFFERSIZE];
std::strncpy (s, "abcd", BUFFERSIZE);
s [2] = 0;

In the above, I cut the string short by writing a new null terminator
to the appropriate character position.

However, this kind of stuff is very error prone, and tends to be a
cause of crashes, memory corruption, security issues and more. It's
very easy to end up reading/writing past the end of your buffers and
so on. Seriously, just use the std::string class - you'll regret it if
you don't.

Oct 13 '08 #7
On 2008-10-13 01:16, sa*******@gmail.com wrote:
On Oct 12, 2:41 pm, Kai-Uwe Bux <jkherci...@gmx.netwrote:
>saraja...@gmail.com wrote:
Hi All,
I have a string represented by char* (I do not user string class):
char *s="abcd";
Assume I want to remove the last two elements of s. What is the
standard solution for this?

Using std::string.
Can I just change the value of s[2] to '\0'?

No.

The reason is a little involved. For starters, if you had

char const * s = "abcd"; // (*)

it would be obvious that you are trying to modify something declared const.
This is undefined behavior.

Second, you have

char * s = "abcd"

and that happens to be roughly equivalent to (*). The right hand side is
const and the conversion to something non-const is only allowed for
compatibility with C. The fact remains that you are trying to modify
the "abcd" string, which is const. That is still undefined behavior, see
[7.1.5.1/4] and [2.13.4/1] or just [2.13.4/2].

Best

Kai-Uwe Bux

What if I use:
char s[4];
s[0]='a';
s[1]='b';
s[2]='c';
s[3]='d';
instead?
If you must use char-arrays use

char s[] = "abcd";

this will ensure that the string is properly null-terminated.
Is there any built-in method to erase a part of string defined as
char[] instead of std::string?
Look up <cstringin your favourite C++ reference.

--
Erik Wikström
Oct 13 '08 #8
Stephen Horne wrote:
Instead, use...

char* s = new char [BUFFERSIZE];
std::strncpy (s, "abcd", BUFFERSIZE);
s [2] = 0;
Given this is almost *exactly* replicating what std::string would do
(except that it would do it more safely), I really can't understand why
you are giving this as a viable solution. Why not give the solution
using std::string rather than this? The end result will be the same, but
much safer and more didactic.
Oct 13 '08 #9
On Mon, 13 Oct 2008 16:34:33 GMT, Juha Nieminen
<no****@thanks.invalidwrote:
>Stephen Horne wrote:
>Instead, use...

char* s = new char [BUFFERSIZE];
std::strncpy (s, "abcd", BUFFERSIZE);
s [2] = 0;

Given this is almost *exactly* replicating what std::string would do
(except that it would do it more safely), I really can't understand why
you are giving this as a viable solution. Why not give the solution
using std::string rather than this? The end result will be the same, but
much safer and more didactic.
Read the rest of the post.

In short, I *didn't* give it as a viable solution, I made it pretty
clear that it *wasn't* a sensible solution. But that doesn't mean that
I take an "I know this but you are not worthy" attitude.

Take a "do what I say" attitude and people will ignore you and do what
they want anyway. So provide the facts and explain why it's a bad
idea. That way, when they ignore you and do it anyway, they don't get
to blame your attitude when it all goes horribly wrong.

Oct 14 '08 #10

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

Similar topics

10
by: Daniel | last post by:
how to make two references to one string that stay refered to the same string reguardless of the changing value in the string?
1
by: Samridhi Kumar Shukla | last post by:
I am facing technical difficulti in using server connection control because we cannot change the path once fixed .. one way is to edit the code of the form generated code but though it allow to...
6
by: Ioannis Demetriades | last post by:
Hi, I need to change the printer's font to "control" -a printer font, and then send a sequence of characters to the printer. My problem is that I cannot change the printer's font. Can this be...
6
by: Darian | last post by:
I am wondering how (if it is possible of course) I can change a users screen resolution to a certain size when running my application and then change it back when they exit my application. For...
4
by: Tugrul HELVACI | last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ?? I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String;...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.