当前位置:网站首页>C language: some self realization of string functions
C language: some self realization of string functions
2022-07-28 05:17:00 【@Bu Xiangwan spicy】
Catalog
1. Find the first subscript of a character in another string
2. Return to the first address
3. Find the address where one string appears in another string abcccccde ccd
4. Count the number of words in the string
5. Insert one string into another
One 、 String function
1. Realize my strlen()
#include <stdio.h>
int MyStrlen(char* str)
{
int count = 0;
while (*str != '\0')
{
count++;
str++;
}
str = '\0';
return count;
}
int main()
{
char str[] = "abcde";
printf("%d\n", MyStrlen(str));
return 0;
}
2. My string copy
#include <stdio.h>
char* MyStrcpy(char* str1, char* str2)
{
char* pMark = str1;
while (*str2 != '\0')
{
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
return pMark;
}
int main()
{
char str[20];
char str1[] = "abcde";
printf("%s\n", MyStrcpy(str, str1));
return 0;
}3. String splicing
#include <stdio.h>
char* MyStrCat(char* str1, char* str2)
{
char* pMark = str1;
while (*str1 != '\0')
{
str1++;
}
while (*str2 != '\0')
{
*str1 = *str2;
str1++;
str2++;
}
*str1 = '\0';
return pMark;
}
int main()
{
char str[10] = "abcd";
char* str1 = "1234";
printf("%s\n", MyStrCat(str,str1));
return 0;
}4. String comparison
#include <stdio.h>
int MyStrcmp(char* str1,char* str2)
{
/*while (*str1 != '\0' && *str2 != '\0') // The first method
{
if (*str1 > *str2)
{
return 1;
}
else if (*str1 < *str2)
{
return -1;
}
else
{
str1++;
str2++;
}
}
if (*str1 > *str2)
{
return 1;
}
else if (*str1 < *str2)
{
return -1;
}
else
{
return 0;
}*/
while (*str1 != '\0' || *str2 != '\0') // The second method
{
if (*str1 > *str2)
{
return 1;
}
else if (*str1 < *str2)
{
return -1;
}
else
{
str1++;
str2++;
}
}
return 0;
}
int main()
{
printf("%d\n", MyStrcmp("abc", "abc"));
printf("%d\n", MyStrcmp("ab", "abc"));
printf("%d\n", MyStrcmp("ac", "abc"));
return 0;
}Two 、 String practice
1. Find the first subscript of a character in another string
#include <stdio.h>
int T1(char* str, char ch)
{
int count = 0;
while (*str != '\0')
{
if (*str == ch)
{
return count;
}
str++;
count++;
}
return -1;
}
int main()
{
printf("%d\n", T1("abcd", 'b'));
return 0;
}Output :
1 // Because it is the following table, it is the second element
D:\c++\c Language \ character string \x64\Debug\ character string .exe ( process 4356) Exited , The code is 0.
To automatically close the console when debugging stops , Please enable “ Tools ”->“ Options ”->“ debugging ”->“ Automatically close the console when debugging stops ”.
Press any key to close this window . . .2. Return to the first address
#include <stdio.h>
char* T2(char* str, char ch)
{
while (*str != '\0')
{
if (*str == ch)
{
return str;
}
str++;
}
return NULL;
}
int main()
{
printf("%c\n",*T2("abcd",'b'));
return 0;
}Output
b // Because you want to be easy to observe So use %c Print
D:\c++\c Language \ character string \x64\Debug\ character string .exe ( process 17304) Exited , The code is 0.
To automatically close the console when debugging stops , Please enable “ Tools ”->“ Options ”->“ debugging ”->“ Automatically close the console when debugging stops ”.
Press any key to close this window . . .3. Find the address where one string appears in another string abcccccde ccd
#include <stdio.h>
#include <stdlib.h>
char* t3(char* str1, char* str2)
{
while (*str1 != '\0')
{
if (0 == strncmp(str1, str2, strlen(str2)))
{
return str1;
}
str1++;
}
return NULL;
}
int main()
{
printf("%s\n", t3("abcccccde", "ccd"));
return 0;
}Output
ccde // For convenience of observation , So use %c Print
D:\c++\c Language \ character string \x64\Debug\ character string .exe ( process 22856) Exited , The code is 0.
To automatically close the console when debugging stops , Please enable “ Tools ”->“ Options ”->“ debugging ”->“ Automatically close the console when debugging stops ”.
Press any key to close this window . . .4. Count the number of words in the string
#include <stdio.h>
int T4(char* str)
{
int count = 0;
while (*str != '\0')
{
if (*str == ' ')
{
count++;
}
str++;
}
return count + 1;
}
int main()
{
printf("%d\n", T4("i love you"));
return 0;
}Output
3
D:\c++\c Language \ character string \x64\Debug\ character string .exe ( process 19016) Exited , The code is 0.
To automatically close the console when debugging stops , Please enable “ Tools ”->“ Options ”->“ debugging ”->“ Automatically close the console when debugging stops ”.
Press any key to close this window . . .
5. Insert one string into another
#include <stdio.h>
void T5(char* str1, char* str2, int n)
{
char* phead = str1 + n - 1;
char* pend = str1 + strlen(str2);
while(phead <= pend)
{
*(pend + strlen(str2)) = *pend;
pend--;
}
while (*str2 != '\0')
{
*phead = *str2;
phead++;
str2++;
}
}
int main()
{
char str[20] = "abcd";
T5(str, "12345", 3);
printf("%s\n", str);
return 0;
}Output
ab12345cd
D:\c++\c Language \ character string \x64\Debug\ character string .exe ( process 14688) Exited , The code is 0.
To automatically close the console when debugging stops , Please enable “ Tools ”->“ Options ”->“ debugging ”->“ Automatically close the console when debugging stops ”.
Press any key to close this window . . .
6. Flip strings
#include <stdio.h>
void T6(char* str)
{
char* pbegin = str;
char* pend = str + strlen(str) - 1;
while (pbegin < pend)
{
char ptemp = *pbegin;
*pbegin = *pend;
*pend = ptemp;
pbegin++;
pend--;
}
}
int main()
{
char str[20] = "abcd";
T6(str);
printf("%s\n", str);
return 0;
}Output
dcba
D:\c++\c Language \ character string \x64\Debug\ character string .exe ( process 9832) Exited , The code is 0.
To automatically close the console when debugging stops , Please enable “ Tools ”->“ Options ”->“ debugging ”->“ Automatically close the console when debugging stops ”.
Press any key to close this window . . .
7.GetString()
#include <stdio.h>
#include <stdlib.h>
char* Getstring()
{
int size = 5;
char* str = (char*)malloc(sizeof(char) * size);
int count = 0;
char ch;
while ((ch = getchar()) != '\n')
{
str[count] = ch;
count++;
if (count == size)
{
size += 5;
str = realloc(str, size);
}
}
str[count] = '\0';
return str;
}
int main()
{
char* str = Getstring();
printf("%s\n", str);
free(str);
return 0;
}
Output
1234
1234
D:\c++\c Language \ character string \x64\Debug\ character string .exe ( process 18236) Exited , The code is 0.
To automatically close the console when debugging stops , Please enable “ Tools ”->“ Options ”->“ debugging ”->“ Automatically close the console when debugging stops ”.
Press any key to close this window . . .
边栏推荐
- PC端-bug记录
- Tips for using swiper (1)
- Interpretation of afnetworking4.0 request principle
- Autoreleasepool problem summary
- [slam] lvi-sam analysis - Overview
- MySQL basic query
- JSON in JS (launch object deep copy)
- RT based_ Distributed wireless temperature monitoring system of thread (I)
- Professor dongjunyu made a report on the academic activities of "Tongxin sticks to the study of war and epidemic"
- FPGA: use PWM wave to control LED brightness
猜你喜欢

7. < tag string and API trade-offs> supplement: Sword finger offer 05. replace spaces

The most detailed installation of windows10 virtual machine, install virtual machine by hand, and solve the problem that the Hyper-V option cannot be found in the home version window

Google browser cannot open localhost:3000. If you open localhost, you will jump to the test address

数据库日期类型全部为0
![[high CPU consumption] software_ reporter_ tool.exe](/img/3f/2c1ecff0a81ead0448e1215567ede7.png)
[high CPU consumption] software_ reporter_ tool.exe
![Classes and objects [medium]](/img/0a/955d00d63f06e7e15e946599628edf.png)
Classes and objects [medium]

RT based_ Distributed wireless temperature monitoring system based on thread

Summary and review of puppeter

Mysql基本查询

The solution after the samesite by default cookies of Chrome browser 91 version are removed, and the solution that cross domain post requests in chrome cannot carry cookies
随机推荐
C language classic 100 question exercise (1~21)
【CVPR2022】On the Integration of Self-Attention and Convolution
HashSet add
What is the core value of testing?
Have you learned the common SQL interview questions on the short video platform?
Service object creation and use
Using RAC to realize the sending logic of verification code
Melt cloud x chat, create a "stress free social" habitat with sound
Automated test tool playwright (quick start)
How to quickly turn function test to automatic test
CPU and memory usage are too high. How to modify RTSP round robin detection parameters to reduce server consumption?
Why is MD5 irreversible, but it may also be decrypted by MD5 free decryption website
【CPU占用高】software_reporter_tool.exe
HDU 3592 World Exhibition (differential constraint)
Table image extraction based on traditional intersection method and Tesseract OCR
RT_ Use of thread message queue
【ARXIV2204】Vision Transformers for Single Image Dehazing
【ARXIV2204】Simple Baselines for Image Restoration
Offline loading of wkwebview and problems encountered
JSON in JS (launch object deep copy)