Home >Computer Tutorials >Computer Knowledge >C programming: use a loop to concatenate two strings and avoid using the strcat function

C programming: use a loop to concatenate two strings and avoid using the strcat function

王林
王林forward
2024-01-22 17:33:061269browse

C language programming: 5. Write a program to connect two strings without using the strcat function

Idea: String concatenation requires determining the end position of the first string, then placing the elements of the second string after the first string, and finally adding the end mark.

Reference code: splicing 123 and 456

#include

void mystrcat(char a[],char b[]){//String connection function

int i=0,j=0;

while(a[i ]!='\0'); //Find the end position of a

i--;

while(b[j]!='\0'){//Assign b element to a

a[i ]=b[j ];

}

a[i]='\0'; //Add end mark

}

int main()

{

char a[100],b[100];

scanf("%s%s",a,b);

mystrcat(a,b);

puts(a);

return 0;

}

/*

operation result:

123 456

123456

*/

How to solve string concatenation in C language?

You can use the string connection function strcat() function, the header file is #include;

Examples are as follows:

Two strings char [100]="abc",b[50]="def";

Convert it into a string and output

#include

#include

int main()

{

char a[100]="abc",b[50]="def";

strcat(a,b);

printf("%s\n",a);

}

C programming: use a loop to concatenate two strings and avoid using the strcat function

Expand information

C language is a general computer programming language that is widely used in low-level development. The design goal of the C language is to provide a programming language that can be easily compiled, handle low-level memory, generate a small amount of machine code, and can run without any runtime environment support.

Although C language provides many low-level processing functions, it still maintains good cross-platform characteristics. C language programs written in a standard specification can be compiled on many computer platforms, even including some embedded processors. (Single-chip microcomputer or MCU) and supercomputers and other operating platforms.

In the 1980s, in order to avoid differences in the C language syntax used by various developers, the American National Bureau of Standards formulated a complete set of American national standard syntax for C language, called ANSI C, as the C language original standard. Currently, on December 8, 2011, the C11 standard released by the International Organization for Standardization (ISO) and the International Electrotechnical Commission (IEC) is the third official standard of the C language and the latest standard of the C language. This standard better supports Chinese characters. Function names and Chinese character identifiers realize Chinese character programming to a certain extent.

C language is a process-oriented computer programming language, which is different from object-oriented programming languages ​​such as C and Java.

Its compilers mainly include Clang, GCC, WIN-TC, SUBLIME, MSVC, Turbo C, etc.

The above is the detailed content of C programming: use a loop to concatenate two strings and avoid using the strcat function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:docexcel.net. If there is any infringement, please contact admin@php.cn delete