实验六

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

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

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

                  2.2001;2004;

                  3.因为首先,整型占4个字节,字符型占1个;并且q与p不同,q指向的是一维数组,进行++操作后,跳过的是一整个数组,而不是跳过一个元素。

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


}

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


}

回答问题:实验3.1:1.大小是13;计算的是数组s1的大小;统计的是数组s1中的有效字符;

                                2.不能;

                                3.交换了。

                   实验3.2:1.存放的是字符串的首地址;计算的是地址变量s1所占的字节数;统计的是所有有效字符的个数;

                                 2.可以;

                                 3.交换的是地址;在内存存储单元中没有交换。

#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;
    if (strlen(str) != 18)
        return 0;
    for (i = 0; i < 18; i++)
    {
        if ((str[i] >= '0' && str[i] <= '9') || str[i] == 'X')
            continue;
        else
            return 0;
    }
    return 1;
}

#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_s(str);
    flag = is_palindrome(str);
    if (flag)
        printf("YES\n");
    else
        printf("NO\n");
    return 0;
}
int is_palindrome(char* s)
{
    int i,j;
    j = strlen(s);
    for (i = 0;s[i]!='\0'; i++)
    {
        if (s[i] == s[j - i-1])
            continue;
        else
            return 0;
    }
    return 1;
}

#include<stdio.h>
#define N 80
void encoder(char* s);
void decoder(char* s);
int main()
{
    char words[N];
    printf("输入英文文本: ");
    gets_s(words);

    printf("编码后的英文文本: ");
    encoder(words);
    printf("%s\n", words);

    printf("对编码后的英文文本解码: ");
    decoder(words);
    printf("%s\n", words);

    return 0;
}
void encoder(char* s)
{
    int i,j=0;
    for (i = 0; s[i] != '\0'; i++)
        j++;
    for (i = 0; i < j; i++)
    {
        if ((s[i] >= 'a' && s[i] < 'z') || (s[i] >= 'A' && s[i] < 'Z'))
            s[i] = s[i] + 1;
        else if ((s[i] == 'z') || (s[i] == 'Z'))
            s[i] = s[i] - 25;
    }
}
void decoder(char* s)
{
    int i, j = 0;
    for (i = 0; s[i] != '\0'; i++)
        j++;
    for (i = 0; i < j; 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] == 'A'))
            s[i] = s[i] + 25;
    }
}

 

posted @ 2022-06-07 18:54  唐卉宁  阅读(17)  评论(3编辑  收藏  举报