我无法解决改变角色

时间:2019-01-27 10:15:46

标签: c

必须使用返回字符串长度的函数。 编写一个接收两个字符链(n1,n2)的函数。该功能的功能是检查字符串n2是否为字符串n1的订阅。该函数返回字符串n1(如果n2是字符串n1)或-1(如果n2不是字符串n1)中字符串n2首次出现的索引。假设:题字n2短于题字n1。 示例:题词n1:“计算机”题词n2:“ er”函数返回:6

我做到了,它奏效了

#include <stdio.h>

#define LIMIT 50

char * string_in(char *string, char *substring);
char * get(char *string, int n);

int main(void)
{
    // test string_in()

    char string[LIMIT];
    char substring[LIMIT];

    char *substr_loc;

    printf("Enter a string: ");
    get(string, LIMIT);
    while (string[0] != '\0')
    {
        printf("Enter a substring to look for: ");
        get(substring, LIMIT);

        substr_loc = string_in(string, substring);

        if (substr_loc == NULL)
            printf("%s not in %s\n", substring, string);
        else
            printf("%s found in %s at index %lu\n",
                   substring, string, substr_loc - string);

        printf("Enter a string (empty line to quit): ");
        get(string, LIMIT);
    }

    puts("Bye");

    return 0;
}

char * string_in(char *string, char *substring)
{
    // checks if substring is in string
    // returns pointer to first location of substring
    // in string or NULL if substring not in string

    int i;

    while (*string != '\0')
    {
        i = 0;

        // check for substring at current location
        while (*(string + i) == *(substring + i))
        {
            i++;

            // if next char in substring is null, then match
            // is found. return current location
            if (*(substring + i) == '\0')
                return string;
        }

        string++;
    }

    // no match
    return NULL;
}


char * get(char *string, int n)
{
    // wrapper for fgets that replaces first newline with null

    char *return_value = fgets(string, n, stdin);

    while (*string != '\0')
    {
        if (*string == '\n')
        {
            *string = '\0';
            break;
        }

        string++;
    }

    return return_value;
}

下一步是 编写程序的一部分,将用字符串(字符“ *”)替换字符串n1中所有出现的n2字符串。从任务点使用该功能。请告诉我如何编写此功能

示例:n1:“眼镜” n2:“ c”字符串更改后的n1。 "Spe*ta*le"

void function(char * get, char * string_in)
int i = 0;
for ( i = 0; get[i]=!'\0';i++){
if (get[i] == string_in[o]
get[i] = '*';} 

不要工作; << / p>

2 个答案:

答案 0 :(得分:0)

如果您替换另一个字符串的字符串较长,则情况会有些复杂。这里有简单的功能。

size_t strstrIndex(const char *heap, const char *needle) // returns SIZE_MAX if not found
{
    char *result = strstr(heap,needle);

    return result ? result - heap : SIZE_MAX;
}

char *replace(const char *heap, const char *needle, const char *rep)
{
    size_t pos = 0, nocc = 0;
    size_t len = strlen(heap), nlen = strlen(needle), rlen = strlen(rep);

    char *string;
    char *wstr = (char *)heap;

    while((pos = strstrIndex(wstr, needle)) != SIZE_MAX)
    {
        nocc++;
        wstr += pos + nlen;
    }
    string = calloc(1, len + ((rlen > nlen) ? (rlen - nlen) * nocc : 0) + 1);
    if(string)
    {
        wstr = string;
        while((pos = strstrIndex(heap, needle)) != SIZE_MAX)
        {
            strncpy(wstr, heap, pos);
            heap += pos + nlen;
            wstr += pos;
            strcpy(wstr, rep);
            wstr += rlen;
        }
        if(*heap)
        {
            strcpy(wstr, heap);
        }
    }
    return string;
}

int main()
{
    char *heap = "Spectaclec";

    printf("%s\n", replace(heap, "c", "*"));
    printf("%s\n", replace(heap, "c", "*****"));
    printf("%s\n", replace("ccSpecctaccleccX", "cc", "*****"));
}

答案 1 :(得分:0)

如果使用C库附带的功能,则此任务很简单:

void ReplaceString(char *pTarget, const char *pPattern)
{
    char *p;
    size_t PatternLength = strlen(pPattern);

    // for all occurances of the pattern..      
    while (p = strstr(pTarget, pPattern))
    {
        // The function strstr found an occurance of the pattern.
        // So it must be sufficient space in the target starting at the pointer p..

        // replace the characters in the target
        memset(p, '*', PatternLength);
    }
}

如果出于某些学术目的而避免使用函数,则可以实现自己的strlenstrstrmemset版本。您的示例显示了一个函数string_in,看起来像是`strstr这样的版本。