使用c语言编写顺序串的所有功能,包括串赋值、串比较、串连接、求串长、求子串、串删除、串置换、子串查找定位等。
时间: 2023-12-14 12:02:12 浏览: 80
好的,这是一个关于C语言字符串操作的问题。C语言中提供了一些字符串相关的函数,例如strcpy()、strcmp()、strcat()、strlen()等可以实现串的赋值、比较、连接、求长度等功能。此外,还有一些函数可以实现子串的查找、删除、置换等操作。你可以根据你的具体需求来选择合适的函数进行操作。需要注意的是,字符串末尾必须是'\0'结束符。
相关问题
用c语言实现编写程序实现字符串的基本操作,包括求串长,赋值,比较,串连接,求子串
以下是用C语言实现字符串的基本操作的代码:
```c
#include <stdio.h>
#include <string.h>
// 求字符串长度
int strlen(char *s) {
int len = 0;
while (*s) {
len++;
s++;
}
return len;
}
// 字符串赋值
void strcpy(char *dest, char *src) {
while (*src) {
*dest = *src;
dest++;
src++;
}
*dest = '\0';
}
// 字符串比较
int strcmp(char *s1, char *s2) {
while (*s1 && *s2 && (*s1 == *s2)) {
s1++;
s2++;
}
return (*s1 - *s2);
}
// 字符串连接
void strcat(char *dest, char *src) {
while (*dest) {
dest++;
}
while (*src) {
*dest = *src;
dest++;
src++;
}
*dest = '\0';
}
// 求子串
char* substr(char *s, int start, int len) {
char *sub = (char*)malloc((len + 1) * sizeof(char));
int i;
for (i = 0; i < len; i++) {
sub[i] = s[start + i];
}
sub[len] = '\0';
return sub;
}
int main() {
char s1[100] = "Hello, world!";
char s2[100] = "C programming";
char *sub;
// 求字符串长度
printf("Length of s1: %d\n", strlen(s1));
printf("Length of s2: %d\n", strlen(s2));
// 字符串赋值
strcpy(s1, s2);
printf("After copying s2 to s1: %s\n", s1);
// 字符串比较
if (strcmp(s1, s2) == 0) {
printf("s1 and s2 are equal\n");
} else {
printf("s1 and s2 are not equal\n");
}
// 字符串连接
strcat(s1, " and ");
strcat(s1, "C++ programming");
printf("After concatenating: %s\n", s1);
// 求子串
sub = substr(s1, 5, 10);
printf("Substring of s1: %s\n", sub);
free(sub);
return 0;
}
```
输出结果:
```
Length of s1: 13
Length of s2: 14
After copying s2 to s1: C programming
s1 and s2 are not equal
After concatenating: C programming and C++ programming
Substring of s1: o programming
```
请使用C语言.编写程序实现字符串的基本操作,包括求串长、赋值、比较、串连接、求子串、替换和定位算法。
以下是基本操作的C语言实现:
```c
#include<stdio.h>
#include<string.h>
// 求字符串长度
int str_length(char *str) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
// 字符串赋值
void str_copy(char *str1, char *str2) {
int i = 0;
while (str2[i] != '\0') {
str1[i] = str2[i];
i++;
}
str1[i] = '\0';
}
// 字符串比较
int str_compare(char *str1, char *str2) {
int i = 0;
while (str1[i] != '\0' || str2[i] != '\0') {
if (str1[i] != str2[i]) {
return str1[i] - str2[i];
}
i++;
}
return 0;
}
// 字符串连接
void str_concat(char *str1, char *str2) {
int i = 0, j = 0;
while (str1[i] != '\0') {
i++;
}
while (str2[j] != '\0') {
str1[i] = str2[j];
i++;
j++;
}
str1[i] = '\0';
}
// 求子串
void str_substr(char *str1, char *str2, int start, int length) {
int i;
for (i = 0; i < length; i++) {
str2[i] = str1[start + i];
}
str2[length] = '\0';
}
// 字符串替换
void str_replace(char *str1, char *old_str, char *new_str) {
char *p = strstr(str1, old_str);
if (p != NULL) {
int old_length = str_length(old_str);
int new_length = str_length(new_str);
int diff = new_length - old_length;
int total_length = str_length(str1) + diff;
char temp[total_length + 1];
strcpy(temp, "");
while (p != NULL) {
strncat(temp, str1, p - str1);
strcat(temp, new_str);
str1 = p + old_length;
p = strstr(str1, old_str);
}
strcat(temp, str1);
strcpy(str1, temp);
}
}
// 定位算法
int str_locate(char *str1, char *str2) {
char *p = strstr(str1, str2);
if (p != NULL) {
return p - str1;
}
return -1;
}
int main() {
char str1[100], str2[100], old_str[100], new_str[100];
int start, length;
printf("请输入字符串1:");
gets(str1);
printf("请输入字符串2:");
gets(str2);
printf("字符串1的长度为:%d\n", str_length(str1));
str_copy(str1, str2);
printf("字符串1复制后为:%s\n", str1);
printf("字符串1和字符串2比较结果为:%d\n", str_compare(str1, str2));
str_concat(str1, str2);
printf("字符串1和字符串2连接后为:%s\n", str1);
printf("请输入字符串1的子串起始位置和长度,用空格隔开:");
scanf("%d %d", &start, &length);
str_substr(str1, str2, start, length);
printf("字符串1的子串为:%s\n", str2);
printf("请输入要替换的字符串:");
scanf("%s", old_str);
printf("请输入要替换为的新字符串:");
scanf("%s", new_str);
str_replace(str1, old_str, new_str);
printf("替换后的字符串为:%s\n", str1);
printf("请输入要查找的字符串:");
scanf("%s", str2);
printf("字符串2在字符串1中的位置为:%d\n", str_locate(str1, str2));
return 0;
}
```
注意,本代码中使用了`gets()`函数,该函数在C11标准中已经被废弃,可以使用`fgets()`函数代替。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)
![](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)