當前位置:首頁 » 編程語言 » c語言strrpl
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

c語言strrpl

發布時間: 2022-01-20 00:29:44

1. c語言:如何將字元串中指定的字元替換為另一個指定字元

需要准備的材料分別有:電腦、C語言編譯器。

1、首先,打開C語言編譯器,新建一個初始.cpp文件,例如:test.cpp。

2. C語言從右到左strtok解析字元串

最近正好看到字元串處理函數部分,所以答一下,順便練習。
思路是使用字元串數組存儲分割後的字元串,知道數組大小,就可以獲取最後一個 / 之前的字元串(即倒數第二個數組元素)。
C語言中沒有string這個類型,要實現字元串數組可以考慮利用指針數組(其實質就是二維字元數組)。
下面是一個示例代碼:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define LEN 30 //指針數組的大小

int main(void)
{
char str[] = "ab/cds/1231/csdf/ae/qdsfa";
char *token = NULL, *p[LEN];
int i = 0;

p[i] = strtok(str, "/");
while ((token = strtok(NULL, "/")) != NULL)
p[++i] = token;
printf("第一個字元串: %s\n第二個字元串: %s\n倒數第二個字元串: %s\n", \
p[0], p[1], p[--i]);
return 0;
}

3. c語言 strstr和strrpl庫函數的作用是什麼

標准C語言實現下列標准庫函數,設計中不得使用其他庫函數。
strstr庫函數:

char *strstr(char *str1,char *str2);
在字元串str1中,尋找字串str2,若找到返回找到的位置,否則返回NULL。

#include <iostream>

char *strstr(const char *str1, const char *str2);

char *strstr(const char *str1, const char *str2)
{
char *s1, *s2;
assert ((str1 != (char *)0) && (str2 != (char *)0));

/* 空字元串是任務字元串的子字元串 */
if ('\0' == *str2)
{
return ((char *)str1);
}

while (*str1)
{
s1 = (char *)str1;
s2 = (char *)str2;

while ((*s1 == *s2) && *s1 && *s2)
{
s1++;
s2++;
}
if ('\0' == *s2)
{
return ((char *)str1);
}
str1++;
}
/* 查找不成功,返回NULL */
return ((char *)0);
}

int main(int argc,char *argv[])
{

char a[20]="abcde";
char b[20]="bc";
printf("%s\n", strstr(a,b));

system ("pause");
return 0;
}

strrpl庫函數:

/* 把 s 中的字元串 s1 替換成 s2 */

char *strrpl(char *s, const char *s1, const char *s2)

{

char *ptr;

while (ptr = strstr(s, s1)) /* 如果在s中找到s1 */

{

memmove(ptr + strlen(s2) , ptr + strlen(s1), strlen(ptr) - strlen(s1) + 1);

memcpy(ptr, &s2[0], strlen(s2));

}

return s;

}

4. strrpl 是c語言庫函數嗎

是的
strrpl庫函數:

/* 把 s 中的字元串 s1 替換成 s2 */

char *strrpl(char *s, const char *s1, const char *s2)

{

char *ptr;

while (ptr = strstr(s, s1)) /* 如果在s中找到s1 */

{

memmove(ptr + strlen(s2) , ptr + strlen(s1), strlen(ptr) - strlen(s1) + 1);

memcpy(ptr, &s2[0], strlen(s2));

}

return s;

}

5. c語言 strstr和strrpl庫函數的作用是什麼

標准C語言實現下列標准庫函數,設計中不得使用其他庫函數。
strstr庫函數:
char
*strstr(char
*str1,char
*str2);
在字元串str1中,尋找字串str2,若找到返回找到的位置,否則返回NULL。
#include
<iostream>
char
*strstr(const
char
*str1,
const
char
*str2);
char
*strstr(const
char
*str1,
const
char
*str2)
{
char
*s1,
*s2;
assert
((str1
!=
(char
*)0)
&&
(str2
!=
(char
*)0));
/*
空字元串是任務字元串的子字元串
*/
if
(''
==
*str2)
{
return
((char
*)str1);
}
while
(*str1)
{
s1
=
(char
*)str1;
s2
=
(char
*)str2;
while
((*s1
==
*s2)
&&
*s1
&&
*s2)
{
s1++;
s2++;
}
if
(''
==
*s2)
{
return
((char
*)str1);
}
str1++;
}
/*
查找不成功,返回NULL
*/
return
((char
*)0);
}
int
main(int
argc,char
*argv[])
{
char
a[20]="abcde";
char
b[20]="bc";
printf("%s
",
strstr(a,b));
system
("pause");
return
0;
}
strrpl庫函數:
/*

s
中的字元串
s1
替換成
s2
*/
char
*strrpl(char
*s,
const
char
*s1,
const
char
*s2)
{
char
*ptr;
while
(ptr
=
strstr(s,
s1))
/*
如果在s中找到s1
*/
{
memmove(ptr
+
strlen(s2)
,
ptr
+
strlen(s1),
strlen(ptr)
-
strlen(s1)
+
1);
memcpy(ptr,
&s2[0],
strlen(s2));
}
return
s;
}

6. c語言:如何將字元串中指定的字元替換為另一個指定字元

void
rep(char
*s,char
*s1,char
*s2)
{
char
*p;
for(;*s;s++)
/*順序訪問字元串s中的每個字元*/
{
for(p=s1;*p&&*p!=*s;p++);/*檢查當前字元是否在字元串s1中出現*/
if(*p)
*s=*(p-s1+s2);
/*當前字元在字元串s1中出現,用字元串s2中的對應字元代替s中的字元*/
}
}
不知道對於不對,你自己去試下,對了請採納,不對請往下瀏覽