What is the ltoa function in C.

C functions and C functionalities I used in Arduino

(Concatenate, compare, copy, split, delete, ...)

  • strcat
  • strncat
  • strcmp
  • strcpy
  • strncpy
  • strlen
  • strstr
  • strtok

and

Concatenate strings with strcat (stringcatenate):

char * strcat (char * destination, const char * source);

Function: Connects source to destination, i.e. source is appended (chained) to destination

char text1 [10] = "12345678";

char text2 [10] = "absdefgh";

strcat (text1, text2);

Use strncat (string-n-catenate) to add n characters to a string:

char * strncat (char * destination, const char * source, size_t n);

Function: Connects the first n characters of source with destination, i.e. the first n characters of source are appended (concatenated) to destination

Append a single character:

char text1 [10] = "abc";

char c = 'x';

strncat (text1, & c, 1);

Append the first 2 characters of a character array:

char text1 [10] = "abcd";
char text2 [] = "ABCD";

strncat (text1, text2, 2);

Compare two strings with strcmp (stringcompare):

int strcmp (const char * str1, const char * str2);

Function: Compares the string str1 with str2

char text1 [] = "abcde";

char text2 [] = "abcdf";

if (strcmp (text1, text2) == 0) Serial.println ("Strings are identical");

Return:

  • 0 ... if the strings are identical
  • > 0 ... if the first unequal character in text1 is larger than in text2
  • <0 ... if the first unequal character in text1 is smaller than in text2

Compare a certain number of characters with strncmp (string-n-compare)

int strncmp (const char * str1, const char * str2, size_t n);

Function: Compares the string str1 with the first n characters of the string str2

char text1 [] = "abcde";

char text2 [] = "abcdf";

int number = 3;

if (strncmp (text1, text2, number) == 0) Serial.println ("the first 3 characters are identical");

Return:

see strcmp

Copy strings with strcpy (stringcopy):

char * strcpy (char * destination, const char * source);

Function: Copies source to destination

char text1 [5];

strcpy (text1, "ABCD");

or

char text1 [5];

char text2 [5] = "EFGH";

strcpy (text1, text2);

Copy n characters from strings with strncpy (string-n-copy)

char * strncpy (char * destination, const char * source, size_t n);

Function: Copies the first n characters from source to destination

char text1 [10] = "12345678";

char text2 [10];

strncpy (text2, text1, 3); // Copy 3 characters from text1 to text2

text2 [3] = '\ 0' // set end character !!!

Determine the length of a string with strlen (stringlength):

size_t strlen (const char * str);

Function: Determination of the length of the string str

char string [100] = "ABCD";

int stringlength = strlen (string);

Result = 4

Note: The zero terminator that ends a string is not counted!

Search substring in string with strstr (stringstring):

const char * strstr (const char * str1, const char * str2);
char * strstr (char * str1, const char * str2);

Function: Compares whether string str2 is contained in string str1

char text [] = "ABCDEFG";

if (strstr (text, "CDE")) Serial.println ("substring found");

Split a string with strtok (stringtoken):

char * strtok (char * str, const char * delimiters);

Function: Divides the string str into substrings, limited by delimiters

char text [] = "Kurt, Kanns; 5556; DE";

char delimiter [] = ",;"; // partial string delimiter

char * ptr; // NULL pointer

ptr = strtok (text, delimiter); // Initialize and first substring

while (ptr! = NULL) // More substrings

{

Serial.println (ptr);

ptr = strtok (NULL, delimiter);

}

Output on the serial monitor:

Kurt
Can
5556
DE

or

char text [] = "Kurt, Kanns; 5556; DE";

char * ptr = NULL; // NULL pointer

char first name [10];

char surname [20];

int personnel number;

ptr = strtok (text, ","); // Initialize and first substring

strcpy (first name, ptr);

ptr = strtok (NULL, ";"); // Second substring
strcpy (last name, ptr);
personnel number = atoi (strtok (NULL, ";")); // Third substring
Serial.println (first name);
Serial.println (last name);
Serial.println (personnel number);

Output on the serial monitor:

Kurt
Can
5556

Delete string:

char text [10] = "abcdefg";

text [0] = '\ 0'

or

strcpy (text, "");