c语言对于指定的字符串,复制其中从m到n的多个字符,组成一个新串。
时间: 2024-06-13 07:09:45 浏览: 21
以下是C语言中复制指定字符串中从m到n的多个字符,组成一个新串的代码实现:
```c
#include <stdio.h>
#include <string.h>
void copystr(char *p1, char *p2, int m, int n) {
int i = 0;
while (i < m - 1) {
++;
p1++;
}
while (i < n) {
*p2 = *p1;
p1++;
p2++;
i++;
}
*p2 = '\0';
}
int main() {
int m, n;
char str1[20], str2[20];
printf("请输入一个字符串:");
gets(str1);
printf("从第几个字符开始复制?");
scanf("%d", &m);
printf("复制多少个字符?");
scanf("%d", &n);
if (strlen(str1) < m + n - 1)
printf("输入错误!");
else {
copystr(str1, str2, m, m + n);
printf("新的字符串为:%s", str2);
}
return 0;
}
```
相关问题
对于指定的字符串复制其中从m到n的多个字符组成一个新串
以下是两个C语言的例子,可以复制指定字符串中从m到n的多个字符组成一个新串:
1. 引用:
```c
#include<stdio.h>
#include<string.h>
#define n 100
void fun(char *p,int m,int n) {
char *q;
char s[n]={0};
int i;
int t;
q=s;
t=strlen(p);
memcpy(q,p+m-1,n-m+1); //将p中从第m到n的字符全部复制到q字符指针中
printf("%s",s);
}
int main() {
char s[n];
int m,n;
printf("请输入一个字符串\n");
gets(s);
printf("请输入一个M值\n");
scanf("%d",&m);
printf("请输入一个N值\n");
scanf("%d",&n);
fun(s,m,n);
return 0;
}
```
2. 引用:
```c
#include <stdio.h>
#include <string.h>
void copystr(char *p1,char *p2,int m,int n){
//字符串部分复制函数
int i=0;
while(i<n-m+1){
*(p2+i)=*(p1+m-1+i);
i++;
}
*(p2+i)='\0';
}
int main(){
int m,n;
char str1[20],str2[20];
printf("请输入一个字符串:");
gets(str1);
printf("从第几个字符开始复制?");
scanf("%d",&m);
printf("复制几个字符?");
scanf("%d",&n);
if(strlen(str1)<m+n-1)
printf("输入错误!");
else{
copystr(str1,str2,m,n);
printf("新的字符串为:%s",str2);
}
return 0;
}
```
对于指定的字符串,复制其中从m到n的多个字符,组成一个新串。C语言
以下是C语言实现复制指定字符串中从m到n的多个字符,组成一个新串的代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* copyString(char* str, int m, int n) {
int len = strlen(str);
if (m < 0 || n > len || m > n) {
return NULL;
}
char* newStr = (char*)malloc(sizeof(char) * (n - m + 1));
strncpy(newStr, str + m, n - m);
newStr[n - m] = '\0';
return newStr;
}
int main() {
char str[] = "Hello, world!";
int m = 2, n = 7;
char* newStr = copyString(str, m, n);
if (newStr == NULL) {
printf("Invalid input!\n");
} else {
printf("The new string is: %s\n", newStr);
free(newStr);
}
return 0;
}
```
输出结果为:
```
The new string is: llo, w
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)