C language (string and printf)

Posted by Inkyskin on Sat, 30 Oct 2021 14:42:32 +0200

character string:

strcat

  • char *strcat(char *restrict s1,const char *restrict s2);
  • restrict indicates that src and dst do not overlap (c99)
  • Copy s2 to the back of s1 (from back to front), connect it into a long string, and return s1
  • s1 there must be enough space
  • Both strcpy and strcat may have security problems: the destination may not have enough space, so try not to use it
  • Security version:
    char *strncpy(char *restrict dst,const char *restrict src size_t n);
    //Copy a limited number n of strings to dst 
    char *strncat(char *restrict s1,const char *restrict s2,size_t n);
    //Copy a limited number of n strings after s1 
    int strncmp(const char *s1,const char*s2,size_t n);)
    //Compare the first n characters 

strchr

  • Find a character in the string. When a character is found, the character address is returned as the first address of the new pointer
  • char *strchr(const char *s,int c);       Start from the left of the string s
  • char *strrchr(const char *s,int c);     Start at the right of the string s
  • NULL is returned to indicate that it was not found
  • Find the character in the string and output the string before the character
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main(int argc,char const *argv[])
    {
    	char s[]="hello";
    	char *p=strchr(s,'l');//*The first address of p is the first l 
    	char c=*p;
    	*p='\0';//Change the content of * p pointing to the location and change l to \ 0
    	//At this time, the string in s is' he  he \ 0 LLO \ 0  llo he \ 0 LLO \ 0  ' 
    	char *t=(char*)malloc(strlen(s)+1);//Allocate memory 
    	strcpy(t,s);//Copy s to t, and stop copying when \ 0 is encountered during the copying process 
    	printf("%s\n",t);
    	free(t);
    	
    	return 0;
    } 
    
    he
    

strstr

  • Find string from string
  • char *strstr(const char *s1,const char *s2);
  • char *strcasestr(const char *s1,const char *s2);
  • Find the string from the string, ignoring case in the search process

The difference between double quotation marks and single quotation marks:

  • Different meanings.

A character caused by single quotation marks actually represents an integer, and the integer value corresponds to the sequence value of the character in the character set adopted by the compiler. Generally, our compiler adopts ASCII character set. Therefore, the meaning of's' is actually consistent with the meaning of decimal number 115.

The string caused by double quotation marks represents a pointer to the starting character of the nameless array.

  • Different sizes.

The size of a character caused by single quotation marks is one byte.

The string size caused by double quotation marks is the total size of characters + 1, because the string caused by double quotation marks will add a binary character '\ 0' at the end of the string.

printf("%_", i);

  • %a: Floating point numbers, hexadecimal digits, and p-notation (C99)  
  • %A: Floating point numbers, hexadecimal digits, and p-notation (C99)
  •  % c: One character char
  •  % C: One ISO wide character
  •  % d: Signed decimal integer int  
  • %ld,%Ld: long integer data  
  • %hd: short integer data *%e: floating point number, e-notation
  •  % E: Floating point number, e-notation
  •  % f: Single double precision floating point number (default float), decimal notation
  •  %. nf: here n means decimal notation, which is accurate to N decimal places
  •  % g: Automatically select% f or% e according to different values
  •  % G: Automatically select% f or% e according to different values
  •  % i: Signed decimal number (same as% d)
  •  % o: Unsigned octal integer *%p: pointer
  •  % s: Corresponding string char * (% s = =% HS = =% HS output narrow string)
  •  % S: Corresponding wide string WCAHR * (% WS = =% s output wide string)
  •  % u: Unsigned decimal integer unsigned int
  •  % x: Unsigned hexadecimal integer (form 2f)
  •  %# x: Unsigned hexadecimal integer (form 0x2f)
  •  % 10: Unsigned hexadecimal integer (form 2F)  
  • %#10: Unsigned hexadecimal integer (form 0x2F)
  •  %%: Print a percent sign
  •  % lld: used for INT64 or long
  •  % llu: used for UINT64 or unsigned long
  •  % llx: for 64 bit hexadecimal data

practice:

Use the pointer to find the maximum value of the array: input n (n < = 10) integers and store them in the array. Use the pointer to operate the array elements to find the maximum value and output it to the screen.

#include<stdio.h>

int main()
{
	int a[10]={0},n,i;
	int *max;
	
	printf("enter n:");
	scanf("%d",&n);//Confirm n integers 
	
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}//Enter integer 
	
	max=a;//Initialization pointer 
	for(i=0;i<n;i++){
		if(*max<a[i+1]){
			max=&a[i+1];
		}
	}//Find the maximum number of arrays 
	
	printf("%d",*max);//output 
	return 0;
}

Your program reads a line of text separated by spaces into several words, ending with. You have to output the length of each word. The words here have nothing to do with language and can include various symbols. For example, it's a word with a length of 4. Note that consecutive spaces may appear in the line; The last. Is not included.

  • Pay attention to the judgment format in the if statement
#include<stdio.h>
//Calculate word length 
int main()
{
	char a[100];
	int i=0,sum=0,c=0,d;
	int b[100]={0};
	
	while(scanf("%c",&a[i++])){	//input			
		if(a[i-1]=='.')//Encountered '.' exit loop 
			break;
	}
	
//Count word length	
	for(d=0;d<i;d++){
		sum++;//sum counts the length of a word 
//		if(a[d]==' '||'.');    The wrong format should be judged separately 
		if(a[d]==' '||a[d]=='.'){//Space or period encountered 
			b[c++]=sum-1;//Record the result of sum counting in the b array 
			sum=0;//Clear sum 
		}
	}	
	for(d=0;d<c;d++){//Print the number of b array records 
		printf("%d ",b[d]);
	}
	return 0;
}

Floating point number:

Floating point numbers cannot be directly judged by if(a==b) like shaping, because floating point numbers are imprecisely represented by float or double.

float is accurate to 6 decimal places

double is accurate to 15 decimal places

Therefore, in other words, if a float is less than 0.00000 1, we don't know whether it is valid or not. It can also be considered to be approximately 0.

Similarly, the effective range of a double is 1e-15. For double types less than 1e-15, we can think that it is approximately 0

  • Judge whether it is 0: fabs (variable) < 1e-6 by comparing the absolute value of floating-point number with 1e-6 or 1e-15
  • pow (variable, number) is required for the power of floating-point number
  • sqrt (variable) for square root
  • The header file is math.h