shorten string length by 1

J

John Smith

I want to shorten a string by replacing the last character by '\0'.

The following code displays the string. It works fine. It's in a
loop and different strings are displayed without problems.
-----------------------------------------------------------
temp= strlen(aGroup);
for (i=0; i< temp;i++)
printf(" %d %d %c\n", i+1,temp,aGroup);
---------------------------------------------------------

To replace the last character by '\0', I have tried

aGroup[i-1]='\0';

or
temp--;
aGroup[temp]='\0';

or
aGroup[temp-1]='\0';

Though there is no compiling error, the program crashed when ran
(I was asked whether to send error report to Microsoft. Same error
for all three.)

But "aGroup[22]='\0';" works (I knew the length is greater than 22.)

What gives? This is on VC++ 6.0.
 
J

Jason

John said:
I want to shorten a string by replacing the last character by '\0'.

The following code displays the string. It works fine. It's in a
loop and different strings are displayed without problems.
-----------------------------------------------------------
temp= strlen(aGroup);
for (i=0; i< temp;i++)
printf(" %d %d %c\n", i+1,temp,aGroup);
---------------------------------------------------------

To replace the last character by '\0', I have tried

aGroup[i-1]='\0';

or
temp--;
aGroup[temp]='\0';

or
aGroup[temp-1]='\0';

Though there is no compiling error, the program crashed when ran
(I was asked whether to send error report to Microsoft. Same error
for all three.)

But "aGroup[22]='\0';" works (I knew the length is greater than 22.)

What gives? This is on VC++ 6.0.


Verify that aGroup has been properly declared, and that it is
suffiently large enough to store the string you are copying into it.
Perhaps you are overflowing a buffer, or forgot to properly call
malloc?

-Jason
-Jason
 
J

John Smith

Jason said:
John said:
I want to shorten a string by replacing the last character by '\0'.

The following code displays the string. It works fine. It's in a
loop and different strings are displayed without problems.
-----------------------------------------------------------
temp= strlen(aGroup);
for (i=0; i< temp;i++)
printf(" %d %d %c\n", i+1,temp,aGroup);
---------------------------------------------------------

To replace the last character by '\0', I have tried

aGroup[i-1]='\0';

or
temp--;
aGroup[temp]='\0';

or
aGroup[temp-1]='\0';

Though there is no compiling error, the program crashed when ran
(I was asked whether to send error report to Microsoft. Same error
for all three.)

But "aGroup[22]='\0';" works (I knew the length is greater than 22.)

What gives? This is on VC++ 6.0.



Verify that aGroup has been properly declared, and that it is
suffiently large enough to store the string you are copying into it.
Perhaps you are overflowing a buffer, or forgot to properly call
malloc?

-Jason
-Jason


Thanks for the reply.

As I said. The program worked fine until I added the code (any one
of the three) to replace the last character. I think this should
rule out the potential problems you mentioned.

BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.
 
J

Jason

John said:
BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.

Could you post the actual code? Without further information I'm afraid
I won't be able to assist you.

-Jason
 
B

Barry Schwarz

I want to shorten a string by replacing the last character by '\0'.

snip incomplete code
Though there is no compiling error, the program crashed when ran
(I was asked whether to send error report to Microsoft. Same error
for all three.)

But "aGroup[22]='\0';" works (I knew the length is greater than 22.)

What gives? This is on VC++ 6.0.

Show the definition and initialization of aGroup please.


<<Remove the del for email>>
 
K

Keith Thompson

John Smith said:
As I said. The program worked fine until I added the code (any one
of the three) to replace the last character. I think this should
rule out the potential problems you mentioned.

No, "The program worked fine" doesn't rule out anything. Your recent
changes could simply have changed it from undefined behavior that
happens to be harmless to undefined behavior that blows things up.
BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.

If aGroup contains a valid string, the following should shorten it by
one character:

aGroup[strlen(aGroup)-1] = '\0';
 
W

websnarf

Keith said:
John Smith said:
BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.

You mean its 10 times larger than necessary most of the time, and
insufficient for some marginal cases. So its neither sufficient or
efficient.
If aGroup contains a valid string, the following should shorten it by
one character:

aGroup[strlen(aGroup)-1] = '\0';

Excuse me? No. Try the following:

aGroup[strlen(aGroup) - (aGroup[0] != '\0')] = '\0';

Using Bstrlib, this is somewhat more understandable:

bdelete(b_aGroup, blength(b_aGroup) - 1, 1);
 
J

Joe Wright

John said:
Jason said:
John Smith wrote:

I want to shorten a string by replacing the last character by '\0'.

The following code displays the string. It works fine. It's in a
loop and different strings are displayed without problems.
-----------------------------------------------------------
temp= strlen(aGroup);
for (i=0; i< temp;i++)
printf(" %d %d %c\n", i+1,temp,aGroup);
---------------------------------------------------------

To replace the last character by '\0', I have tried

aGroup[i-1]='\0';

or
temp--;
aGroup[temp]='\0';

or
aGroup[temp-1]='\0';

Though there is no compiling error, the program crashed when ran
(I was asked whether to send error report to Microsoft. Same error
for all three.)

But "aGroup[22]='\0';" works (I knew the length is greater than 22.)

What gives? This is on VC++ 6.0.



Verify that aGroup has been properly declared, and that it is
suffiently large enough to store the string you are copying into it.
Perhaps you are overflowing a buffer, or forgot to properly call
malloc?

-Jason
-Jason



Thanks for the reply.

As I said. The program worked fine until I added the code (any one
of the three) to replace the last character. I think this should
rule out the potential problems you mentioned.

BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.



#include <stdio.h>
#include <string.h>

int main(void)
{
char aGroup[2048];
int i;
strcpy(aGroup, "Hello Sailor");
i = strlen(aGroup);
while (i--) {
puts(aGroup);
aGroup = '\0';
}
return 0;
}

Why is this thread so long?
 
M

Mark McIntyre

If aGroup contains a valid string, the following should shorten it by
one character:

aGroup[strlen(aGroup)-1] = '\0';

Excuse me? No. Try the following:

aGroup[strlen(aGroup) - (aGroup[0] != '\0')] = '\0';

Re-read what keith said.
Using Bstrlib, this is somewhat more understandable:

bdelete(b_aGroup, blength(b_aGroup) - 1, 1);

however Bstrlib is not part of Standard C.
 
K

Keith Thompson

Mark McIntyre said:
If aGroup contains a valid string, the following should shorten it by
one character:

aGroup[strlen(aGroup)-1] = '\0';

Excuse me? No. Try the following:

aGroup[strlen(aGroup) - (aGroup[0] != '\0')] = '\0';

Re-read what keith said.

Actually, websnarf caught something that I missed. If aGroup contains
a valid string of length 0, my code will write before the beginning of
the array, invoking undefined behavior. (Of course shortening an
empty string by one character doesn't make any sense, but that's no
excuse for UB.)

But I don't see the need to put everything into the index expression.
I'd do it this way:

if (aGroup[0] != '\0') {
aGroup[strlen(aGroup)-1] = '\0';
}
 
M

Mac

John said:
Jason said:
John Smith wrote:


I want to shorten a string by replacing the last character by '\0'.

The following code displays the string. It works fine. It's in a
loop and different strings are displayed without problems.
-----------------------------------------------------------
temp= strlen(aGroup);
for (i=0; i< temp;i++)
printf(" %d %d %c\n", i+1,temp,aGroup);
---------------------------------------------------------

To replace the last character by '\0', I have tried

aGroup[i-1]='\0';

or
temp--;
aGroup[temp]='\0';

or
aGroup[temp-1]='\0';

Though there is no compiling error, the program crashed when ran
(I was asked whether to send error report to Microsoft. Same error
for all three.)

But "aGroup[22]='\0';" works (I knew the length is greater than 22.)

What gives? This is on VC++ 6.0.


Verify that aGroup has been properly declared, and that it is
suffiently large enough to store the string you are copying into it.
Perhaps you are overflowing a buffer, or forgot to properly call
malloc?

-Jason
-Jason



Thanks for the reply.

As I said. The program worked fine until I added the code (any one
of the three) to replace the last character. I think this should
rule out the potential problems you mentioned.

BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.



#include <stdio.h>
#include <string.h>

int main(void)
{
char aGroup[2048];
int i;
strcpy(aGroup, "Hello Sailor");
i = strlen(aGroup);
while (i--) {
puts(aGroup);
aGroup = '\0';
}
return 0;
}

Why is this thread so long?


Is this like a Zen Koan or something? Does the behavior of this program
somehow shed light on what is wrong with the OP's code fragment? If so, I
don't get it. :-(

--Mac
 
M

Mark McIntyre

Mark McIntyre said:
If aGroup contains a valid string, the following should shorten it by
one character:

aGroup[strlen(aGroup)-1] = '\0';

Excuse me? No. Try the following:

aGroup[strlen(aGroup) - (aGroup[0] != '\0')] = '\0';

Re-read what keith said.

Actually, websnarf caught something that I missed. If aGroup contains
a valid string of length 0, my code will write before the beginning of
the array, invoking undefined behavior.

Yes, I realise thats what he was trying to trap. I don't consider a
string of length zero to be a valid string however. YMMV, as may
Paul's.
But I don't see the need to put everything into the index expression.

me neither - it was unnecessarily obfuscated and would have failed a
code review at my shop.
 
K

Keith Thompson

Mark McIntyre said:
Yes, I realise thats what he was trying to trap. I don't consider a
string of length zero to be a valid string however. YMMV, as may
Paul's.

Of course a string of length zero is a valid string; why wouldn't it
be? Almost any function in the standard library that accepts a string
will happily accept an empty string.
 
P

pete

Mac said:
John Smith wrote:
BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.


#include <stdio.h>
#include <string.h>

int main(void)
{
char aGroup[2048];
int i;
strcpy(aGroup, "Hello Sailor");
i = strlen(aGroup);
while (i--) {
puts(aGroup);
aGroup = '\0';
}
return 0;
}

Why is this thread so long?


Is this like a Zen Koan or something?
Does the behavior of this program
somehow shed light on what is wrong with the OP's code fragment?
If so, I don't get it. :-(


The program takes advantage of the fact that there is
nothing extraordinary about a 2048 byte automatic object,
contrary to what John Smith suggested.
The program shortens string length by 1,
and the program is portable.
 
P

pete

Keith said:
Of course a string of length zero is a valid string; why wouldn't it
be? Almost any function in the standard library that accepts a string
will happily accept an empty string.

Almost?
 
K

Keith Thompson

pete said:

I was thinking of the mode argument to fopen(). An empty string is
invalid not because it's empty, but because it's not one of the
allowed forms. There may be other examples.
 
M

Mark McIntyre

Of course a string of length zero is a valid string; why wouldn't it
be?

Because it contains no characters.
Almost any function in the standard library that accepts a string
will happily accept an empty string.

Sure. That doesn't mean that /I/ consider a zero length string to be a
valid string. Personally I would have trapped that long before trying
to manipulate it, so Paul's obfuscated code would have been quite
gratuitous.
 
M

Mac

Mac said:
John Smith wrote:
BTW, aGroup is declared as "char aGroup[2048];" which is probably
10 times larger than what it can be.


#include <stdio.h>
#include <string.h>

int main(void)
{
char aGroup[2048];
int i;
strcpy(aGroup, "Hello Sailor");
i = strlen(aGroup);
while (i--) {
puts(aGroup);
aGroup = '\0';
}
return 0;
}

Why is this thread so long?


Is this like a Zen Koan or something?
Does the behavior of this program
somehow shed light on what is wrong with the OP's code fragment?
If so, I don't get it. :-(


The program takes advantage of the fact that there is
nothing extraordinary about a 2048 byte automatic object,
contrary to what John Smith suggested.
The program shortens string length by 1,
and the program is portable.


I read what John Smith wrote a little differently. According to my
reading, he was emphasizing that 2048 bytes is 10 times longer than the
actual string can be, so there is no danger of an overflow. I could be
wrong.

Anyway, John Smith is obstinate and resistant to suggestions that could
help him, so I don't want to defend him. And I guess I don't really want
to say anything more in this thread.

HAND. ;-)

--Mac
 
K

Keith Thompson

Mark McIntyre said:
Because it contains no characters.

You're using the term "valid" in a very odd sense.

An empty string may be invalid for some purposes. A string containing
non-printable characters, or one containing the letter 'q', may be
invalid for other purposes.

If you mean that an empty string is invalid in the limited context of
wanting to shorten a string by 1 character, I agree. (Note that if
the initial string has a length of 1, the *result* is a perfectly
valid empty string.)

Would you have the compiler issue a warning on
char *s = "";
or
char s[] = "";
?
 
M

Mark McIntyre

You're using the term "valid" in a very odd sense.

How so? The context was how to shorten a string by one character. I'm
unsure how a zero length string can be valid for that operation. And
FWIW I consider it very odd to think of a string with zero length as
being a valid one.
An empty string may be invalid for some purposes.

such as the one under discussion. And even setting aside the context,
I still don't consider a character array with only a null in it to be
a string. YMMV.
Would you have the compiler issue a warning on
char *s = "";
or
char s[] = "";

Nope. Why should I? Merely because its (in my opinion) not a
well-formed string, doesn't make it any the less a useful code
construct. Come on, try not to be so silly.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top