当前位置:网站首页>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 :
  1. 0:s1 == s2
  2. 1:s1  > s2
  3. -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

 

原网站

版权声明
本文为[Maccy37]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206122043274768.html