实验6

tesk1-1.c

#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");
    // 方式1:通过数组名和下标遍历输出数组元素 
    
    for(p=x; p<x+N; ++p)
        printf("%d", *p);
    printf("\n"); 
    // 方式2:通过指针变量遍历输出数组元素 (写法1)
    
    p = x;
    for(i=0; i<N; ++i)
        printf("%d", *(p+i));
    printf("\n");
    // 方式2:通过指针变量遍历输出数组元素(写法2)
    
    p = x;
    for(i=0; i<N; ++i)
        printf("%d", p[i]);
    printf("\n");
    // 方式2:通过指针变量遍历输出数组元素(写法3)    
    return 0;
}

答:2004

tesk1-2.c

#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;
}

答;2001

tesk2-1.c

#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;
}

答:2004

  2016

tesk2-2.c

#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;
}

答:2001

  2004

tesk3-1.c

#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;
}

答:

strlen计算字符串中的字符个数,而sizeof计算的事对象所占的内存空间

13

不可以替换

交换了

tesk3-2.c

#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;
}

答:

存放的是"C, I love u."的地址,sizeof计算的是s1所占的内存空间,strlen计算的是字符串的字符个数

可以替换

交换的是地址,没有交换

tesk4.c

#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)
{
     char *p,*temp;
    for(p=str;p<str+5;p++) 
    {
        if(strlen(p) != 18)
            {
               return 0;
               break;
            }           
        else
            {
                temp = p;
                for(;temp<p+18;temp++)
                {
                    if(*temp>='0'&&*temp<='9'||*temp == 'X');
                    else
                        {
                            return 0;
                            break;
                        }
                }
                if(temp == p+18)
                return 1;            
            }
    }

}

tesk5.c

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define N 80
 5 int is_palindrome(char *s);      
 6 
 7 int main()
 8 {
 9     char str[N];
10     int flag;
11 
12     printf("Enter a string:\n");
13     gets(str);
14 
15     flag = is_palindrome(str);   
16 
17     if (flag)
18         printf("YES\n");
19     else
20         printf("NO\n");
21 
22     return 0;
23 }
24 
25 
26 int is_palindrome(char *s)
27 {
28     int i=0,len,j;
29    len = strlen(s);
30    printf("字符串长度是: %d\n", len);
31    j = len/2;
32    for(;i<j;)
33    {
34            if(*(s+i) == *(s+len-i-1))
35                i++;
36            else
37            {
38               return 0;
39               break;
40            }
41         if(i==j)
42            return 1;
43    }       
44 }

tesk6.c

#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; 

}

/*函数定义 功能:对s指向的字符串进行编码处理 
编码规则: 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换 其它非字母字符,保持不变 */

void encoder(char *s) 
{ 
   for(;*s!='\0';s++)
   {
        if(*s == 'z'||*s == 'Z')
           *s = *s - 25;
        else if(*s >= 'a'&&*s < 'z'||*s >= 'A'&&*s < 'Z')
           *s = *s + 1;
   }
}

/*函数定义 功能:对s指向的字符串进行解码处理 
解码规则: 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换 其它非字母字符,保持不变 */

void decoder(char *s) 
{ 
   for(;*s!='\0';s++)
   {
           if(*s == 'a'||*s == 'A')
           *s = *s + 25;
        else if(*s > 'a'&&*s <= 'z'||*s > 'A'&&*s <= 'Z')
           *s = *s - 1;
   }
}

 

posted @ 2022-06-14 09:10  甜司康饼  阅读(4)  评论(2编辑  收藏  举报