Basic knowledge:
1. The reading rules of scanf have "default read to the end of the space"
Solution: scanf("% [^\n] ", arr); Function: " read to end of newline "
2. Commonly used gets and puts for strings
gets() reads a string including spaces
put(x) is equivalent to printf(" %s \n ", x) will have one more newline than normal printf
3. The difference between character arrays and strings (with or without \0)
例1:char s1[3]={'a','b','c'},s2[4]={'a','b','c','0'};
Both s1 and s2 are character arrays, but s2 is also a string .
4. Initialization rules:
char str[1000]=''abdce'' --------Add \0 after default (namely string )
5. Two ways of printing character arrays (1. traversing %c 2. Printing %s directly)
example:
#include<stdio.h>
int main()
{
char arr1[4], arr2[5];
scanf("%s", arr1);//&arr1[]
//方式1(一个一个出来)
int i = 0;
for (i = 0; i < 4; i++)
{
printf("%c", arr1[i]);
}
printf("\n");
//方式2(一整个出来)
printf("%s", arr1);
}
Output result:
———————————————————————————————————————————
Topic: Reverse the contents of a string str
#include<stdio.h>
#include<string.h>
int main()
{
char arr1[10000] = { 0 };
scanf("%[^\n]", arr1);
int len = strlen(arr1);
int left = 0;
int right = len - 1;
while (left < right)
{
char tmp = arr1[left];
arr1[left] = arr1[right];
arr1[right] = tmp;
left++;
right--;
}
printf("%s\n", arr1);
return 0;
}