C/String/String Append
Содержание
- 1 Append string: how to use strcat
- 2 Append string: strncat
- 3 Append string to another string
- 4 Append substring to string: strncat
- 5 Demonstrates how to put strings together using strcat
- 6 Get first and last name and print them together
- 7 Join strings
- 8 Join strings: revitalised: strlen and strcat
- 9 String concatenate
Append string: how to use strcat
#include <stdio.h>
#include <string.h>
int main ()
{
char str[80];
strcpy (str,"part I, ");
strcat (str,"part II, ");
strcat (str,"part III.");
puts (str);
return 0;
}
Append string: strncat
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[80], s2[80];
unsigned int len;
gets(s1);
gets(s2);
len = 79 - strlen(s2);
strncat(s2, s1, len);
printf(s2);
return 0;
}
Append string to another string
#include <stdio.h>
#include <string.h>
int main(void)
{
char s1[80], s2[80];
gets(s1);
gets(s2);
strcat(s2, s1);
printf(s2);
return 0;
}
Append substring to string: strncat
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[20];
char str2[20];
strcpy (str1,"qqq34567790");
strcpy (str2,"333333");
strncat (str1, str2, 6);
puts (str1);
return 0;
}
Demonstrates how to put strings together using strcat
#include <string.h>
#include <stdio.h>
int main()
{
char first[100];
char last[100];
char full_name[200];
strcpy(first, "firstName");
strcpy(last, "secondName");
strcpy(full_name, first);
strcat(full_name, " ");
strcat(full_name, last);
printf("The full name is %s\n", full_name);
return (0);
}
Get first and last name and print them together
#include <stdio.h>
#include <string.h>
int main() {
char first[100];
char last[100];
char full[100];
printf("Enter first name: ");
fgets(first, sizeof(first), stdin);
/* trim off last character */
first[strlen(first)-1] = "\0";
printf("Enter last name: ");
fgets(last, sizeof(last), stdin);
/* trim off last character */
last[strlen(last)-1] = "\0";
strcpy(full, first);
strcat(full, " ");
strcat(full, last);
printf("The name is %s\n", full);
return (0);
}
Join strings
/* Joining strings */
#include <stdio.h>
void main()
{
char str1[40] = "To be or not to be";
char str2[40] = ",that is the question";
int count1 = 0; /* Length of str1 */
int count2 = 0; /* Length of str2 */
/* find the length of str1 */
while (str1[count1] != "\0") /* Increment count till we reach the terminating character*/
count1++;
/* Find the length of str2 */
while (str2[count2] != "\0") /* Count characters in second string */
count2++;
/* Check that we have enough space for both strings */
if(sizeof str1 < count1 + count2 + 1)
printf("\nYou can"t put a quart into a pint pot.");
else
{ /* Copy 2nd string to end of the first */
count2 = 0; /* Reset index for str2 to 0 */
while(str2[count2] != "\0") /* Copy up to null from str2 */
str1[count1++] = str2[count2++];
str1[count1] = "\0"; /* Make sure we add terminator */
printf("\n%s\n", str1 ); /* Output combined string */
}
}
Join strings: revitalised: strlen and strcat
#include <stdio.h>
#include <string.h>
#define STR_LENGTH 40
void main() {
char str1[STR_LENGTH] = "String in C";
char str2[STR_LENGTH] = ",String in C";
if(STR_LENGTH > strlen(str1) + strlen(str2)) /* Enough space ? */
printf("\n%s\n", strcat(str1, str2) ); /* yes, so print joined string */
else
printf("\n no enough room.");
}
String concatenate
#include <stdio.h>
#define concat(a, b) a ## b
int main(void)
{
int xy = 10;
printf("%d", concat(x, y));
return 0;
}