当前位置:网站首页>String Basics
String Basics
2022-06-12 20:53:00 【Maccy37】
character string
- #include<string.h>
- With 0( Integers 0) A string of characters at the end
->0 or “\0” It's the same , But and ‘0’ Different
- 0 String end flag , But it's not part of a string , The string length is the literal length of the character we see with our eyes ( Exclude this end flag 0)
- The string exists as an array , Access as an array or pointer ( You can traverse the string in an array )
String variable
- char work[10] = "working";
- char work_arr[] = "working"; // The string literal is used to initialize the array
- char *work_str = "working";
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
printf(" character string ---------------\n");
char work[10] = "working";
printf("work sizeof :%d\n", sizeof(work));
printf("work strlen :%d\n", strlen(work));
char work_arr[] = "working";
printf("work_arr sizeof :%d\n", sizeof(work_arr));
printf("work_arr 1strlen :%d\n", strlen(work_arr));
char *work_str = "working";
printf("work_str sizeof :%d\n", sizeof(work_str));// Indicates the pointer byte length
printf("work_str 1strlen :%d\n", strlen(work_str));
while (1)
{}
return 0;
}
Output :(strlen Calculate string length )

String input and output
String input and output
- char string[8];
- scanf("%s",string);
- printf("%s",string);
- scanf Read in a word ( Until space /tab/ Until you return )
- scanf unsafe , I don't know the length of the read content
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
printf(" String input and output --------------\n");
char str[20];
char str2[20];
scanf("%s", str);
scanf("%s", str2);
printf("*** %s &&& %s\n", str, str2);
while (1)
{}
return 0;
}
Output :scanf Output stops when it encounters a space , The input characters of the display screen are not completely output , They were given to str、str2 Two variables

Safe input
- char string[8];
- scanf("%7s",string);
- 7: Indicates the maximum number of characters allowed to be read , Smaller than array 1,7 = 8 - 1
Common mistakes
- char *string; // The pointer does not point to an actual valid address
- scanf("%s",string);
- Define string class type variables string, But it is not initialized to 0, There may be errors during all operations
An empty string
- char buffer[100] =" ";// An empty string ,buffer[0] == '\0'
- char buffer[ ] =" "; // Array length only 1
Array of strings
char **a
a It's a pointer , Point to another pointer , This pointer points to a character ( strand )
- char a[ ][ ] Two dimensional array variables The second bit length is a definite value (a Is an array a[0]->char [10])
- char *a[ ] (a[0]->char*)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
printf(" Array of strings --------------\n");
char a[][10] = { "Hello","World","abcdefg" }; //a[0] ->char[10]
char *a[] = { "Hello","World","abcdefg" }; //a[0] ->char *
char *seasons[4] = {"Winter","Spring","Summer","Fall"};
while (1)
{}
return 0;
}
Program parameters
- int main(int argc,const char *argv[ ])
- argv[0] It's the command itself
- Use Unix The symbolic link itself , Reflect the name of the symbolic link ( Represents the input parameter )

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
using namespace std;
int main(int argc, const char *argv[])
{
printf(" Array of strings --------------\n");
printf("\n");
for (int i = 0; i < argc; i++)
{
printf("string-->%d:%s\n",i,argv[i]);
}
while (1)
{}
return 0;
}Output :

Single character input and output
getchar
- int getchar(void)
- Read in a character from standard input
- The return type is int In order to return EOF(-1)
- Windows ->Ctrl-Z
- Unix ->Ctrl-D
putchar
- int putchar(int c)
- Write a character to standard output
- Return write a few characters ,EOF(end of file)(-1) Indicates write failure
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
using namespace std;
int main(int argc, const char *argv[])
{
printf(" Single character input and output --------------\n");
int ch = 0;
while ((ch = getchar()) != EOF)
{
putchar(ch);
}
printf("EOF\n");
while (1)
{}
return 0;
}Output :

Buffered input : After the user enters characters , Characters are stored in a buffer , Until the user presses Enter key , Will print the characters in the buffer .
while loop , Enter a string , Determine whether this string is EOF character , If not , perform putchar(), The characters entered by the user are stored in the buffer , The user did not press
First, enter a character , Determine whether this character is EOF character , If it is not , execute putchar(), At this time, the characters entered by the user are stored in the buffer , But I didn't press Enter key , Then even if the program is executed to putchar(), This character will not be printed , Because there is no flush buffer at all , When the user presses Enter After the key , Computer flush buffer , Print the contents of this buffer .
String function strlen
strlen strcmp strcpy strcat strchr strstr
#include <string.h>
strlen
- size_t strlen(const char*s);
- return s The character length of ( Not including the ending 0)
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
using namespace std;
int mylen(const char* c_str);
int main(int argc, const char *argv[])
{
printf(" String function strlen--------------\n");
char word[] = "Hello good good study,day day up!";
printf("word strlen: %lu\n", strlen(word));
printf("word mylen: %lu\n", mylen(word));
printf("word sizeof: %lu\n", sizeof(word));
while (1)
{}
return 0;
}
int mylen(const char* c_str)
{
int cnt = 0;
while (*c_str++ != '\0')
{
cnt++;
}
return cnt;
}
String function strcmp
strcmp
- int strcmp(const char *s1,const char*s2)
- Compare two strings , Return value :
- 0:s1 == s2
- 1:s1 > s2
- -1:s1 < s2
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
using namespace std;
int mycmp(const char* s1, const char* s2);
int main(int argc, const char *argv[])
{
printf(" String function strcmp--------------\n");
char s1[] = "Abc";
char s2[] = "ab";
printf("strcmp: %d\n",strcmp(s1,s2));
printf("mycmp: %d\n", mycmp(s1, s2));
while (1)
{}
return 0;
}
int mycmp(const char* s1, const char* s2)
{
int ret = 0;
while (*s1 ==*s2 && *s1 != '\0')
{
s1++;
s2++;
}
if (*s1 - *s2 > 0)
{
ret = 1;
}
else if (*s1 - *s2 < 0)
{
ret = -1;
}
return ret;
}
// The first strcmp The source code of
int __cdecl strcmp (
const char * src,
const char * dst
)
{
int ret = 0 ;
while((ret = *(unsigned char *)src - *(unsigned char *)dst) == 0 && *dst)
++src, ++dst;
if ( ret < 0 )
ret = -1 ;
else if ( ret > 0 )
ret = 1 ;
return( ret );
}
String function strcpy
strcpy
- char *strcpy(char*dst,char*src)
- src Copy the string to dst, return dst
Copy a string
- char *dst = (char*)malloc(strlen(src)+1) ;
- strcpy(dst,src);
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string.h>
using namespace std;
char *mycpy(char *dst, const char*src);
int main(int argc, const char *argv[])
{
printf("\n");
printf(" String function strcpy--------------\n");
char cpy1[] = "";
char cpy2[] = "abcd------";
char cpy3[] = "";
char cpy4[] = "good good study,day day up";
strcpy(cpy1, cpy2);
mycpy(cpy3, cpy4);
printf("strcpy %s\n", cpy1);
printf("mycpy %s\n", cpy3);
while (1)
{}
return 0;
}
char *mycpy(char *dst, const char*src)
{
char *ret = dst;
while (*dst++ = *src++)
{
;
}
*dst = '\0';
return ret;
}Output :

Reference resources :B standing ->《C Introduction and advanced language Weng Kai 》 video
边栏推荐
- Circularly insert one excel column and the sum of multiple columns
- Introduction to system mode development of rouya wechat mall
- QT knowledge: QT widgets widget class [01]
- Integrated monitoring solution for power environment of small and medium-sized computer rooms
- Go -- monitor file changes
- Foreign brands become a thing of the past? What are the key words of the TV industry in 2022?
- 检测当前系统语言
- Niuke.com: sum of three numbers
- 作用域和作用域链
- 一致性哈希的简单认识
猜你喜欢

How do testers plan for their future? To achieve 25K in 2 years?

阿里前辈给我推荐的软件测试人员必读书籍(附电子书),让我受益匪浅...

QT pro file configuration ffmpeg macro

EU officially released the data act, Ukraine was attacked by DDoS again, kitchen appliance giant Meiya was attacked, internal data leakage network security weekly

Solve the cvxpy error the solver GLPK_ MI is not installed

Lake shore PT-100 platinum resistance temperature sensor
![[live streaming] understand the design of d3js and learn how to read the source code.](/img/31/53e2d267acda0f87bcef081f2ebc8b.jpg)
[live streaming] understand the design of d3js and learn how to read the source code.

Data visualization - Calendar chart

QT pro文件配置ffmpeg宏

A Zhu and Xu Baobao's high-rise game - difference
随机推荐
大小端转换
Algorinote_2_主定理与 Akra-Bazzi 定理
初步了解认识正则表达式(Regex)
EDI 855 purchase order confirmation
new做了哪几件事
QT pro文件配置ffmpeg宏
Data visualization - biaxial comparison effect
How to download putty using alicloud image?
HR SaaS unicorn is about to emerge. Will the employee experience be the next explosive point?
【无标题】
Understanding of functions
Is foreign exchange speculation formal and is the fund safe?
Lightroom 大使系列:用 Meg Loeks 捕捉怀旧之情
Do we media video, and share the necessary app for friendly new media operation
The required books for software testers (with e-books) recommended by senior Ali have benefited me a lot
test
New product release Junda intelligent integrated environmental monitoring terminal
Illustrator tutorial, how to recolor artwork in illustrator?
Troubleshooting of service port failure
Preliminary understanding of regular expressions (regex)