Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C中替换字符串中的字符_C_String - Fatal编程技术网

在C中替换字符串中的字符

在C中替换字符串中的字符,c,string,C,String,我最近开始使用C,在理解“&”和“*”的用法以及在代码中放置它们的位置时遇到困难。当前的分配要求我接受两个输入,一个是用户希望交换的字符串中的字符,另一个是要替换的新字符。我不允许使用字符串库。这是我当前的代码,但由于某种原因,输出永远无法访问“replacementchar[0]”,我不知道为什么。任何帮助都将不胜感激。谢谢大家! if(response[0] == yes[0]){ char replacechar[1]; printf("What chara

我最近开始使用C,在理解“&”和“*”的用法以及在代码中放置它们的位置时遇到困难。当前的分配要求我接受两个输入,一个是用户希望交换的字符串中的字符,另一个是要替换的新字符。我不允许使用字符串库。这是我当前的代码,但由于某种原因,输出永远无法访问“replacementchar[0]”,我不知道为什么。任何帮助都将不胜感激。谢谢大家!

if(response[0] == yes[0]){
        char replacechar[1];
        printf("What character do you want to replace: ");
        scanf("%s", replacechar);

        char newchar[1];
        printf("What is the replacement character: ");
        scanf("%s", newchar);

        printf("you want to replace %c with %c \n", replacechar[0], newchar[0]);
        for(int i = len-1; i > -1; --i){
            if(word[i] == replacechar[0] && word[i] != '\0'){
                printf("found one\n");
                word[i] = newchar[0];
            }
        }
    }

在C中,*和&是指针运算符。*用于创建指针变量并取消引用它们用于获取某物的内存地址

下面的代码(1)创建int a,(2)使用*创建int指针p,(3)使用&将指针p的值设置为a的内存地址

int a;
int *p;
p = &a;

printf("%d", p);
printf("%d", *p);
第一次打印将返回p的值,p是a的内存地址。在第二次打印中,我们取消了对p的引用,它给出了存储在该内存地址的数据——int a的值

还有其他使用指针的方法。创建数组时

int arr[10];
…您正在隐式创建指向数组中第一个元素的指针(“arr”)。可以通过多种方式访问数组中的元素

arr[5]
*(arr + 5)
两者都通过查看arr(第一个元素的内存地址)并添加5来获得数组的第五个元素,5给出了第五个元素的内存地址,然后用*解引用以获得第五个元素的值

您的代码错误不是因为误用了&和*,而是因为scanf(您使用的)的行为。Scanf写入缓冲区,然后必须复制该缓冲区才能保存值。另外,考虑到您只扫描单个字符,因此不需要使用字符数组。查看此解决方案:

char buf, replacechar, newchar;
printf("What character do you want to replace: ");
scanf("%c", &buf);
replacechar = buf;

printf("What is the replacement character: ");
scanf("%s", &buf);
newchar = buf;

printf("You want to replace %c with %c\n", replacechar,
newchar);
for (int i = len-1; i > -1; --i) {
    if (word[i] == replacechar && word[i] != '\0') {
    printf("Found one!\n");
    word[i] = newchar;
    }
}

在C中,*和&是指针运算符。*用于创建指针变量并取消引用它们用于获取某物的内存地址

下面的代码(1)创建int a,(2)使用*创建int指针p,(3)使用&将指针p的值设置为a的内存地址

int a;
int *p;
p = &a;

printf("%d", p);
printf("%d", *p);
第一次打印将返回p的值,p是a的内存地址。在第二次打印中,我们取消了对p的引用,它给出了存储在该内存地址的数据——int a的值

还有其他使用指针的方法。创建数组时

int arr[10];
…您正在隐式创建指向数组中第一个元素的指针(“arr”)。可以通过多种方式访问数组中的元素

arr[5]
*(arr + 5)
两者都通过查看arr(第一个元素的内存地址)并添加5来获得数组的第五个元素,5给出了第五个元素的内存地址,然后用*解引用以获得第五个元素的值

您的代码错误不是因为误用了&和*,而是因为scanf(您使用的)的行为。Scanf写入缓冲区,然后必须复制该缓冲区才能保存值。另外,考虑到您只扫描单个字符,因此不需要使用字符数组。查看此解决方案:

char buf, replacechar, newchar;
printf("What character do you want to replace: ");
scanf("%c", &buf);
replacechar = buf;

printf("What is the replacement character: ");
scanf("%s", &buf);
newchar = buf;

printf("You want to replace %c with %c\n", replacechar,
newchar);
for (int i = len-1; i > -1; --i) {
    if (word[i] == replacechar && word[i] != '\0') {
    printf("Found one!\n");
    word[i] = newchar;
    }
}

除了使用
%s
%c
getchar
scanf
之外,另一种方法是使用
fgets
。此处
fgets
将从
stdin
读取最多两个字符。如果
选项[1]
是换行符,则只输入了另一个字符。否则,清除
stdin
并提示重试。
replacement
函数通过字符串的每个字符向前移动指针
text
,直到指向
*text
的字符为终止零为止

#include <stdio.h>
#include <stdlib.h>

char fgetschar ( void) {
    char choice[3] = "";

    do {
        if ( fgets ( choice, sizeof choice, stdin)) {//read two characters
            if ( '\n' == choice[0]) {//only newline
                printf ( "\nenter a character\n\ttry again\n");
                continue;
            }
            if ( '\n' != choice[1]) {//did not find expected newline
                while ( fgets ( choice, sizeof choice, stdin)) {
                    if ( '\n' == choice[0] || '\n' == choice[1]) {
                        break;
                    }
                }
                choice[1] = 0;//to make the loop repeat
                printf ( "\nenter one character only\n\ttry again\n");
            }
        }
        else {
            fprintf ( stderr, "fgets EOF\n");
            exit ( EXIT_FAILURE);
        }
    } while ( choice[1] != '\n');

    return choice[0];
}

int replacement ( char *text, char find, char sub) {
    int count = 0;

    while ( text && *text) {//not NULL and not terminating zero
        if ( *text == find) {
            *text = sub;
            ++count;
        }
        ++text;//advance to next character
    }

    return count;
}

int main ( void) {
    char replacechar = 0;
    char newchar = 0;
    char word[] = "a b c d e f g h i j k l m n o p q r s t u v w x y z";

    int size = 21;
    int item = rand ( ) % ( size - (int)( size * 0.6));
    printf ( "item %d\n", item);

    printf ( "What character do you want to replace: ");
    fflush ( stdout);
    replacechar = fgetschar ( );
    printf ( "replace character: %c\n\n", replacechar);

    printf("What is the replacement character: ");
    fflush ( stdout);
    newchar = fgetschar ( );
    printf ( "replacement character: %c\n\n", newchar);

    int found = replacement ( word, replacechar, newchar);

    printf ( "found %d instances of %c\n", found, replacechar);

    printf ( "%s\n", word);

    return 0;
}
#包括
#包括
char fgetschar(无效){
字符选择[3]=“”;
做{
if(fgets(choice,sizeof choice,stdin)){//读取两个字符
如果('\n'==选项[0]){//仅换行
printf(“\n请输入一个字符\n\t再试一次\n”);
继续;
}
如果('\n'!=choice[1]){//未找到预期换行符
while(fgets(choice,sizeof choice,stdin)){
如果('\n'==选项[0]| |'\n'==选项[1]){
打破
}
}
选择[1]=0;//使循环重复
printf(“\n只输入一个字符\n\t重试\n”);
}
}
否则{
fprintf(标准术语,fgets EOF);
退出(退出失败);
}
}while(选项[1]!='\n');
返回选择[0];
}
整数替换(字符*文本、字符查找、字符子){
整数计数=0;
而(text&&*text){//不为空且不以零结尾
如果(*文本==查找){
*text=sub;
++计数;
}
++text;//前进到下一个字符
}
返回计数;
}
内部主(空){
char-replacechar=0;
char newchar=0;
字符字[]=“a b c d e f g h i j k l m n o p q r s t u v w x y z”;
int size=21;
整数项=rand()%(大小-(整数)(大小*0.6));
printf(“项目%d\n”,项目);
printf(“要替换的字符:”);
fflush(stdout);
replacechar=fgetschar();
printf(“替换字符:%c\n\n”,replacechar);
printf(“替换字符是什么:”);
fflush(stdout);
newchar=fgetschar();
printf(“替换字符:%c\n\n”,newchar);
int found=replacement(word、replacechar、newchar);
printf(“找到%d个%c\n的实例”,找到,replacechar);
printf(“%s\n”,word);
返回0;
}

除了
getchar
scanf
使用
%s
%c
之外,另一种方法是使用
fgets
。此处
fgets
将从
stdin
读取最多两个字符。如果
选项[1]
是换行符,则只输入了另一个字符。否则,清除stdin并提示tr