[英]Replacing characters in strings in C
我最近开始使用 C,但在理解“&”和“*”的用法以及将它们放在我的代码中的位置时遇到了麻烦。 我当前的任务要求我接受两个输入,一个是用户想要交换的字符串中的字符,另一个是用来替换它的新字符。 我不允许使用 String 库。 这是我当前的代码,但由于某种原因,输出永远无法访问“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 的值,即 a 的内存地址。 在第二个打印中,我们取消引用p,它为我们提供了存储在该内存地址的数据——int a 的值。
还有其他使用指针的方法。 创建数组时...
int arr[10];
...您正在隐式地创建一个指向数组中第一个元素的指针(“arr”)。 您可以通过多种方式访问数组中的元素。
arr[5]
*(arr + 5)
两者都通过查看 arr(第一个元素的内存地址)并加上 5 来获得数组的第 5 个元素,这为我们提供了第 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
读取最多两个字符。 如果choice[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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.