C:使用循环替换字符串中的子字符串

时间:2015-09-02 22:38:10

标签: c string loops substring

我正在努力克服字符串中替换子串的概念。此特定练习不希望您使用<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] != '\0'; 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] != '\0'; i++)
    {
        n++;
    }
    printf("the number of characters is %d\n",n);

    return (n);
}

1 个答案:

答案 0 :(得分:1)

我尝试了你的问题并为我的代码输出了输出。这是代码 - 编辑 - 这是已编辑的主要代码

    #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 '\0'. so "12-"2"=10" characters to be compared.
    for(i=0;str[i]!='\0';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]!='\0';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.
}

让我解释一下代码的几个部分 -

  1. 我使用过unsigned int e,因为我不想要&#39; e&#39;消极的。(我稍后会详细解释)。
  2. 在第一个for循环中,我正在检查我的字符串是否已到达结尾。
  3. 在第一个&#39; IF&#39; condn,我正在检查字符串的第一个字符是否与需要替换的单词的第一个字符是NOT-EQUAL。如果条件满足,则定期打印原始字符串。
  4. ELSE IF,即(字符串的第一个字符与单词的第一个字符相等)然后检查接下来的几个字符以确保单词匹配。在这里,我使用了&#39; e&#39;因为它会检查str [i + e]和data [i + e]的条件。例如ai notequalto ae。如果我没有在代码中使用&#39; e ...在检查第一个字符本身后,newdata将在newstr中打印出来。我使用了&#39; = 5因为第一个字母和第五个字母的概率在数据中是相同的而str是较少的。您也可以使用&#39; =&#39; = 4。没有必须仅使用&#39; =&#39; = 5。
  5. 现在,我正在减少&#39;并检查字符串中的字母是否相同。我无法增加,因为字符串的大小有一定的限制。因为,我使用unsigned int,&#39; e&#39;不会低于0。
  6. ELSE,(这意味着只有第一个字母匹配,str和数据的第5个字母不匹配),在newstr中打印str。
  7. 在最后一个FOR循环中,我使用了k <114,因为字符串中有很多字符。 (您可以编写代码来查找字符串中有多少个字符。无需手动计数)。
  8. 最后,我使用了条件(j&lt; 10)和(j&gt; = 10)以及ELSE-IF条件,因为在第一个ELSE-IF中,新数据的大小为10.因此,即使单词为被替换超过10,例如12。我不需要额外的2位存储在新数据中。因此,如果大小超过10,则在下一个ELSE-IF条件下绕过它。请注意,这10是新单词的大小。因此,如果您的单词更小或更大,它会有所不同。而且,在第二个ELSE-IF中,我没有增加&#39;(l ++),因为在这里,我没有在newstr中添加任何东西。我只是绕过它。所以,我没有增加。
  9. 我尽力把代码放在文字中。如果您有任何疑问,可以再问一次。我很乐意提供帮助。而且这段代码不是最优的。使用的数值因您使用的单词/字符串而异。当然,我可以为此编写一个通用代码(从字符串中自动获取数值)。但是,我没有在这里编写代码。此代码适用于您的问题。您可以更改一些变量,例如&#39; e&#39;和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]!='\0';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.
    

    //使用函数中的指针传递参数。