"C Language" and "algorithm" String concatenation Methods: C language String concatenation

Source: Internet
Author: User

"C Language" and "algorithm" String concatenation Methods: C language String concatenation

 

1 # include <stdio. h> 2 3 int main () {4 char str1 [20] = "Hello"; 5 char str2 [20] = "World! "; 6 char str3 [20]; 7 puts (strcat (str1, str2); 8 return 0; 9}Use a String concatenation function to merge two strings.

 

 

1 # include <stdio. h> 2 3/* 4 Note: Do not use a string to connect to function 5 to compile a program. Input three strings (no more than 20 characters in length) and store them in a two-dimensional arrays, 6. Connect the 3rd strings to the second string to form a new string and store it in the converted array. 7. Then, output the new string 8 x/9 10 int main () {11 char dest [61], str [3] [21]; 12 int I, j, k; 13 scanf ("% s ", str [0], str [1], str [2]); 14 k = 0; 15 for (I = 0; I <3; I ++) {16 for (j = 0; j <20; j ++) {17 if (str [I] [j] = '\ 0') 18 break; 19 else {20 dest [k] = str [I] [j]; 21 k ++; 22} 23} 24} 25 dest [k] = '\ 0 '; 26 printf ("% s", dest); 27 return 0; 28}Process the merge of two strings in an array Loop

 

 

1 # include <stdio. h> 2 3 int main () {4 char str1 [80], str2 [30], * ptr1 = str1, * ptr2 = str2; 5 printf ("input str1: "); 6 gets (str1); 7 printf (" input str2: "); 8 gets (str2); 9 printf (" str1 ---------- str2 \ n "); 10 printf ("% s ....... % s \ n ", ptr1, ptr2); 11 while (* ptr1) ptr1 ++;/* move the pointer to the end of the string */12 while (* ptr2) * ptr1 ++ = * ptr2 ++;/* connection string */13 * ptr1 = '\ 0';/* End mark of the write string */14 ptr1 = str1; ptr2 = str2; 15 printf ("str1 ---------- str2 \ n"); 16 printf ("% s ....... % s \ n ", ptr1, ptr2); 17 return 0; 18}Use Pointer variables pointing to strings to merge two strings

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.