Get the App
SLTechnology News&Howtos  ›  Development  › 

How to find the intersection of two strings in c language

Shulou Source: shulou.com Published: 2022-06-02 04:26:29 09月18日 Update

In this issue, the editor will bring you about how to find the intersection of two strings in c language. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

Finding the intersection of two strings seems simple, but there are a lot of details to consider.

My train of thought:

1. Simplify the two strings and reduce the repeated letters to one.

two。 Concatenate two strings and find out the characters that repeat twice with the help of a loop.

When you have an idea, start writing code.

1. Main () function

Train of thought:

1. Define two arrays tt [M], pp [M] that store strings

two。 Define the pointer * p to receive the return value of fun2 () and output intersection

3. Enter two strings (note the out-of-bounds problem here)

4. Call function

5. Output intersection

# include # include / / use strcat (), strlen () function # define M 50 void fun1 (char tt []); / / remove the same letters from the string char * fun2 (char tt []); / / find the duplicate (subset) int main () {char tt [M], pp [M]; / / define two arrays to store the string char * p / / accept the address returned by fun2 () and output the intersection string int i; gets (tt); / / abcdef123abc12 gets (pp); / / acef123ace124 fun1 (tt); / / call the function fun1 (pp) / / printf ("% s\ n", tt); / / for inspection use / / printf ("% s\ n", pp); strcat (tt,pp); / / printf ("% s\ n", tt); p=fun2 (tt); / / call function for (iDeplox * (pamphi)! ='\ 0' ) {printf ("% c", * (pairi)); / / acef123} return 0;} II. Fun1 () function

The function of fun1 () is to keep only one repeated character in the string.

Train of thought:

1. First of all, replace the repeated characters with''(here is a space).

two。 Remove''(space) after moving string array elements

Void fun1 (char tt []) / / erase the repetitive characters {int iJournal j; char ch; for

Tags: Character string two function intersection train of thought same address array space code first time output language content subset letter analysis loop one Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Docker Apple NVidia vpn Huawei