[C/PTA - 12. Pointer 1 (in-class practice)]

6-1 Swapping the values ​​of two integers

Insert image description here

void fun(int* a, int* b)
{
    
    
	int* tmp = *a;
	*a = *b;
	*b = tmp;
}

6-2 Use pointers to find the maximum value

Insert image description here

void findmax(int* px, int* py, int* pmax)
{
    
    
    *pmax = *px > *py ? *px : *py;
}

6-3 String concatenation

Insert image description here

char* str_cat(char* s, char* t)
{
    
    
    strcat(s, t);
    return s;
}

6-4 Move letters

Insert image description here

void Shift(char s[])
{
    
    
    char ch[3];
    int count = 0;
    for (int i = 0; i < 3; i++)//存储前三个字符
    {
    
    
        ch[i] = s[i];
    }

    for (int i = 0; i < strlen(s) - 3; i++)//将后面的字符往前挪动覆盖
    {
    
    
        s[i] = s[i+3];
        count++;
    }

    for (int i = count,j=0; i < MAXS,j<3; i++,j++)//将存储的前三个字符存储到s数组的后面
    {
    
    
        s[i] = ch[j];
    }
}

Guess you like

Origin blog.csdn.net/qq_73900397/article/details/134644292