Go Down

Topic: How to add a char to a string? (Read 11625 times) previous topic - next topic


I'm taking in data from a GPS module, but the data comes in one character at a time.
I want to take each character and add add it to a string, so that the string gets bigger and bigger until I am done collecting the data.

strcat(Latitude, DATAIN) doesn't work for me (if DataIn is the character)

So what can I do?

Just in case this helps:
Code: [Select]
#include <string.h>
#include <ctype.h>
int ledPin = 13;                  // LED test pin
int rxPin = 0;                    // RX PIN
int txPin = 1;                    // TX TX
int byteGPS=-1;
char data[300] = "";
char GPRString[7] = "$GPRMC";
int count=0;
int ready=0;
int counta=0;
int info[13];

char Latitude[50];
char Storage;

void setup() {
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  for (int i=0;i<300;i++){       // Initialize a buffer for received data
    data[i]=' ';

void loop() {

void getGPS(){
  byteGPS=Serial.read();         // Read a byte of the serial port
  if (byteGPS == -1) {           // See if the port is empty yet
  } else {
    data[counta]=byteGPS;        // If there is serial port data, it is put in the buffer
    if (byteGPS==13){            // If the received byte is = to 13, end of transmission
      for (int i=1;i<7;i++){     // Verifies if the received command starts with $GPR
        if (data[i]==GPRString[i-1]){
      if(ready==6){               // If yes, countinue and process the data
        for (int i=0;i<300;i++){
          if (data[i]==','){    // check for the position of the  "," separator
          if (data[i]=='*'){    // ... and the "*"
        for (int i=0;i<12;i++){
            case 0 :;break;
            case 1 :;break;
            case 2 :for (int j=info[i];j<(info[i+1]-1);j++){Storage = data[j+1]; strcat(Latitude,Storage);};break;
            case 3 :;break;
            case 4 :;break;
            case 5 :;break;
            case 6 :;break;
            case 7 :;break;
            case 8 :;break;
            case 9 :;break;
            case 10 :;break;
            case 11 :;break;
            case 12 :;break;
      counta=0;                    // Reset the buffer
      for (int i=0;i<300;i++){    //  
        data[i]=' ';            

Rusty in Texas

Feb 19, 2009, 08:00 am Last Edit: Feb 19, 2009, 08:05 am by Fe2o3Fish Reason: 1
First and foremost -- strcat() is for appending two strings, not adding a single character to the end of a string.   It would be best to simply
keep an index to the last character you appended and use that index
to point to the next place your data array can accept another char,
incrementing the index for each addition.

Also, strcat() only works with strings that are NUL terminated which
is something you are not doing.  Probably the easiest way to do this
is to initialize your data array in setup() and at the end of GetGPS()
to NUL's, not spaces.

You can use memset(data, 0, sizeof(data)); to do this for you.

Also, when you declare char data[300] -- there's no need to assign
an initial value of "" since data[] will be filled in by the setup() function.


If you really want to use 'strcat':

Code: [Select]
char str1[32];
char str2[2];

str2[0] = '!';  // Char to append to string
str2[1] = '\0';

strcpy (str1, "hello");
strcat (str1, str2);


Feb 19, 2009, 02:06 pm Last Edit: Feb 19, 2009, 02:08 pm by halley Reason: 1
C++ can fix this, implementing another interface for the strcat() function itself.

Code: [Select]
void strcat(char* original, char appended)
   while (*original++)
   *original++ = appended;
   if (appended)
       *original = '\0';

After defining this function in your sketch, feel free to strcat(myStringBuffer, 'A') to add the letter A to the end of myStringBuffer.


Out of my league a bit, but
strcat(myStringBuffer, "A")
is OK, right?

"Data is not information, information is not knowledge, knowledge is not understanding, understanding is not wisdom."
~ Clifford Stoll


Yes, because "A" is a string with one character in it, whereas 'A' is a character constant that is equivalent to the ASCII code for capital A.  Note the use of double and single quotes!  In memory, a constant like "A" will generate two bytes, one containing the ASCII code for A, and the other holding a zero byte to indicate end-of-string.  Hope that makes sense!


I didn't realize that 'A' and "A" are completely different.

Now I can know my speed no matter what!

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131