0:
#include <stdio.h>
#define MAX 1024
int main()
{
char str[MAX] = {};
char *p = str;
int i, sum = 0;
printf("请输入一个字符串:");
fgets(str, MAX, stdin); // fgets会将换行符当作合法字符读取
for (i = 0; i < MAX; i++){
if(*(p+i) != '\n'){
sum++;
} else {
break;
}
}
printf("您一共输入了%d个字符\n",sum);
return 0;
}
1:
#include <stdio.h>
#define MAX 1024
int main()
{
char str1[MAX] = {};
char str2[MAX] = {};
char *p = str1;
int i, sum = 0;
int num;
char choose;
printf("请输入一个字符串到str1中:");
fgets(str1, MAX, stdin); // fgets会将换行符当作合法字符读取
printf("是否使用进阶版?(y/n):");
scanf("%c",&choose);
if (choose == 'n'){
for (i = 0; i < MAX; i++){
if(*(p+i) != '\n'){
str2[i] = *(p+i);
} else {
str2[i] = '\0'; //结束符也要赋值
break;
}
}
} else {
printf("请输入需要拷贝的字符个数:");
scanf("%d",&num);
for (i = 0; i < num; i++){
if(*(p+i) != '\n'){
str2[i] = *(p+i);
} else {
str2[i] = *(p+i);
break;
}
}
}
printf("开始拷贝str1的内容到str2中...\n");
printf("拷贝完毕!\n");
printf("现在,str2中的内容是:%s\n",str2);
return 0;
}
2:
#include <stdio.h>
#define MAX 1024
int main()
{
char str1[MAX] = {};
char str2[MAX] = {};
char *p = str1;
int i, sum = 0;
int num;
printf("请输入第一个字符串:");
fgets(str1, MAX, stdin); // fgets会将换行符当作合法字符读取
printf("请输入第二个字符串:");
fgets(str2, MAX, stdin);
for (i = 0; i < MAX; i++){
if(*(p+i) == '\n'){
printf("请输入需要连接的字符个数:");
scanf("%d",&num);
for (int j = 0; j < num; j++){
*(p+i+j) = str2[j];
if (str2[j] == '\n'){
goto FINDIT;
}
}
}
}
FINDIT:
printf("连接后的结果是:%s\n",str1);
return 0;
}
3:
#include <stdio.h>
#define MAX 1024
int main()
{
char str1[MAX] = {};
char str2[MAX] = {};
char *p = str1;
int i, sum = 1;
int num;
printf("请输入第一个字符串:");
fgets(str1, MAX, stdin); // fgets会将换行符当作合法字符读取
printf("请输入第二个字符串:");
fgets(str2, MAX, stdin);
printf("请输入要比对的字符个数:");
scanf("%d",&num);
for (i = 0; i < num; i++){
if (str1[i] == str2[i]){
sum++;
// if (str1[i] == '\n' && str2[i]== '\n'){
// printf("两个字符串完全一致\n");
// }
} else {
printf("两个字符串不完全相同,第%d个字符出现不同\n",sum);
goto FINDIT;
// break;
}
}
printf("两个字符串完全一致\n");
FINDIT:
return 0;
}