C/String/String Append — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 13:22, 25 мая 2010

Append string: how to use strcat

<source lang="cpp">

  1. include <stdio.h>
  2. include <string.h>

int main () {

 char str[80];
 strcpy (str,"part I,  ");
 strcat (str,"part II, ");
 strcat (str,"part III.");
 puts (str);
 return 0;

}


      </source>


Append string: strncat

<source lang="cpp">

  1. include <stdio.h>
  2. 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;

}

      </source>


Append string to another string

<source lang="cpp">

  1. include <stdio.h>
  2. include <string.h>

int main(void) {

 char s1[80], s2[80];
 gets(s1);
 gets(s2);
 strcat(s2, s1);
 printf(s2);
 return 0;

}

      </source>


Append substring to string: strncat

<source lang="cpp">

  1. include <stdio.h>
  2. 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;

}

      </source>


Demonstrates how to put strings together using strcat

<source lang="cpp">

  1. include <string.h>
  2. 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);

}


      </source>


Get first and last name and print them together

<source lang="cpp">

  1. include <stdio.h>
  2. 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);

}


      </source>


Join strings

<source lang="cpp"> /* Joining strings */

  1. 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      */
 }

}


      </source>


Join strings: revitalised: strlen and strcat

<source lang="cpp">

  1. include <stdio.h>
  2. include <string.h>
  3. 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.");

}


      </source>


String concatenate

<source lang="cpp">

  1. include <stdio.h>
  2. define concat(a, b) a ## b

int main(void) {

 int xy = 10;
 printf("%d", concat(x, y));
 return 0;

}

      </source>