如何用一个字符替换两个字符?
How to replace two chars with one char?
所以我试图在一个句子中用 &
替换 cc
(例如:"Hi this is Mark cc Alice"
)。到目前为止,我有这段代码,它将第一个 c
替换为 &
但第二个 c
仍然存在。我将如何摆脱第二个 c
?
int main() {
char str[] = "Hi this is Mark cc Alice";
int i = 0;
while (str[i] != '[=10=]') {
if (str[i] == 'c' && str[i + 1] == 'c') {
str[i] = '&';
//str[i + 1] = ' ';
}
i++;
}
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n",str);
return str;
}
输入为:
Hi this is Mark cc Alice
输出为:
Hi this is Mark &c Alice
您可以使用通用的“后移”功能将其随机播放到一个位置:
void shunt(char* dest, char* src) {
while (*dest) {
*dest = *src;
++dest;
++src;
}
}
在哪里可以像这样使用它:
int main(){
char str[] = "Hi this is Mark cc Alice";
for (int i = 0; str[i]; ++i) {
if (str[i] == 'c' && str[i+1] == 'c') {
str[i]='&';
shunt(&str[i+1], &str[i+2]);
}
}
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n",str);
// main() should return a valid int status code (0 = success)
return 0;
}
注意从混乱的 int
声明 + while
+ 递增到一个 for
循环的切换。使用 char*
指针会更简洁:
for (char* s = str; *s; ++s) {
if (s[0] == 'c' && s[1] == 'c'){
*s = '&';
shunt(&s[1], &s[2]);
}
}
在使用 C 字符串时,重要的是要习惯使用指针,因为这可以为您省去 很多 的麻烦。
You should also familiarize yourself with the C Standard Library so you can use tools like strstr
instead of writing your own equivalent of same. Here strstr(str, "cc")
could have helped.
你必须将整个数组向左移动。一个简单的方法是:
#include <stdio.h>
#include <string.h>
#define STR_SIZE 25
int main(){
char str[STR_SIZE] = "Hi this is Mark cc Alice";
int i=0,j=0;
while(str[i]!='[=10=]'){
if(str[i]=='c' && str[i+1]=='c'){
str[i]='&';
for (j=i+1; j<STR_SIZE-1; j++) /* Shifting the array to the left */
{
str[j]=str[j+1];
}
}
i++;
}
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n",str);
return 0;
}
无需更改大部分代码,您也可以按照以下方式进行
#include <stdio.h>
int main()
{
char str[] = "Hi this is Mark cc Alice";
int i=0;
while(str[i]!='[=10=]')
{
if(str[i]=='c' && str[i+1]=='c')
{
str[i]='&';
// after replacing the first 'c' increment the i, so it will point to next c.
i++;
break;
//str[i+1]=' ';
}
i++;
}
// replace previous char with next char until null
while(str[i]!='[=10=]')
{
str[i] = str[i+1];
i++;
}
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n",str);
return 0;
}
可以使用二指法:使用单独的索引变量读写同一个数组中的字符。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hi this is Mark cc Alice";
int i = 0, j = 0;
while (str[i] != '[=10=]') {
if (str[i] == 'c' && str[i + 1] == 'c') {
str[j++] = '&';
i += 2;
} else {
str[j++] = str[i++];
}
}
str[j] = '[=10=]'; // set the null terminator that was not copied.
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n", str);
return 0;
}
所以我试图在一个句子中用 &
替换 cc
(例如:"Hi this is Mark cc Alice"
)。到目前为止,我有这段代码,它将第一个 c
替换为 &
但第二个 c
仍然存在。我将如何摆脱第二个 c
?
int main() {
char str[] = "Hi this is Mark cc Alice";
int i = 0;
while (str[i] != '[=10=]') {
if (str[i] == 'c' && str[i + 1] == 'c') {
str[i] = '&';
//str[i + 1] = ' ';
}
i++;
}
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n",str);
return str;
}
输入为:
Hi this is Mark cc Alice
输出为:
Hi this is Mark &c Alice
您可以使用通用的“后移”功能将其随机播放到一个位置:
void shunt(char* dest, char* src) {
while (*dest) {
*dest = *src;
++dest;
++src;
}
}
在哪里可以像这样使用它:
int main(){
char str[] = "Hi this is Mark cc Alice";
for (int i = 0; str[i]; ++i) {
if (str[i] == 'c' && str[i+1] == 'c') {
str[i]='&';
shunt(&str[i+1], &str[i+2]);
}
}
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n",str);
// main() should return a valid int status code (0 = success)
return 0;
}
注意从混乱的 int
声明 + while
+ 递增到一个 for
循环的切换。使用 char*
指针会更简洁:
for (char* s = str; *s; ++s) {
if (s[0] == 'c' && s[1] == 'c'){
*s = '&';
shunt(&s[1], &s[2]);
}
}
在使用 C 字符串时,重要的是要习惯使用指针,因为这可以为您省去 很多 的麻烦。
You should also familiarize yourself with the C Standard Library so you can use tools like
strstr
instead of writing your own equivalent of same. Herestrstr(str, "cc")
could have helped.
你必须将整个数组向左移动。一个简单的方法是:
#include <stdio.h>
#include <string.h>
#define STR_SIZE 25
int main(){
char str[STR_SIZE] = "Hi this is Mark cc Alice";
int i=0,j=0;
while(str[i]!='[=10=]'){
if(str[i]=='c' && str[i+1]=='c'){
str[i]='&';
for (j=i+1; j<STR_SIZE-1; j++) /* Shifting the array to the left */
{
str[j]=str[j+1];
}
}
i++;
}
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n",str);
return 0;
}
无需更改大部分代码,您也可以按照以下方式进行
#include <stdio.h>
int main()
{
char str[] = "Hi this is Mark cc Alice";
int i=0;
while(str[i]!='[=10=]')
{
if(str[i]=='c' && str[i+1]=='c')
{
str[i]='&';
// after replacing the first 'c' increment the i, so it will point to next c.
i++;
break;
//str[i+1]=' ';
}
i++;
}
// replace previous char with next char until null
while(str[i]!='[=10=]')
{
str[i] = str[i+1];
i++;
}
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n",str);
return 0;
}
可以使用二指法:使用单独的索引变量读写同一个数组中的字符。
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hi this is Mark cc Alice";
int i = 0, j = 0;
while (str[i] != '[=10=]') {
if (str[i] == 'c' && str[i + 1] == 'c') {
str[j++] = '&';
i += 2;
} else {
str[j++] = str[i++];
}
}
str[j] = '[=10=]'; // set the null terminator that was not copied.
printf("\n-------------------------------------");
printf("\nString After Replacing 'cc' by '&'");
printf("\n-------------------------------------\n");
printf("%s\n", str);
return 0;
}