網站首頁 編程語言 正文
字符串中插入某段字符
核心思想
已知插入字符串的長度len,將插入的位置的后面的字母統統往后移動len個長度。
最后將插入的字符串插入到被插入的字符串。
我們先定義(注意:這里我不考慮極端的情況,如字符串溢出的問題!)
char arr[50],str[20];//arr是被插入的函數,str是插入的函數
我們在確定幾個int數
int index, lens, size, lena,j;
其中index是被插入字符串的位置,lens是str的長度,lena是arr的長度,size是arr的index后面要移動lens個長度
接著我們來去這str和arr
gets_s(arr);
printf("輸入插入的位置和插入的字符串:");
scanf_s("%d", &index);
getchar();//清除緩沖區
gets_s(str);
于是,我們確定長度,在確定要將index后面的所有字母都要挪動size次,才能有足夠的空間才存入lens的長度
lena = strlen(arr);//被 插入字符串個數
lens = strlen(str);//要插入字符串個數
size =lens;//一共要挪動的size次數
我們再定義一個變量indexx,作用是輔助作用,等會就懂了
int indexx = index;
最后我們用二層循環
for (int i = 0; i < size; i++) {
for (j = lena; j > indexx; j--) {
arr[j] = arr[j - 1];//前一個位置給后一個賦值
}
//因已經挪動位置了,故indexx和lena都要加1,才能保證第二個for能循環lena-indexx次
//因為要挪動的是index后面的字母
lena++;//arr長度要加1
indexx++;
}
末尾放結束語句
arr[lena] = '\0';//結束字符串
最后插入str
//最后給插入str
int k = 0;
for (int j = index,k=0; j < index + lens; j++,k++) {
arr[j] = str[k];
}
puts(arr);
完整代碼:
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
int main() {
//字符串的插入操作
int index, lens, size, lena,j;
char arr[50],str[20];//arr是被插入的函數,str是插入的函數
gets_s(arr);
printf("輸入插入的位置和插入的字符串:");
scanf_s("%d", &index);
getchar();//清除緩沖區
gets_s(str);
lena = strlen(arr);//被插入字符串個數
lens = strlen(str);//要插入字符串個數
size =lens;//一共要挪動的size次數
//用另一個一個變量,來代替這個變量
int indexx = index;
for (int i = 0; i < size; i++) {
for (j = lena; j > indexx; j--) {
arr[j] = arr[j - 1];//前一個位置給后一個賦值
}
//以為已經挪動位置了,所以indexx和lena都要加1,才能保證第二次第二個for能循環lena-index次
//因為要挪動的是index后面的字母
lena++;
indexx++;
}
arr[lena] = '\0';//結束字符串
//最后給插入str
int k = 0;
for (int j = index,k=0; j < index + lens; j++,k++) {
arr[j] = str[k];
}
puts(arr);
return 0;
}
字符串刪除某段字符串
核心思想
兩層循環,第一層是移動的程度,第二層是移動的字母個數
我們先定義一個字符串 計算他的長度
char arr[50];
gets_s(arr);
int len = strlen(arr);
確定:
int index,lens,k,movestar;
其中 index是插入位置,movestar是要被移動的字母的位置,lens是刪除的長度
printf("輸入您要刪除的開始位置和字符長度");
scanf_s("%d %d", &index, &lens);
如果遇到極端情況,則
if (lens + index > len) {
printf("刪除長度過大!");
return 0;
}
?既然要移動字母,我們肯定得先知道要移動多少個字母,于是定義res
int res = len - index - lens + 1;//要往前挪動的字母個數
res加1使用為我多減掉了1
就不如說我在abcdefg中刪除cd,則index就是3,lens就是2,movesatr就是e的位置,res就是efg這三個被移動的字母
要用兩層循環
int lenss = lens;//輔助變量
for (int i = 0; i < lenss; i++) {
movestar = index + lens - 1;//要開始往前挪動字母的所在位置
for (int j = movestar,k=0; k<res; j++,k++) {
arr[j - 1] = arr[j];
}
arr[--len] = '\0';//置零
lens--;//因為movesatr的位置會改變,所以要-1
}
來試一下效果:
完整代碼:?
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main() {
int index,lens,k,movestar;
char arr[50];
gets_s(arr);
int len = strlen(arr);
printf("輸入您要刪除的開始位置和字符長度");
scanf_s("%d %d", &index, &lens);
if (lens + index > len) {
printf("刪除長度過大!");
return 0;
}
int res = len - index - lens + 1;//要往前挪動的字母個數
int lenss = lens;//輔助變量
for (int i = 0; i < lenss; i++) {
movestar = index + lens - 1;//要開始往前挪動字母的所在位置
for (int j = movestar,k=0; k<res; j++,k++) {
arr[j - 1] = arr[j];
}
arr[--len] = '\0';
lens--;
}
puts(arr);
return 0;
}
總結
原文鏈接:https://blog.csdn.net/l1977831649/article/details/125946135
相關推薦
- 2022-08-18 使用C++的ORM框架QxORM詳解_C 語言
- 2022-10-02 Flutter的鍵值存儲數據庫使用示例詳解_Android
- 2022-04-09 SpringBoot項目的Dockerfile和docker-compose.yml部署文件
- 2022-04-02 .Net使用SuperSocket框架實現WebSocket前端_實用技巧
- 2022-04-28 Python的線程使用隊列Queue來改造轉賬場景_python
- 2023-12-10 記錄一次多數據源配置失效的情況
- 2022-09-07 C語言函數調用約定和返回值詳情_C 語言
- 2022-09-02 Docker資源限制Cgroup的深入理解_docker
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支