繁体   English   中英

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] != '\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);
}

我试过你的问题并得到了我的代码的输出。 这是代码-编辑-这是编辑的主代码

    #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,因为我不希望 'e' 变为负数。(稍后我将对此进行更多解释)。
  2. 在第一个 for 循环中,我正在检查我的字符串是否已到达结尾。
  3. 在第一个 'IF' condn 中,我正在检查字符串的第一个字符是否与需要替换的单词的第一个字符不相等。 如果条件满足,则定期打印原始字符串。
  4. ELSE IF,即(字符串的第一个字符等于单词的第一个字符)然后检查接下来的几个字符以确保单词匹配。 在这里,我使用了 'e',因为它会检查 str[i+e] 和 data[i+e] 的条件。 示例- ai 不等于 ae。 如果我没有使用 'e'in 代码,...在检查第一个字符本身之后,newdata 将被打印在 newstr 中。 我使用 'e'=5 是因为数据中第 1 个字母和第 5 个字母的概率相同并且 str 较小。 您也可以使用 'e'=4。 没有规则必须只使用 'e'=5。
  5. 现在,我正在减少 'e' 并检查字符串中的字母是否相同。 我不能增加,因为字符串的大小有一定的限制。 因为,我使用了 unsigned int,'e' 不会低于 0。
  6. ELSE,(这意味着只有第一个字母匹配,str和data的第5个字母不匹配),打印newstr中的str。
  7. 在最后一个 FOR 循环中,我使用了 k<114,因为字符串中有很多字符。 (你可以写一段代码,找出一个字符串中有多少个字符,无需手动统计)。
  8. 最后,我使用了条件 (j<10) 和 (j>=10) 以及 ELSE-IF 条件,因为在第一个 ELSE-IF 中,新数据的大小为 10。因此,即使要替换的单词是超过 10 个,例如 12 个。 我不需要将额外的 2 位存储在新数据中。 因此,如果大小超过 10,只需在下一个 ELSE-IF 条件中绕过它。 请注意,这 10 是新单词的大小。 因此,如果您的单词更小或更大,它会有所不同。 而且,在第二个 ELSE-IF 中,我没有增加 'l'(l++) 因为,在这里,我没有在 newstr 中放入任何内容。 我只是绕过它。 所以,我没有增加。

我尽力把代码写成文字。 如有疑问,可再问。 我很乐意提供帮助。 并且此代码不是最佳的。 使用的数值因您使用的单词/字符串而异。 当然,我可以为此编写一个通用代码(从字符串中自动获取数值)。 但是,我没有在这里写那个代码。 此代码适用于您的问题。 您可以更改一些变量,例如 '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]!='\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.

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM