计算机二级[英文]-C 字符串移动—东方仙盟

Use VC++2010 to open the solution in the candidate folder prog1. The project in this solution contains a source file prog1.c. Please write a function fun().

Function rule: Move the 1st ~ m-th characters to the end of the string. Move the characters from position m+1 to the end of the string to the front of the string.

For example: Original string: "ABCDEFGHIJK", \(m=3\). After moving, the result string is "DEFGHIJKABC".

Notice: Do not modify the code inside main() and other functions. Only write code inside the braces of function fun().

复制代码
#include <stdio.h>
#include <string.h>
#define N 80
void fun(char *w, int m)
{

}
void main()
{
    FILE *wf;
    char a[N] = "ABCDEFGHIJK", b[N] = "ABCDEFGHIJK";
    int m;
    printf("The original string :\n");
    puts(a);
    printf("\n\nEnter m: ");
    scanf("%d",&m);
    fun(a,m);
    printf("\nThe string after moving :\n");
    puts(a);
    printf("\n\n");
/**********found**********/
    wf = fopen("out.dat","w");
    fun(b,3);
    fprintf(wf,"%s",b);
    fclose(wf);
/**********found**********/
}

Solution Idea

  1. Get the total length len of string w.
  2. We need to reorganize the string:
    • First part: characters starting from index m to the end. (the latter part)
    • Second part: characters from index 0 to m-1. (the first m chars)
  3. We can use a temporary char array to store the new string, then copy it back to original string w.

Example: "ABCDEFGHIJK", m=3 len=11 part1: w3 ~ w10DEFGHIJK part2: w0 ~ w2ABC Combine → DEFGHIJKABC

Complete code for fun()

复制代码
void fun(char *w, int m)
{
    int len = strlen(w);
    char temp[N];
    int i, j = 0;
    // Copy m to end
    for(i = m; i < len; i++)
    {
        temp[j++] = w[i];
    }
    // Copy 0 to m-1
    for(i = 0; i < m; i++)
    {
        temp[j++] = w[i];
    }
    temp[j] = '\0'; // add string terminator
    strcpy(w, temp);
}

Another concise version (without temp array, using string library)

复制代码
void fun(char *w, int m)
{
    char t[N];
    strcpy(t, w+m);
    strcat(t, w);
    t[strlen(w)] = '\0';
    strcpy(w, t);
}

Explanation: w+m points to the m-th character. strcpy(t,w+m) gets the latter half; strcat appends the original first m characters, then truncate the string to original length.

人人皆为创造者,共创方能共成长

每个人都是使用者,也是创造者;是数字世界的消费者,更是价值的生产者与分享者。在智能时代的浪潮里,单打独斗的发展模式早已落幕,唯有开放连接、创意共创、利益共享,才能让个体价值汇聚成生态合力,让技术与创意双向奔赴,实现平台与伙伴的快速成长、共赢致远。

原创永久分成,共赴星辰大海

原创创意共创、永久收益分成,是东方仙盟始终坚守的核心理念。我们坚信,每一份原创智慧都值得被尊重与回馈,以永久分成锚定共创初心,让创意者长期享有价值红利,携手万千伙伴向着科技星辰大海笃定前行,拥抱硅基 生命与数字智能交融的未来,共筑跨越时代的数字文明共同体。

东方仙盟:拥抱知识开源,共筑数字新生态

在全球化与数字化浪潮中,东方仙盟始终秉持开放协作、知识共享的理念,积极拥抱开源技术与开放标准。我们相信,唯有打破技术壁垒、汇聚全球智慧,才能真正推动行业的可持续发展。

开源赋能中小商户:通过将前端异常检测、跨系统数据互联等核心能力开源化,东方仙盟为全球中小商户提供了低成本、高可靠的技术解决方案,让更多商家能够平等享受数字转型的红利。

共建行业标准:我们积极参与国际技术社区,与全球开发者、合作伙伴共同制定开放协议 与技术规范,推动跨境零售、文旅、餐饮等多业态的系统互联互通,构建更加公平、高效的数字生态。

知识普惠,共促发展:通过开源社区 、技术文档与培训体系,东方仙盟致力于将前沿技术转化为可落地的行业实践,赋能全球合作伙伴,共同培育创新人才,推动数字经济 的普惠式增长

阿雪技术观

在科技发展浪潮中,我们不妨积极投身技术共享。不满足于做受益者,更要主动担当贡献者 。无论是分享代码、撰写技术博客,还是参与开源项目 维护改进,每一个微小举动都可能蕴含推动技术进步的巨大能量。东方仙盟是汇聚力量的天地,我们携手在此探索硅基 生命,为科技进步添砖加瓦。

Hey folks, in this wild tech - driven world, why not dive headfirst into the whole tech - sharing scene? Don't just be the one reaping all the benefits; step up and be a contributor too. Whether you're tossing out your code snippets , hammering out some tech blogs, or getting your hands dirty with maintaining and sprucing up open - source projects, every little thing you do might just end up being a massive force that pushes tech forward. And guess what? The Eastern FairyAlliance is this awesome place where we all come together. We're gonna team up and explore the whole silicon - based life thing, and in the process, we'll be fueling the growth of technology

相关推荐
Wang's Blog1 小时前
Java框架快速入门: Spring Security+OAuth2之过滤器与过滤器链
java·开发语言·spring
谢尔登1 小时前
01_Java基础速通
java·开发语言
前端炒粉1 小时前
DOM 常用 API 速查表
开发语言·javascript·ecmascript
en.en..3 小时前
C语言核心解析:#define与typedef本质区别
开发语言·c++·算法
2601_966949653 小时前
批量获取多只股票五档盘口的极简方案:QuantDash Python SDK 实战指南
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
点心的游戏开发世界3 小时前
GDScript 入门笔记:目录
开发语言·笔记·游戏引擎·godot
洋不写bug4 小时前
二叉树(二) 常见基础操作解析|结点数、树高、查找结点、判断完全二叉树
java·开发语言·数据结构·完全二叉树·二叉树结点数·树高·查找结点
果果燕4 小时前
实习笔记(六)主机按钮状态上报完整流程
开发语言·c++·php
平头哥技术团队4 小时前
Day 16 | 用 ul、ol、li 一个标签,把散资料收成有序的技能清单页
开发语言·前端·html·html5