Thursday, July 30, 2009

How do you concatenate a single char onto a string in C?

Given: string s, char c as in:





char s[ 256 ]; //256 is assumed, it can be anything.


char c;





Way 1.


=====





char temp[ 2 ];


temp[ 0 ] = c;


temp[ 1 ] = '\0';


strcat( s, temp );





Way 2.


=====





int iOriginalLength = strlen( s );


s[ iOriginalLength ] = c;


s[ iOriginalLength + 1 ] = '\0';





Way 3


=====


#include %26lt;string%26gt;


using namespace std;


string sNew = s;


sNew += c;


strcpy( s, sNew.c_str() );

How do you concatenate a single char onto a string in C?
Strings in C


Follow these fast links:





strlen()


strcpy()


strncpy()


strcat()


strncat()


strcmp()


strncmp()


Miscellaneous Notes


This discussion of string handling in C presumes that the following compiler directive is used.





#include %26lt;string.h%26gt;





On Linux, this file is located in /usr/include. Only the basic functions will be discussed here, so you may want to investigate string.h.





In C, a string is stored as a null-terminated char array. This means that after the last truly usable char there is a null, hex 00, which is represented in C by '\0'. The subscripts used for the array start with zero (0). The following line declares a char array called str. C provides fifteen consecutive bytes of memory. N.B. Only the first fourteen bytes are usable for character storage, because one must be used for the string-terminating null.





char str[15];





The following is a representation of what would be in RAM, if the string "Hello, world!" is stored in this array.





Characters: H e l l o , w o r l d !


Hex values: 48 65 6C 6C 6F 2C 20 77 6F 71 6C 64 21 00


Subscripts: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14





The name of the array is treated as a pointer to the array. The subscript serves as the offset into the array, i.e., the number of bytes from the starting memory location of the array. Thus, both of the following will save the address of the 0th character in the pointer variable ptr.





ptr = str;


ptr = %26amp;str[0];





Back to Top





strlen()


Syntax: len = strlen(ptr);


where len is an integer and


ptr is a pointer to char





strlen() returns the length of a string, excluding the null. The following code will result in len having the value 13.





int len;


char str[15];





strcpy(str, "Hello, world!");


len = strlen(str);





Back to Top





strcpy()


Syntax: strcpy(ptr1, ptr2);


where ptr1 and ptr2 are pointers to char





strcpy() is used to copy a null-terminated string into a variable. Given the following declarations, several things are possible.





char S[25];


char D[25];





Putting text into a string:





strcpy(S, "This is String 1.");





Copying a whole string from S to D:





strcpy(D, S);





Copying the tail end of string S to D:





strcpy(D, %26amp;S[8]);





N.B. If you fail to ensure that the source string is null-terminated, very strange and sometimes very ugly things may result.





Back to Top





strncpy()


Syntax: strncpy(ptr1, ptr2, n);


where n is an integer and


ptr1 and ptr2 are pointers to char





strncpy() is used to copy a portion of a possibly null-terminated string into a variable. Care must be taken because the '\0' is put at the end of destination string only if it is within the part of the string being copied. Given the following declarations, several things are possible.





char S[25];


char D[25];





Assume that the following statement has been executed before each of the remaining code fragments.





Putting text into the source string:





strcpy(S, "This is String 1.");





Copying four characters from the beginning of S to D and placing a null at the end:





strncpy(D, S, 4);


D[4] = '\0';





Copying two characters from the middle of string S to D:





strncpy(D, %26amp;S[5], 2);


D[2] = '\0';





Copying the tail end of string S to D:





strncpy(D, %26amp;S[8], 15);





which produces the same result as strcpy(D, %26amp;S[8]);





Back to Top





strcat()


Syntax: strcat(ptr1, ptr2);


where ptr1 and ptr2 are pointers to char





strcat() is used to concatenate a null-terminated string to end of another string variable. This is equivalent to pasting one string onto the end of another, overwriting the null terminator. There is only one common use for strcat().





char S[25] = "world!";


char D[25] = "Hello, ";





Concatenating the whole string S onto D:





strcat(D, S);





N.B. If you fail to ensure that the source string is null-terminated, very strange and sometimes very ugly things may result.





Back to Top





strncat()


Syntax: strncat(ptr1, ptr2, n);


where n is an integer and


ptr1 and ptr2 are pointers to char





strncat() is used to concatenate a portion of a possibly null-terminated string onto the end of another string variable. Care must be taken because some earlier implementations of C do not append the '\0' at the end of destination string. Given the following declarations, several things are possible, but only one is commonly used.





char S[25] = "world!";


char D[25] = "Hello, ";





Concatenating five characters from the beginning of S onto the end of D and placing a null at the end:





strncat(D, S, 5);


strncat(D, S, strlen(S) -1);





Both would result in D containing "Hello, world".


N.B. If you fail to ensure that the source string is null-terminated, very strange and sometimes very ugly things may result.





Back to Top





strcmp()


Syntax: diff = strcmp(ptr1, ptr2);


where diff is an integer and


ptr1 and ptr2 are pointers to char





strcmp() is used to compare two strings. The strings are compared character by character starting at the characters pointed at by the two pointers. If the strings are identical, the integer value zero (0) is returned. As soon as a difference is found, the comparison is halted and if the ASCII value at the point of difference in the first string is less than that in the second (e.g. 'a' 0x61 vs. 'e' 0x65) a negative value is returned; otherwise, a positive value is returned. Examine the following examples.





char s1[25] = "pat";


char s2[25] = "pet";





diff will have a negative value after the following statement is executed.








diff = strcmp(s1, s2);





diff will have a positive value after the following statement is executed.








diff = strcmp(s2, s1);





diff will have a value of zero (0) after the execution of the following statement, which compares s1 with itself.








diff = strcmp(s1, s1);





Back to Top





strncmp()


Syntax: diff = strncmp(ptr1, ptr2, n);


where diff and n are integers


ptr1 and ptr2 are pointers to char





strncmp() is used to compare the first n characters of two strings. The strings are compared character by character starting at the characters pointed at by the two pointers. If the first n strings are identical, the integer value zero (0) is returned. As soon as a difference is found, the comparison is halted and if the ASCII value at the point of difference in the first string is less than that in the second (e.g. 'a' 0x61 vs. 'e' 0x65) a negative value is returned; otherwise, a positive value is returned. Examine the following examples.





char s1[25] = "pat";


char s2[25] = "pet";





diff will have a negative value after the following statement is executed.








diff = strncmp(s1, s2, 2);





diff will have a positive value after the following statement is executed.








diff = strncmp(s2, s1, 3);





diff will have a value of zero (0) after the following statement.








diff = strncmp(s1, s2, 1);





Back to Top





Miscellaneous Notes


Single characters can be replaced in a string. Given the following declarations, several things are possible.





char str[25] = "cot";


char ch = 'u';


char D[25] = "pat";





Replacing a single character using a char variable:





D[1] = ch;





This would result in D containing "put".





Replacing a single character using a char literal:





D[1] = 'e';





This would result in D containing "pet".





Replacing a single character using a single character from a string variable:





D[1] = str[1];





This would result in D containing "pot".





Back to Top
Reply:Alternatively, way easier:





- put the character into a new string, and use strcat





or





- If you are absolutely CERTAIN that sizeof(string) %26gt; strlen(string), do:





string[strlen(string)] = char; /* copy the string as last */


string[strlen(string)] = '\0'; /* Terminate with NULL */





explanation: since we count offsets from 0 in C, string[strlen(string)] points to the null at the end of the string. Replace it with the char in question. Then, strlen(string) points at the NEW end of the string - make sure there's a null there.





BE CAREFUL. If the string doesnt have enough memory, you risk a buffer overflow! This could lead to miscellaneous weird effects, culminating in program crash, or even code injection.





Hope This Helps,





J


No comments:

Post a Comment