使用原型在c中用一个字符替换另一个字符

时间:2017-04-08 13:27:53

标签: c string pointers character

#include "stdafx.h"
#include "stdio.h"
void repl(char *string, char oldc, char newc);

char text[60] = { "I am going to replace the character a with the character i";
char newc = 'b';
char oldc = 'a';

int main()
{
    void repl(char *string, char oldc, char newc);

    return 0;

}

void repl(char *string, char oldc, char newc)
{
    int i = 0;
    for (i = 0; i < *string; i++)
    {
        if (*(string + i) == oldc)
        {
            *(string + i) == newc;
        }
    }
    printf("%s", string);
}

我正在尝试将字符a替换为字符b。 我知道如何在不使用指针的情况下执行此操作,但是我不太确定指针是什么。 我得到的原型是:

void repl(char *string, char oldc, char newc);

1 个答案:

答案 0 :(得分:1)

这就是你所追求的。我认真地推荐阅读一些基本的C编程主题,例如调用函数和声明char数组。在此期间......

#include "stdafx.h"
#include "stdio.h"
void repl(char *string, char oldc, char newc);

char text[60] = "I am going to replace the character a with the character i";
char newc = 'i';
char oldc = 'a';

int main()
{
    repl(text, oldc, newc);
    return 0;
}

void repl(char *string, char oldc, char newc)
{
    int i = 0;
    for (i = 0; string[i]; i++)
    {
        if (string[i] == oldc)
        {
            string[i] = newc;
        }
    }
    printf("%s", string);
}