Archived

This topic is now archived and is closed to further replies.

NewbJ

Arrays and String functions

Recommended Posts

NewbJ    226
Well, I found the problem with my other code, but now I have a new problem...I know a couple solutions, but none of them seem to work for me. I''m trying to put a single character onto the end of a string. I tried using strcat(), but apparently you can only use strings, not single characters, in that. I also tried using code like this: //assuming that both the string and character have valid values //in them, and that the string is not full string[strlen(string) + 1] = character1; As you can see, I''m trying to put the character in the next empty element of the array...the string''s length +1. It compiles and runs, but I just get a few meaningless characters on the end of the string where character1 should be.

Share this post


Link to post
Share on other sites
Sneftel    1788
You''ll also have to null-terminate the string. by adding a \0 after the last character. When you append the character, you''re overwriting the current terminator.

Are you using C++? if so, using std::string is a much better solution than strcat() et al.


"Sneftel is correct, if rather vulgar." --Flarelocke

Share this post


Link to post
Share on other sites
UberGeek    138
Your probably removing the null terminator. Tack another one at the end after the character is that your adding.

Also don''t overflow any buffers!

Share this post


Link to post
Share on other sites
-silencer-    144
do that.. but you have to add ''\0'' to the end of your string..

string[strlen(string) + 1] = character1;
string[strlen(string) + 2] = ''\0'';

make sure ya don''t go out of the array bounds though.. ''\0'' counts as a character in the array.

(silencer)

Share this post


Link to post
Share on other sites
chippolot    122
Mmm... I could be wrong but I think you have to null terminate the string with a ''/0'' after the new char you want to add.

The junk you''re getting is probably due to that

Share this post


Link to post
Share on other sites
arm    164
First dont forget that strlen("aaaa") will give 4 but since strings are indexed from zero the position of the last character will be 3. So you should be using:

string[strlen(string)] = character1;

Another point is that this will overwrite the null terminating character, so unless the string was already padded with zero''s, you will have to add that after the new character.

Share this post


Link to post
Share on other sites
Brennon    122

Howdy, not sure if this will help but I have had dramas with strings and char arrays in the past so i will try my best.

I dont know if you are using a string from the std::string class, if not than maybe have a look at it as it can help a lot

lets say that you have a string already defined as in the following

string command1="Howdy there";

and you want to convert it to a char array than you would do this

char NewCommand1[80];
strcpy(NewCommand1,command1.c_str ());

but lets say it was the other way around and you had a value alreayd in a char array like this

char In_COMMAND[]="Howdy";

then you would append it to a string like this

string Master_COMMAND;
Master_COMMAND.append(In_COMMAND);


you can loop through all the characters in the string by doing this

string Master_COMMAND="Howdy there";

if(Master_COMMAND.substr(0,1)="H")
debug.print"It worked";

hopefully this may help a little





Share this post


Link to post
Share on other sites
NewbJ    226
thanks everyone...I think my problem was that I had overwritten the null terminator and hadn''t put one back in, but I''ll take a look at the std::string class too

Share this post


Link to post
Share on other sites