Using character arrays and strings

1. Character array

Definition and initialization

A character array is an array whose elements are of data type character. For example: char c[6],char[3][4];

The initialization of the character array can be assigned character by character, and can also be assigned with a string constant.

For example, char ch[5]= {'b','o','y'} ;= {"boy"} ;= "boy" ; If it is not enough, it will be automatically assigned \0. There must be \0 when assigning a value with " ", otherwise it will not work.

char ch[5]= {'b','o','y','\0'} ; add '\0' to indicate the end of the character array.

The ASCII code value of the character '\0' is 0, so it is the same, and it also has the same meaning as NULL.

Two, the string

There is no string variable in C language, and strings are processed with character arrays

End of string: '\0'

char fruit[ ][7]={"apple",orange","banana"};

1. Definition and printing method of two-dimensional array 1

#include <stdio.h>             //二维数组的定义与打印方法一
int main(int argc, const char *argv[])
{
char fruit[][20]={"appLe", "orange","watermelon"};
int m, n,i,j;
n=sizeof(fruit)/sizeof(fruit[0]);//整个二维数组所占用的空间/每一行所占用的空间=行数
m=sizeof(fruit[0])/sizeof(char );//每一行所占用的空间/任何一个元素所占用的空间=列数
for (i=0;i<n;i++ ){
for (j=0;j<m;j++ ){
putchar(fruit[i][j]);
}
putchar('\n');
}
return 0;

2. Definition and printing method of two-dimensional array II

#include <stdio.h>
int main(int argc, const char *argv[] )
{                                           //二维数组的定义与打印方法二          
char fruit[ ][20]={"apple","orange","watermelon"};
int n, i;
n=sizeof(fruit)/sizeof(fruit[0]);
for (i=0;i<n;i++ ){
printf( "%s\n",fruit[i]);
putchar('\n');
}
return 0;
打印结果为:apple

          orange
          
          watermelon

3. Exercise: output strings in reverse order

①The method that the character string cannot be input by itself

#include <stdio.h> 
int main(int argc, const char *argv[] )      //字符串不可自行输入的方法
char arr[ ]="welcome " ;
int n, i;
n=sizeof(arr )/sizeof(char );
for(i=n- 1:i>=0:i--)
putchar(arr[i]);
putchar('\n');
return 0;
}

② String inverse output method 1: The output is in reverse order

#include <stdio.h>                      //字符串逆输出方法一:输出是逆序
#include <string.h> 
int main(int argc, const char *argv[ ] )
char arr[20];
int n, i;
printf("please input a string:\n");
gets(arr);
n=strlen(arr );
for(i=n-1;i>=0;i--)
putchar(arr[i]);
putchar('\n');
return 0;
}

③ String inverse output method 2: Reverse sequence after input

#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[ ])
{
char arr[20];               //字符串逆输出方法二:输入完逆序
int n,
i, j,temp;
pr intf("please input a string:\n");
gets(arr);
n=strlen(arr);
i=0,j=n-1;
while(i<j){
temp=arr[i];
arr[i]=arr[j];
arr [ j ]=temp ;
i++ ;
j-- ;
}
puts(arr);
putchar('\n' );
return 0;
}

Three, strlen, strcy, strcat, strcmp functions of string functions

header file #include <string.h>

① The function strlen to find the length of the string

The length does not include '\0'

How to use:

char s[ ]={'1','2'}

int n;

n=strlen(s);

\ 69: represents the octal number 69

\x 59: Indicates the hexadecimal number 69

②String copy function strcpy

Format: strcpy(char array 1, string 2)

Function: copy string 2 to character array 1

Return value: return the first address of character array 1

Explanation: The character array 1 must be large enough, and '\0' is copied together when copying

#include <stdio.h>
#include<string. h>
int main(int argc, const char *argv[ ] )
{
char a[ ]="lalalala";
char b[20];
strcpy(b,a);
puts(a);
puts(b);
return 0;
}

③ String connection function strcat

Format: strcat(character array 1, character array 2)

Function: connect character array 2 to the back of character array 1

Return value: return the first address of character array 1

Explanation: The character array 1 must be large enough. Before connection, both strings end with '\0'; after connection, '\0' in string 1 is canceled, and '\0' is added at the end of the new string

#include <stdio.h>
#include <string.h>
int main(int argc, const char *argv[ ] )
char a[100 ]= "yingyang
char b[ ]= "kuaixian" ;
strcat(a,b);
puts(a);
puts(b);
return 0;
}

④String comparison function strcmp

Format: strcmp(string1, string2);

Comparison rules: compare the two strings one by one from left to right (ASCII code), until a different character '\0' is encountered.

Return value: returns an integer of type int

a. If string 1<string 2, return negative integer -1

b. If string 1>string 2, return a positive integer 1

c. If string 1 == string 2, return 0

#include <stdio.h>
#include <string.h> 
int main(int argc, const char *argv[] )
{
char a[ ]="abc";
char bi ]="ab"
pr intf(”%d\n",strcmp(a,b));
return 0;
}

Fourth, the extended usage of string functions

①strncpy(p,p1,n) copy the specified length string: directly replace the n characters of p

#include <stdi0.h>
#include <string.h>
#define N 30
int main(int argc, char *argv[])
char src[] ="makeru" ;
char dest[N] = ".com.cn ;
str@cpy(dest,src,4);
puts(src);
puts (dest) ; 
return 0;
}
结果为:makeru
       make.cn

②strncat(p,p1,n) appends a string of specified length

#include <std10. h>
#include <string.h>
#define N 30
int main(int argc, char *argv[])
{
char src[] ="makeru" ;
char dest[N] =".com.cn" ;
strncat(dest, src, 4);
puts(src);
puts(dest) ;
return 0;
结果为:  makeru
         .com.cnmake

③3strcasecmp ignores case comparison strings

④strncmp(p,p1,n) compares strings of specified length

#include <std10. h>
#include <string .h>
#define N 38
int main(int argc, char *argv[])
{
char s1[] =
QUIT ;
char s2[] =
quit" ;
printf( "%d\n", strncmp(s1, s2, 4));
printf( "%d\n",strcasecmp(sl, s2));
return 0;
}

⑤strchr(p,c) finds the specified character in the string

#include <stdio. h> 
#include <string.h>
int main(int argc, const char *argv[] )
char a[]=" cbusnjanjzhy";
int ch;
ch='z' ;
printf("%p %p \n" ,a,strchr(a,ch)); //字符串起始位置和查找到的位置一比较即可得出
return 0;
}

⑥strstr(p,p1) find string

#include <stdio. h>
#include <string.h> 
{
int main(int argc, char *argv[])
char s[] = "how are you" ;
char subs[] = "are ;
0
printf( "%d\n", strstr(s, subs)-s);   //查找到的位置减去字符串起始位置即可
return 0;

⑦isalpha() checks whether it is an alphabetic character

⑧isupper() checks whether it is an uppercase alphabetic character

⑨islower() checks whether it is a lowercase alphabetic character

⑩isdigit() checks whether it is a number

#include <stdio h> 
#include <ctype.h>
int main(int argc, char *argv[])
{
int ch;
while ((ch = getchar()) != EOF) {    //判断不是特殊输入ctry+D
if (isalpha(ch)) {        //判断是不是字母
if (isupper(ch))          //判断字母是不是大写
printf("Upper:%c\n", ch);
if (islower(ch))           //判断字母是不是小写 
printf(" Lower:%c\n" , ch);
}
if (isdigit(ch))             //判断是不是数字
printf("Digit:%d %c\n", ch-'θ', ch);   //字符转换成数字
putchar(ch);
}
return 0;
}
#include <stdio. h> 
#include <ctype . h>
int main(int argc, char *argv[])
int ch;
while ((ch = getchar()) != EOF) {
if (isalpha(ch)) {  //判断是不是字母
if (isupper(ch)) { 
ch = tolower(ch);   //是大写转为小写
}
else {
ch = toupper(ch);   //是小写转为大写
}
}
printf( "%c\n", ch);
}
return 0;
}

Guess you like

Origin blog.csdn.net/qq_52049228/article/details/129632427