C language Zhejiang University version "C language programming (3rd edition)" question set exercises 8-8 moving letters (10 points)

 

This question requires writing a function to move the first 3 characters of the input string to the end.

Function interface definition:

void Shift( char s[] );

Among them char s[]is the string passed in by the user, and the length of the title is guaranteed to be no less than 3; the function Shiftmust still exist s[]in the string after the transformation according to the requirements .

Sample referee test procedure:

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

#define MAXS 10

void Shift( char s[] );

void GetString( char s[] ); /* 实现细节在此不表 */

int main()
{
    char s[MAXS];

    GetString(s);
    Shift(s);
    printf("%s\n", s);
	
    return 0; 
}

/* 你的代码将被嵌在这里 */

Input sample:

abcdef

Sample output:

defabc

//1. If you have any questions, please leave a message to point out, thanks! ! !

//2. If you have a better idea, you are also welcome to leave a message, thank you! ! !

void Shift( char s[])
{int i,j,k,n;
    n=strlen(s);// strlen() is used to calculate the length of the specified string s, excluding the end character "\0".
    for(i=0;i<3;i++)//Move the first 3 characters of the input string to the end, that is, loop three times
    {k=s[0];//Take the first element out
        for( j=1;j<n;j++)//From the second one, assign the previous value
            s[j-1]=s[j];
        s[n-1]=k;//The number will be taken out Move to the end
    }
}

 

Guess you like

Origin blog.csdn.net/L_Z_jay/article/details/106165656