0117521-  
#include <stdio.h>
#include <stdlib.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"); system("pause"); }

 

 

#include <stdio.h>
#include <stdlib.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");
        system("pause");
    return 0;
}

2004

2001

int型指针变量占四个字节的内存单元

char型指针变量占一个字节的内存单元

task2

#include <stdio.h>
#include <stdlib.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");
    }


    system("pause");
}

#include <stdio.h>
#include <stdlib.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");
    }
    

    system("pause");
}

 

2004  2016

2001   2004

 

p指向的是二维数组元素的指针变量

q是指向一维数组的指针变量

task3

#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);
    
    system("pause");
}

 

#include <stdio.h>
#include <string.h>
#include <stdlib.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);

    system("pause");



}

 

 

1.s1大小是12

strlen测试的是实际长度,sizeof 统计的是字节数

2.不能

3交换了

 1.s1存放的是字符串,sizeof统计char型指针变量占的字节数,strlen计算的是实际长度

2可以

3.交换的是指针的指向,没有

task4

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int check_id(char *str);

#define N 5
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]);    





    system("pause");

}

int check_id(char *str)
{
    int i=0,j,flag=1;
    int n=strlen(str);
    if(n!=18)
        flag=0;
    else
        {for(i=0;i<17;i++)
        {
            if(str[i]>='0'&&str[i]<='9')
        
                i++;
        
        }

    if(i=16)
    {
        if(str[17]=='X')
    flag=1;
        else if(str[17]>='0'&&str[17]<='9')
    flag=1;
        else flag=0;
    }

    }

    return flag;
}

 

task5



#include <stdio.h>
#include
<string.h> #include <stdlib.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"); system("pause"); } int is_palindrome(char *s) { int i,flag=1; int n=strlen(s)-1; for(i=0;i<n/2;i++) { if(s[i]!=s[n-i]) { flag=0; break; } } return flag; }

 task6

#include <stdio.h>
#include <stdlib.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);


system("pause");
}
void encoder(char*s)
{
    int i=0,n;
    while(s[i])
    {    i++;}
    n=i;
    for(i=0;i<n;i++)
    {
        if((s[i]>='a'&&s[i]<='y')||(s[i]>='A'&&s[i]<='Y'))
    
            s[i]=s[i]+1;
    

        else if(s[i]=='z')
            s[i]='a';
        else if(s[i]=='Z')
            s[i]='A';
    
    
    }}
    void decoder(char*s)
{
    int i=0,n;
    while(s[i])
    {    i++;}
    n=i;
    for(i=0;i<n;i++)
    {
        if((s[i]>'a'&&s[i]<='z')||(s[i]>'A'&&s[i]<='Z'))
    
            s[i]=s[i]-1;
    

        else if(s[i]=='a')
            s[i]='z';
        else if(s[i]=='A')
            s[i]='Z';
    
    
    }




}

 

posted on 2022-06-07 17:39  董璇  阅读(75)  评论(3编辑  收藏  举报