1.
#include<stdio.h>
int main()
{
char b[] = {'R','O','H','E','\0','O','O','\0'};
char a[3] = {'H','E','\0'}; //Remember to add '\0' as the end of a string
printf("The string a is %s\n",a);
puts(a); //The function of "puts" could only output a string at one time.
printf("\nThe string b is %s\n",b);
puts(b);
printf("\nThe address of a is %p\n",a);
printf("The address of b is %p\n",b);
return 0;
}
Output:
The string a is HE
HE
The string b is ROHE
ROHE
The address of a is 0x7fffd5b54410
The address of b is 0x7fffd5b54420
2.
// 字符和字符串常用处理函数
#include<stdio.h>
int main()
{
//字符输出函数
putchar(65); //It will output "A" here, rather than "65"
printf("\n");
char d = 65;
putchar(d); //It will also output "A" here
printf("\nd is %c\n",d); //单个字符的输出格式时%c
putchar('l'); //putchar括号里面的字符时单引号,一次只能输出一个字符;printf括号里面时双引号,一次能输出多个字符
printf("\n");
//字符输入函数
char c;
printf("Please input a character:\n");
c = getchar(); //getchar一次只能读入一个字符,所以如果输入65则,只会显示6.
putchar(c);
printf("\n");
int size = strlen("jin");
char s1[] = "jin";
int size1 = strlen(s1);
char s2[] = {'j','i','n','\0','l','o','n','g','\0'};
int size2 = strlen(s2); //Calculate the length of string s2
printf("The size of s1 is %d and s2 is %d\n",size1,size2);
char s3[10];
strcpy(s3,"jinlong");
puts(s3); //Copy string "jinlong" to to s3
char s4[20];
char s5[] = {'j','i','n','\0','l','o','n','g','\0'};
strcpy(s4,s5); //Copy string s5 to s4
puts(s4);
char s6[20] = "Jin";
strcat(s6, "long"); //Connect string s6 with string "long"
puts(s6);
char s7[] = "abc";
char s8[] = "abc";
char s9[] = "Abc";
char s10[] = "abd";
printf("%d, %d, %d\n",strcmp(s7,s8),strcmp(s7,s9),strcmp(s9,s10)); //Compare two strings and return the ASCII difference value.
return 0;
}
A
A
d is A
l
Please input a character:
65
6
The size of s1 is 3 and s2 is 3
jinlong
jin
Jinlong
0, 32, -32
3.
//The initialization of strings and strings array
#include<stdio.h>
int main()
{
char a[4] = {'j','i','n','\0'} ;
puts(a); //puts会从a的首地址开始输出字符,知道\0字符为止;puts一次只能输出一个字符串,输出后自动换行;
putchar('jinl');//In this case, it will output the last character 'l';
printf("\n");
char b[4];
b[0] = 'j'; // Here is b[0] = 'j' rather than b[0] = j
b[1] = 'i';
b[2] = 'n';
b[3] = '\0';
printf("Sring b is %s\n",b);
char c[] = "jin";
printf("Sring c is %s\n",c);
char d[10] = "jin";
printf("Sring d is %s\n",d);
char name1[2][5] = {{'j','i','n','\0'}, {'l','o','n','g','\0'}};
char name2[2][5] = {"jin","long"};
char name3[2][5] = {{"jin"},{"long"}};
putchar(name1[1][2]);
printf("\n");
printf("name1 is %s\n",name1);//When "printf" funtion encounters '\0', it will stop printing, so the name1 would be "jin", rather than "jinlong".
int i;
printf("name1 is:");
for(i = 0; i < 2; i++)
{
printf("%s\n",name1[i]);
}
printf("\n");
printf("name2 is:");
for(i = 0; i < 2; i++)
{
printf("%s\n",name2[i]);
}
printf("\n");
printf("name3 is:");
for(i = 0; i < 2; i++)
{
printf("%s\n",name3[i]);
}
printf("\n");
return 0;
}
Output:
jin
l
Sring b is jin
Sring c is jin
Sring d is jin
n
name1 is jin
name1 is:jin
long
name2 is:jin
long
name3 is:jin
long