取子串(指针)

cpp 复制代码
#include <stdio.h>
#include <string.h>

char* substr(char *s, int startloc, int len) {
    static char result[51]; // 定义一个足够大的静态数组来存储结果
    static char result1[] = {'N','U','L','L','\0'};
    int i, j;

    // 检查startloc是否在字符串的范围内
    if (startloc < 1 || startloc > strlen(s)) {
        return result1; // 如果不在范围内,返回NULL
    }

    // 计算实际要复制的子串长度
    int actual_len = (startloc + len-1 < strlen(s)) ? len : (strlen(s) - startloc+1);

    // 复制子串到结果数组
    for (i = startloc-1, j = 0; i < startloc + actual_len-1; i++, j++) {
        result[j] = s[i];
    }
    result[j] = '\0'; // 添加字符串结束符

    return result; // 返回结果
}

int main() {
	int t;
	scanf("%d",&t);
	while(t--)
	{
		char a[100];
		scanf("%s",a);
		int x,y;
		scanf("%d %d",&x,&y);
		printf("%s\n", substr(a, x, y));
	}

    return 0;
}
相关推荐
Hical6114 小时前
C++26 前瞻心得:下一代 C++ 最值得期待的特性
c++
悲伤小伞14 小时前
Linux_传输层协议TCP详解
linux·网络·c++·网络协议·tcp/ip
笨笨饿14 小时前
#72_聊聊I2C以及他们的变体
linux·c语言·网络·stm32·单片机·算法·个人开发
机器人图像处理14 小时前
6-自动白平衡(灰度世界算法)
opencv·算法·相机
Dr.Zeus14 小时前
从电芯到系统:BMS算法视角下的电池热管理深度解析作者署名
算法·能源
ulias21215 小时前
leetcode热题 - 6
linux·算法·leetcode
北顾笙98015 小时前
day42-数据结构力扣
数据结构
七颗糖很甜15 小时前
卫星通信遇到“太空天气”会怎样---电离层闪烁对卫星通信的影响
大数据·python·算法
Frank_refuel15 小时前
C++之STL->string类的使用和实现
java·开发语言·c++
风筝在晴天搁浅15 小时前
阿里淘天/京东 CodeTop LeetCode103.二叉树的锯齿形层序遍历
数据结构