简体   繁体   English

使用指针算法将数组元素更改为用户输入

[英]Changing elements of an array to user input with pointer arithmetic

I am trying to ask the user for 3 different strings, if the second string is a part of the first string it will replace that part with the third string.我试图向用户询问 3 个不同的字符串,如果第二个字符串是第一个字符串的一部分,它将用第三个字符串替换该部分。 In addition to this, I must use pointer arithmetic to index through any arrays/strings.除此之外,我必须使用指针算法来索引任何数组/字符串。 For example:例如:

string1 = foobar  
string2 = oba  
string3 = 12345   
output = fo12345r

My program works when the pattern string (string2) and the replacement string (string3) are the same lengths, but if not I get weird results.当模式字符串(string2)和替换字符串(string3)的长度相同时,我的程序可以工作,但如果不是,我会得到奇怪的结果。

For example:例如:

s1: foobar  
s2: obar  
s3: 3333  
r: fo3333 

(^works) (^作品)

s1: foobar  
s2: obar  
s3: 33  
r: fo33  ends without including the original last 2 characters ('a','r')  

(^doesn't work) (^不起作用)

s1: foobar  
s2: obar  
s3: 12345  
r: fo1234  ends without including the last new character ('5')  

(^doesn't work) (^不起作用)

#include <stdio.h>

void GetString(char *str);
void StringReplace(char *original, char *pattern, char *replacement);

int main(void){
    char originalstring[256];
    char patternstring[32];
    char replacement[32];

    printf("Please enter the original string:\n");
    GetString(originalstring);

    printf("Please enter the pattern:\n");
    GetString(patternstring);

    printf("Please enter the replacement:\n");
    GetString(replacement);

    StringReplace(originalstring, patternstring, replacement);
    printf("The Resultant string is %s\n", originalstring);
}


void StringReplace(char *original, char *pattern, char *replacement){
    for(int i = 0; *(original + i) != '\0'; i++){
        if(*(pattern) == *(original + i)){
            for(int x = 0; *(pattern + x) == *(original + i + x);  x++){
                if(x == 1){  
                    *(original + i + (x - 1)) = *(replacement + (x - 1));
                    *(original + i + x) = *(replacement + x);
                }
                if(x >= 2){
                    *(original + i + x) = *(replacement + x);
                }
            }
        }
    }
} // this is supposed to replace the pattern string with the replacement string if they have 2 or more similar characters in succession

You check some of the conditions., If pattern string was found in the original string, then check size_of(s2) with size_of(s3).您检查一些条件。如果在原始字符串中找到模式字符串,则检查 size_of(s2) 和 size_of(s3)。 if s3 >= s2, then simply remove pattern string from original string and replace it with s3.如果 s3 >= s2,则只需从原始字符串中删除模式字符串并将其替换为 s3。 Else case find the size difference between s3 and s2 and then remove pattern string from original string only with that size difference..Finally replace that place with s3.Hope This might helps:)否则,找到 s3 和 s2 之间的大小差异,然后仅从原始字符串中删除具有该大小差异的模式字符串。最后用 s3 替换那个地方。希望这可能会有所帮助:)

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

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