task1-1 include stdio.h#define N 4int main(){int x[N] = {1, 9, 8, 4};int i;int *p;for(i=0; iN; ++i) printf("%d", x[i]);printf("\n");for(p=x; px+N; ++p) printf("%d", *p);printf("\n");p = x;for(i=0; iN; ++i) printf("%d", *(p+i));printf("\n");
include <stdio.h>
#define N 4
int main()
{
int x[N] = {1, 9, 8, 4};
int i;
int *p;
for(i=0; i<N; ++i)
printf("%d", x[i]);
printf("\n");
for(p=x; p<x+N; ++p)
printf("%d", *p);
printf("\n");
p = x;
for(i=0; i<N; ++i)
printf("%d", *(p+i));
printf("\n");
p = x;
for(i=0; i<N; ++i)
printf("%d", p[i]);
printf("\n");
return 0;
}
task1-2
#include <stdio.h>
#define N 4
int main()
{
char x[N] = {'1', '9', '8', '4'};
int i;
char *p;
for(i=0; i<N; ++i)
printf("%c", x[i]);
printf("\n");
for(p=x; p<x+N; ++p)
printf("%c", *p);
printf("\n");
p = x;
for(i=0; i<N; ++i)
printf("%c", *(p+i));
printf("\n");
p = x;
for(i=0; i<N; ++i)
printf("%c", p[i]);
printf("\n");
return 0;
}
1、2004
2、2001
3、字符类型不同,变量存储空间大小不同.
#include <stdio.h>
int main()
{
int x[2][4] = { {1,9,8,4}, {2,0,2,2}} ;
int i, j;
int *p;
int (*q)[4];
for(i=0; i<2; ++i)
{
for(j=0; j<4; ++j)
printf("%d", x[i][j]);
printf("\n");
}
for(p = &x[0][0], i = 0; p < &x[0][0] + 8; ++p, ++i)
{
printf("%d", *p);
if( (i+1)%4 == 0)
printf("\n");
}
for(q=x; q<x+2; ++q)
{
for(j=0; j<4; ++j)
printf("%d", *(*q+j));
printf("\n");
}
return 0;
}
task2-2
#include <stdio.h>
int main()
{
char x[2][4] = { {'1', '9', '8', '4'}, {'2', '0', '2', '2'} };
int i, j;
char *p;
char (*q)[4];
for(i=0; i<2; ++i)
{
for(j=0; j<4; ++j)
printf("%c", x[i][j]);
printf("\n");
}
for(p = &x[0][0], i = 0; p < &x[0][0] + 8; ++p, ++i)
{
printf("%c", *p);
if( (i+1)%4 == 0)
printf("\n");
}
for(q=x; q<x+2; ++q)
{
for(j=0; j<4; ++j)
printf("%c", *(*q+j));
printf("\n");
}
return 0;
}
1、2004.
2、2017.
程序task1_2.c
1、2001.
2、2005.
都是对指针变量做++操作,为什么结果有差别?
答:字符类型不同,变量存储空间大小不同.
#include <stdio.h>
#include <string.h>
#define N 80
int main()
{
char s1[] = "C, I love u.";
char s2[] = "C, I hate u.";
char tmp[N];
printf("sizeof(s1) vs. strlen(s1): \n");
printf("sizeof(s1) = %d\n", sizeof(s1));
printf("strlen(s1) = %d\n", strlen(s1));
printf("\nbefore swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
printf("\nswapping...\n");
strcpy(tmp, s1);
strcpy(s1, s2);
strcpy(s2, tmp);
printf("\nafter swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
return 0;
}
问题1:数组s1的大小是多少?sizeof(s1)计算的是什么?strlen(s1)统计的是什么?
sizeof(s1)计算的是字符数组的长度,strlen(s1)计算的字符数组的有效长度
问题2:line7代码,能否替换成以下写法?
不能
问题3:line20-22执行后,字符数组s1和s2中的内容是否交换?
是
task3-2#include <stdio.h>
#include <string.h>
#define N 80
int main()
{
char *s1 = "C, I love u.";
char *s2 = "C, I hate u.";
char *tmp;
printf("sizeof(s1) vs. strlen(s1): \n");
printf("sizeof(s1) = %d\n", sizeof(s1));
printf("strlen(s1) = %d\n", strlen(s1));
printf("\nbefore swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
printf("\nswapping...\n");
tmp = s1;
s1 = s2;
s2 = tmp;
printf("\nafter swap: \n");
printf("s1: %s\n", s1);
printf("s2: %s\n", s2);
return 0;
}
问题1:指针变量s1中存放的是什么?sizeof(s1)计算的是什么?strlen(s1)统计的是什么?
sizeof(s1)计算的是地址变量的大小;strlen(s1)统计的是s1字符数组有效长度;
问题2:line7代码能否替换成下面的写法?
可以
问题3:line20-line22,交换的是什么?字符串常量"C, I love u."和字符串常量"C, I hate u."在内存 存储单元中有没有交换?
没有
task4#include <stdio.h>
#include <string.h>
#define N 5
int check_id(char *str);
int main()
{
char *pid[N] = {"31010120000721656X",
"330106199609203301",
"53010220051126571",
"510104199211197977",
"53010220051126133Y"};
int i;
for(i=0; i<N; ++i)
if( check_id(pid[i]) )
printf("%s\tTrue\n", pid[i]);
else
printf("%s\tFalse\n", pid[i]);
return 0;
}
int check_id(char *str)
{
int i,flag;
for(i=0;i<=17;i++)
{
if(str[i]>='0'&&str[i]<='9')
flag=1;
else
flag=0;
}
if(str[17]=='X')
flag=1;
if(strlen(str)==18&&flag==1)
return 1;
else
return 0;
}
#include <stdio.h>
#include <string.h>
#define N 80
int is_palindrome(char *s);
int main()
{
char str[N];
int flag;
printf("Enter a string:\n");
gets(str);
flag = is_palindrome(str);
if (flag)
printf("YES\n");
else
printf("NO\n");
return 0;
}
int is_palindrome(char *s)
{
int i=0,j;
j=strlen(s)-1;
while(i<=j&&s[i]==s[j])
{
i++;
j--;
}
if(i>=j)
return 1;
else
return 0;
}
task6
#include <stdio.h>
#define N 80
void encoder(char *s);
void decoder(char *s);
int main()
{
char words[N];
printf("输入英文文本: ");
gets(words);
printf("编码后的英文文本: ");
encoder(words);
printf("%s\n", words);
printf("对编码后的英文文本解码: ");
decoder(words);
printf("%s\n", words);
return 0;
}
void encoder(char *s)
{
int i;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='a'&&s[i]<='z')
{
if(s[i]!='z')
s[i]=s[i]+1;
else
s[i]='a';
}
if(s[i]>='A'&&s[i]<='Z')
{
if(s[i]!='Z')
s[i]=s[i]+1;
else
s[i]='A';
}
}
}
void decoder(char *s)
{
int i;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='a'&&s[i]<='z')
{
if(s[i]!='a')
s[i]=s[i]-1;
else
s[i]='z';
}
if(s[i]>='A'&&s[i]<='Z')
{
if(s[i]!='A')
s[i]=s[i]-1;
else
s[i]='Z';
}
}
}