第十四章 字符串

1.1 整数字符串转化

例题1 整数转化为字符串

#include <stdlib.h>

int main() {
int num = 65535;
char str[7] = {0};
char temp[7] = {0};
int i = 0, j = 0;

while(num) {
temp[i] = num%10 + '0';
i++;
num = num/10;
}
temp[i] = '\0';
printf("temp: %s\n", temp);
i = i - 1;
//逆转
while(i >= 0) {
str[j] = temp[i];
i--;
j++;
}
str[j] = '\0';
printf("str: %s\n", str);

         char*itoa(intvalue,char*string,intradix);
//itoa(num, str, 10);
//printf("integer = %d, str = %s\n", num, str);

return 0;
}


运行如图所示:且图中的注释代码也可以完成类似的功能。

例题2 字符串转化为整数

int main() {
int i = 0, sum = 0;
char temp[7] = {'1','2','3','4','5','\0'};


while(temp[i]) {
sum = sum*10 + (temp[i] - '0');
i++;
}

int atoi(const char *nptr);
//sum = atoi(temp);

printf("sum: %d\n", sum);
return 0;
}


运行结果如图所示:


1.2 字符数组和strcpy

例题1 strcpy实现

char* strcpy1(char *strDest, const char *strSrc) {
if ((strSrc == NULL) || (strDest == NULL)) {
return NULL;
}
char* address = strDest;
while(*strSrc != '\0') {
*strDest = *strSrc;
strDest++;
strSrc++;
}
return address;
}

int main() {
const char srcstr[] = {"adgdkgdkfal"};
char strdst[256] = {0};
int length = 0;
strcpy1(strdst, srcstr);
printf("strdst: %s\n", strdst);
length = strlen(strcpy1(strdst, srcstr));
printf("length of strdst: %d", length);
return 0;
}


运行结果如图所示:


例题2 循环右移

#include <string.h>

#define MAX_LEN  256

void LoopMove(char *pStr, int steps) {
int len = strlen(pStr);
char tmp[MAX_LEN] = {0};
//char* strcpy(char *strDest, const char *strSrc)
strcpy(&tmp[0], pStr+(len-steps));
strcpy(&tmp[steps], pStr);
//strcpy() which uses '\0' to indicate the end. otherwise is string constant.
//must be line.
tmp[len] = '\0';
strcpy(pStr, tmp);
}

void LoopMove1(char *pStr, int steps) {
int len = strlen(pStr);
char tmp[MAX_LEN] = {0};
//void *memcpy(void *dest, const void *src, size_t n)
memcpy(&tmp[0], pStr+(len-steps), steps);
memcpy(&tmp[steps], pStr, len-steps);
memcpy(pStr, tmp, len);
}

int main() {
char data[] = {"abdweifalg"};
printf("before loopmove: %s\n", data);
//LoopMove(data, 3);
LoopMove1(data, 3);
printf("after loopmove: %s\n", data);
return 0;
}


运行结果如下图所示:


1.3 数组初始化与数组越界

例题1 

#include <string.h>
void test1() {
char string[11];
char *str1 = "0123456789";
strcpy(string, str1);
printf("test1: %s\n", string);
}

void test2() {
char string[10], str1[10];
for(int i = 0; i < 9; i++) {
str1[i] = 'a';
}
str1[9] = '\0';
strcpy(string, str1);
printf("test2: %s\n", string);
}

void test3(char *str1) {
char string[10];
if (strlen(str1) < 10) {
strcpy(string, str1);
}
printf("test3: %s\n", string);
}

int main() {
char *str = "012345678";
test1();
test2();
test3(str);
return 0;
}


运行结果如下图所示:


例题2  

#define MAX 255

int main() {
char p[MAX+1];
unsigned char ch;
for(ch = 0; ch < MAX; ch++) {
p[ch] = ch;
printf("ch: %d\n", ch);
}
p[ch] = ch;
printf("ch: %d\n", ch);
return 0;
}


1.4 字符串其他问题

例题1 strstr()函数实现:该函数返回值是主串中字符子串的位置以后的所有字符。

const char* strstr1(const char* string, const char* subStrSet) {
int i = 0, j = 0;
for(i = 0; string[i] != '\0'; i++) {
if (string[i] == subStrSet[j]) {
while(string[i++] == subStrSet[j++]) {
if (subStrSet[j] == '\0') {
return &string[i-j];
}
}
}
}
return NULL;
}

int main() {
char *string = "123456789";
char *substr = "46";
char *substr1 = "456";
char subStrR[10];
const char *p = subStrR;
p = strstr1(string, substr1);
printf("after strstr: %s", p);
return 0;
}


运行结果如图所示:


例题2 句子翻转,如"i come from tianjin." 转换为“tianjin. from com i”

#include <string.h>

int main() {
char charStr[] = {"i come from tianjin."};
int j = strlen(charStr) - 1;
char temp = 0;
int i = 0;
int begin = 0, end = 0;

while(j > i) {
temp = charStr[j];
charStr[j] = charStr[i];
charStr[i] = temp;
j--;
i++;
}
printf("charStr: %s\n", charStr);
i = 0;

while(charStr[i] != '\0') {
if (charStr[i] != ' ') {
begin = i;
while(charStr[i] && charStr[i] != ' ') {
i++;
}
i = i -1;
end = i;
}

while(end > begin) {
temp = charStr[begin];
charStr[begin] = charStr[end];
charStr[end] = temp;
begin++;
end--;
}
i++;
}
printf("charStr: %s", charStr);
return 0;
}


运行结果如图所示:先全部翻转,再每个单词翻转。


1.5 字符子串问题

例题1 转换字符格式为原来字符串里的字符+该字符连续出现的个数

#include <string.h>

int main() {
char inchar[] = {"12344668899999"};
char outchar[50];
int len = strlen(inchar);
int k = 0, count = 1, j = 0;
for(k = 0; k < len; k++) {
if(inchar[k+1] == inchar[k]) {
count++;
} else {
// int sprintf( char *buffer, const char *format, [ argument] … )
sprintf(&outchar[j], "%c%d", inchar[k], count);
j = j + 2;
count = 1;
}
}
printf("outchar: %s", outchar);
return 0;
}


运行结果如图所示:

CSDN海神之光上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b或2023b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹中; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪(CEEMDAN)、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 1. EMD(经验模态分解,Empirical Mode Decomposition) 2. TVF-EMD(时变滤波的经验模态分解,Time-Varying Filtered Empirical Mode Decomposition) 3. EEMD(集成经验模态分解,Ensemble Empirical Mode Decomposition) 4. VMD(变分模态分解,Variational Mode Decomposition) 5. CEEMDAN(完全自适应噪声集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 6. LMD(局部均值分解,Local Mean Decomposition) 7. RLMD(鲁棒局部均值分解, Robust Local Mean Decomposition) 8. ITD(固有时间尺度分解,Intrinsic Time Decomposition) 9. SVMD(逐次变分模态分解,Sequential Variational Mode Decomposition) 10. ICEEMDAN(改进的完全自适应噪声集合经验模态分解,Improved Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 11. FMD(特征模式分解,Feature Mode Decomposition) 12. REMD(鲁棒经验模态分解,Robust Empirical Mode Decomposition) 13. SGMD(辛几何模态分解,Spectral-Grouping-based Mode Decomposition) 14. RLMD(鲁棒局部均值分解,Robust Intrinsic Time Decomposition) 15. ESMD(极点对称模态分解, extreme-point symmetric mode decomposition) 16. CEEMD(互补集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition) 17. SSA(奇异谱分析,Singular Spectrum Analysis) 18. SWD(群分解,Swarm Decomposition) 19. RPSEMD(再生相移正弦辅助经验模态分解,Regenerated Phase-shifted Sinusoids assisted Empirical Mode Decomposition) 20. EWT(经验小波变换,Empirical Wavelet Transform) 21. DWT(离散小波变换,Discraete wavelet transform) 22. TDD(时域分解,Time Domain Decomposition) 23. MODWT(最大重叠离散小波变换,Maximal Overlap Discrete Wavelet Transform) 24. MEMD(多元经验模态分解,Multivariate Empirical Mode Decomposition) 25. MVMD(多元变分模态分解,Multivariate Variational Mode Decomposition)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值