C:使用循环替换字符串中的子字符串
C: Replacing a substring within a string using loops
我正在为替换字符串中的子字符串的概念而苦恼。此特定练习不希望您使用 <string.h>
或 <strings.h>
.
中的内置函数
给定由以下两行组成的字符串:
"Mr. Fay, is this going to be a battle of wits?"
"If it is," was the indifferent retort, "you have come unarmed!"
我必须用另一个字符串替换一个子字符串。
这是我目前所拥有的,我在将子字符串复制到新数组并用新字符串替换子字符串时遇到了问题:
#include <stdio.h>
#include <string.h>
int dynamic();
int main()
{
char str[]="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";
int i, j=0, k=0, l=0, n=0;
unsigned int e = n-2;
char data[150];
char newData[150];
char newStr[150];
printf("Give me a substring from the string");
gets(data);
printf("Give me a substring to replace it with");
gets(newData);
dynamic();
for (i=0; str[i] != '[=11=]'; i++)
{
if (str[i] != data[j])
{
newStr[l] = str[i];
l++;
}
else if ((str[i+e] == data[j+e]) && (j<n))
{
newStr[l] = newData[j];
j++;
l++;
e--;
}
else if ((str[i+e] == data[j+e]) && (j>=n))
{
j++;
e--;
}
else
{
newStr[l] = str[i];
l++;
}
}
printf("original string is-");
for (k=0; k<n; k++)
printf("%c",str[k]);
printf("\n");
printf("modified string is-");
for(k=0; k<n; k++)
printf("%c",newStr[k]);
printf("\n");
}
int dynamic()
{
char str[]="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";
int i, n=0;
for (i=0; str[i] != '[=11=]'; i++)
{
n++;
}
printf("the number of characters is %d\n",n);
return (n);
}
我尝试了你的问题并得到了我的代码的输出。这是代码-
编辑 - 这是编辑后的主要代码
#include <stdio.h>
#include <string.h>
int var(char *); //function declaration. I am telling CPU that I will be using this function in the later stage with one argument of type char *
int main() //main function
{
char *str="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";
int i,j=0,k=0,l=0;
char data[] = "indifferent";
char newData[] = "nonchalant";
char newStr[150];
//here 'n' is returned from the 'var' function and is received in form of r,r1,r2,r3.
int r=var(str); //getting the length of str from the function 'var' and storing in 'r'
int r1=var(data); //getting the length of data from the function 'var' and storing in 'r1'
int r2=var(newData); //getting the length of newData from the function and storing in 'r2'
unsigned int e=r1-2; //r1-2 because r1 is the data to be replaced. and string index starts from 0. Here r1 is of length 12. but we dont need to check last
//character because it is null character and the index starts from 0. not from 1. so, it is 0 to 11 and 11th is '[=10=]'. so "12-"2"=10" characters to be compared.
for(i=0;str[i]!='[=10=]';i++)
{
if(str[i]!=data[j])
{
newStr[l]=str[i];
l++;
}
else if((str[i+e]==data[j+e]) && (j<r2))
{
newStr[l]=newData[j];
j++;
l++;
e--;
}
else if((str[i+e]==data[j+e]) && (j>=r2))
{
j++;
e--;
}
else
{
newStr[l]=str[i];
l++;
}
}
int r3=var(newStr); //getting the length of str from the function and storing in 'r'
printf("original string is-");
for(k=0;k<r;k++)
printf("%c",str[k]);
printf("\n");
printf("modified string is-");
for(k=0;k<r3;k++)
printf("%c",newStr[k]);
printf("\n");
} // end of main function
// Below is the new function called 'var' to get the character length
//'var' is the function name and it has one parameter. I am returning integer. so, it is int var.
int var(char *stri)//common function to get length of strings and substrings
{
int i,n=0;
for(i=0;stri[i]!='[=10=]';i++)
{
n++; //n holds the length of a string.
}
// printf("the number of characters is %d\n",n);
return (n); //returning this 'n' wherever the function is called.
}
让我解释一下代码的几个部分-
- 我使用了unsigned int e,因为我不想让'e'变成负数。(稍后我会解释更多)。
- 在第一个 for 循环中,我正在检查我的字符串是否已到达末尾。
- 在第一个 'IF' condn 中,我正在检查字符串的第一个字符是否不等于需要替换的单词的第一个字符。如果条件满足,则定期打印原始字符串。
- ELSE IF,即(字符串的第一个字符等于单词的第一个字符)然后检查接下来的几个字符以确保单词匹配。在这里,我使用 'e' 因为它会检查 str[i+e] 和 data[i+e] 的条件。示例- ai notequalto ae。如果我没有在代码中使用 'e',...在检查第一个字符本身之后,newdata 将打印在 newstr 中。我使用 'e'=5 因为第一个字母和第五个字母在数据中相同的概率和 str 更少。您也可以使用 'e'=4。没有规定你只能使用 'e'=5。
- 现在,我递减 'e' 并检查字符串中的字母是否相同。我不能递增,因为字符串的大小有一定的限制。因为,我使用了 unsigned int,'e' 不会低于 0。
- ELSE,(这意味着只有第一个字母匹配,str的第5个字母和数据不匹配),打印newstr中的str。
- 在最后一个 FOR 循环中,我使用了 k<114,因为字符串中有那么多字符。 (可以自己写一段代码,查找一个字符串有多少个字符,不用手动统计)。
- 最后,我使用了条件 (j<10) 和 (j>=10) 以及 ELSE-IF 条件,因为在第一个 ELSE-IF 中,新数据的大小为 10。因此,即使要替换的单词超过 10 个,例如 12 个。我不需要将额外的 2 位存储在新数据中。因此,如果大小超过 10,只需在下一个 ELSE-IF 条件中绕过它。请注意,这 10 是新单词的大小。因此,如果您的单词更小或更大,它会有所不同。并且,在第二个 ELSE-IF 中,我没有递增 'l'(l++),因为在这里,我没有在 newstr 中放置任何内容。我只是绕过它。所以,我没有增加。
我尽量把代码写成文字。有疑问可以再问。我很乐意提供帮助。而且这段代码不是最佳的。使用的数值因您使用的 words/strings 而异。当然,我可以为此编写通用代码(自动从字符串中获取数值)。但是,我没有在这里编写该代码。此代码适用于您的问题。您可以更改一些变量,例如 'e' 和 ELSE-IF 部分,并尝试了解代码的工作原理。一起玩吧。
编辑-
包括
int main()
{
char str[]="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";// I took this as string. The string which u need to calculate the length, You have to pass that as the function parameter.
int i,n=0;
for(i=0;str[i]!='[=11=]';i++)
{
n++;
}
printf("the number of characters is %d\n",n);
return (n);
}// If you execute this as a separate program, you will get the number of characters in the string. Basically, you just have to modify this code to act as a separate function and when calling the function, you have to pass correct arguments.
//在函数中使用指针传递参数。
我正在为替换字符串中的子字符串的概念而苦恼。此特定练习不希望您使用 <string.h>
或 <strings.h>
.
给定由以下两行组成的字符串:
"Mr. Fay, is this going to be a battle of wits?"
"If it is," was the indifferent retort, "you have come unarmed!"
我必须用另一个字符串替换一个子字符串。
这是我目前所拥有的,我在将子字符串复制到新数组并用新字符串替换子字符串时遇到了问题:
#include <stdio.h>
#include <string.h>
int dynamic();
int main()
{
char str[]="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";
int i, j=0, k=0, l=0, n=0;
unsigned int e = n-2;
char data[150];
char newData[150];
char newStr[150];
printf("Give me a substring from the string");
gets(data);
printf("Give me a substring to replace it with");
gets(newData);
dynamic();
for (i=0; str[i] != '[=11=]'; i++)
{
if (str[i] != data[j])
{
newStr[l] = str[i];
l++;
}
else if ((str[i+e] == data[j+e]) && (j<n))
{
newStr[l] = newData[j];
j++;
l++;
e--;
}
else if ((str[i+e] == data[j+e]) && (j>=n))
{
j++;
e--;
}
else
{
newStr[l] = str[i];
l++;
}
}
printf("original string is-");
for (k=0; k<n; k++)
printf("%c",str[k]);
printf("\n");
printf("modified string is-");
for(k=0; k<n; k++)
printf("%c",newStr[k]);
printf("\n");
}
int dynamic()
{
char str[]="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";
int i, n=0;
for (i=0; str[i] != '[=11=]'; i++)
{
n++;
}
printf("the number of characters is %d\n",n);
return (n);
}
我尝试了你的问题并得到了我的代码的输出。这是代码- 编辑 - 这是编辑后的主要代码
#include <stdio.h>
#include <string.h>
int var(char *); //function declaration. I am telling CPU that I will be using this function in the later stage with one argument of type char *
int main() //main function
{
char *str="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";
int i,j=0,k=0,l=0;
char data[] = "indifferent";
char newData[] = "nonchalant";
char newStr[150];
//here 'n' is returned from the 'var' function and is received in form of r,r1,r2,r3.
int r=var(str); //getting the length of str from the function 'var' and storing in 'r'
int r1=var(data); //getting the length of data from the function 'var' and storing in 'r1'
int r2=var(newData); //getting the length of newData from the function and storing in 'r2'
unsigned int e=r1-2; //r1-2 because r1 is the data to be replaced. and string index starts from 0. Here r1 is of length 12. but we dont need to check last
//character because it is null character and the index starts from 0. not from 1. so, it is 0 to 11 and 11th is '[=10=]'. so "12-"2"=10" characters to be compared.
for(i=0;str[i]!='[=10=]';i++)
{
if(str[i]!=data[j])
{
newStr[l]=str[i];
l++;
}
else if((str[i+e]==data[j+e]) && (j<r2))
{
newStr[l]=newData[j];
j++;
l++;
e--;
}
else if((str[i+e]==data[j+e]) && (j>=r2))
{
j++;
e--;
}
else
{
newStr[l]=str[i];
l++;
}
}
int r3=var(newStr); //getting the length of str from the function and storing in 'r'
printf("original string is-");
for(k=0;k<r;k++)
printf("%c",str[k]);
printf("\n");
printf("modified string is-");
for(k=0;k<r3;k++)
printf("%c",newStr[k]);
printf("\n");
} // end of main function
// Below is the new function called 'var' to get the character length
//'var' is the function name and it has one parameter. I am returning integer. so, it is int var.
int var(char *stri)//common function to get length of strings and substrings
{
int i,n=0;
for(i=0;stri[i]!='[=10=]';i++)
{
n++; //n holds the length of a string.
}
// printf("the number of characters is %d\n",n);
return (n); //returning this 'n' wherever the function is called.
}
让我解释一下代码的几个部分-
- 我使用了unsigned int e,因为我不想让'e'变成负数。(稍后我会解释更多)。
- 在第一个 for 循环中,我正在检查我的字符串是否已到达末尾。
- 在第一个 'IF' condn 中,我正在检查字符串的第一个字符是否不等于需要替换的单词的第一个字符。如果条件满足,则定期打印原始字符串。
- ELSE IF,即(字符串的第一个字符等于单词的第一个字符)然后检查接下来的几个字符以确保单词匹配。在这里,我使用 'e' 因为它会检查 str[i+e] 和 data[i+e] 的条件。示例- ai notequalto ae。如果我没有在代码中使用 'e',...在检查第一个字符本身之后,newdata 将打印在 newstr 中。我使用 'e'=5 因为第一个字母和第五个字母在数据中相同的概率和 str 更少。您也可以使用 'e'=4。没有规定你只能使用 'e'=5。
- 现在,我递减 'e' 并检查字符串中的字母是否相同。我不能递增,因为字符串的大小有一定的限制。因为,我使用了 unsigned int,'e' 不会低于 0。
- ELSE,(这意味着只有第一个字母匹配,str的第5个字母和数据不匹配),打印newstr中的str。
- 在最后一个 FOR 循环中,我使用了 k<114,因为字符串中有那么多字符。 (可以自己写一段代码,查找一个字符串有多少个字符,不用手动统计)。
- 最后,我使用了条件 (j<10) 和 (j>=10) 以及 ELSE-IF 条件,因为在第一个 ELSE-IF 中,新数据的大小为 10。因此,即使要替换的单词超过 10 个,例如 12 个。我不需要将额外的 2 位存储在新数据中。因此,如果大小超过 10,只需在下一个 ELSE-IF 条件中绕过它。请注意,这 10 是新单词的大小。因此,如果您的单词更小或更大,它会有所不同。并且,在第二个 ELSE-IF 中,我没有递增 'l'(l++),因为在这里,我没有在 newstr 中放置任何内容。我只是绕过它。所以,我没有增加。
我尽量把代码写成文字。有疑问可以再问。我很乐意提供帮助。而且这段代码不是最佳的。使用的数值因您使用的 words/strings 而异。当然,我可以为此编写通用代码(自动从字符串中获取数值)。但是,我没有在这里编写该代码。此代码适用于您的问题。您可以更改一些变量,例如 'e' 和 ELSE-IF 部分,并尝试了解代码的工作原理。一起玩吧。
编辑-
包括
int main()
{
char str[]="\n\"Mr. Fay, is this going to be a battle of wits?\" \n\"If it is,\" was the indifferent retort, \"you have come unarmed!\"";// I took this as string. The string which u need to calculate the length, You have to pass that as the function parameter.
int i,n=0;
for(i=0;str[i]!='[=11=]';i++)
{
n++;
}
printf("the number of characters is %d\n",n);
return (n);
}// If you execute this as a separate program, you will get the number of characters in the string. Basically, you just have to modify this code to act as a separate function and when calling the function, you have to pass correct arguments.
//在函数中使用指针传递参数。