6 Replies - 3013 Views - Last Post: 23 February 2011 - 10:47 AM Rate Topic: -----

#1 Guest_Kom*


Reputation:

Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:21 AM

If I were to do
(length of string is 10 chars)
strncpy(string, temp, (len-1));

is it the same as
string[10] = '\0';
?
Is This A Good Question/Topic? 0

Replies To: Removing last char of a string by moving null char left 1

#2 vividexstance   User is offline

  • Tiocfaidh ár lá
  • member icon

Reputation: 792
  • View blog
  • Posts: 2,873
  • Joined: 31-December 10

Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:30 AM

what character does temp hold? and is string already a NULL-terminated string? Also if string is 10 characters long, it would be
string[9] = '\0'
because you start counting from zero not one.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg   User is online

  • member icon

Reputation: 5488
  • View blog
  • Posts: 17,064
  • Joined: 25-December 09

Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:30 AM

Since you have a C-string with a length of 10 characters string[10] = '\0' would be writing outside of the bounds of the array. Remember in C arrays go from 0 to size -1. So in this case it should be: string[9] = '\0'.


Jim

This post has been edited by jimblumberg: 23 February 2011 - 10:31 AM

Was This Post Helpful? 1
  • +
  • -

#4 Guest_Kom*


Reputation:

Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:38 AM

View Postvividexstance, on 23 February 2011 - 10:30 AM, said:

what character does temp hold? and is string already a NULL-terminated string? Also if string is 10 characters long, it would be
string[9] = '\0'
because you start counting from zero not one.



View Postjimblumberg, on 23 February 2011 - 10:30 AM, said:

Since you have a C-string with a length of 10 characters string[10] = '\0' would be writing outside of the bounds of the array. Remember in C arrays go from 0 to size -1. So in this case it should be: string[9] = '\0'.


Jim

Ah, that's right!

Also, 'temp' was a copy of string, and 'string' is a null terminated string.
I was simply testing ways, because I can't figure out how to code this..

I'm supposed to remove the last element of a string by moving the null character left one place. I thought that if I were to simply copy the string - 1, it would solve it, but it doesn't :P.
Was This Post Helpful? 0

#5 Guest_Kom*


Reputation:

Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:40 AM

View PostKom, on 23 February 2011 - 10:38 AM, said:

View Postvividexstance, on 23 February 2011 - 10:30 AM, said:

what character does temp hold? and is string already a NULL-terminated string? Also if string is 10 characters long, it would be
string[9] = '\0'
because you start counting from zero not one.



View Postjimblumberg, on 23 February 2011 - 10:30 AM, said:

Since you have a C-string with a length of 10 characters string[10] = '\0' would be writing outside of the bounds of the array. Remember in C arrays go from 0 to size -1. So in this case it should be: string[9] = '\0'.


Jim

Ah, that's right!

Also, 'temp' was a copy of string, and 'string' is a null terminated string.
I was simply testing ways, because I can't figure out how to code this..

I'm supposed to remove the last element of a string by moving the null character left one place, using 'pointers', so I can't simply do
string[9] = '\0'
. I thought that if I were to simply copy the string - 1, it would solve it, but it doesn't :P.

Was This Post Helpful? 0

#6 Guest_Kom*


Reputation:

Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:41 AM

Ahh!! Sorry for the spam, I meant to edit my post, but I guess guests can not edit their posts :(.
The revised post is in the second one
Was This Post Helpful? 0

#7 jimblumberg   User is online

  • member icon

Reputation: 5488
  • View blog
  • Posts: 17,064
  • Joined: 25-December 09

Re: Removing last char of a string by moving null char left 1

Posted 23 February 2011 - 10:47 AM

But you can use strncpy() just the first 9 characters to your new location. Just be sure to attach the end of string char to the end. See this link: strncpy() it has the description along with an example.


Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1