【问题标题】:replace a character element of an array for another character entered by user input将数组的字符元素替换为用户输入的另一个字符
【发布时间】:2017-02-17 10:39:26
【问题描述】:

我有一个充满 * 字符的数组。我需要将数组中的任何字符替换为用户输入的另一个字符。可能吗?我会很感激任何建议。谢谢

    #include <stdio.h>
    #include <string.h>

    strreplace(char[],char,char);
    int main()
    {
        char s[17]="* * * *\n* * * * ";
        char chr,repl_chr;
        printf("%s", s);
        printf("\nEnter character to be replaced: ");
        chr=getchar();
        fflush(stdin);
        printf("\nEnter replacement character: ");
        repl_chr=getchar();
        printf("\nModified string after replacement is: \n");
        strreplace(s,chr,repl_chr);
        getch();
        return 0;
   }



   strreplace(char s[], char chr, char repl_chr)
   {
      int i=0;
      while(s[i]!=' ')
      {
           if(s[i]==chr)
           {
               s[i]=repl_chr;
           }
           i++;
      }
      puts(s);
      return 0;
  }

【问题讨论】:

  • 正确理解:您希望所有次出现的第一个输入字符替换为第二个输入字符?例如,如果用户输入*- 替换,那么程序应该输出"- - - -\n- - - - ",是吗?
  • 但是有一个问题:因为 char 数组只包含可替换的 *strreplace 显然会忽略空格),所以用户输入的任何其他字符(例如 ab, c, -, ...) 在该数组中找不到,因此无法替换任何内容。
  • 并不是我的显示应该看起来像两行四列的那些 - 符号。使用可以替换数组中的任何字符。假设用户想用 B 替换第三个 - 那么它看起来像这样 - - B - \n - - - -。他必须输入他想在数组中为他想要的另一个字符替换哪个元素。
  • 这当然是一个关键信息:因此用户不必指定要替换的字符,而是指定该字符在源字符串中的坐标是什么。
  • 是的!可以这样做吗?还是有其他方法可以做到这一点?

标签: c arrays input replace character


【解决方案1】:

更改您的strreplace,以便将参数chr 转换为索引。

int strreplace(char s[], char chr, char repl_chr)
{
    int i = 0;

    // Convert the input char into an index.
    // Note that 1 is subtracted to make the index zero-based.
    // Remember that the user entered a one-based index.
    int index = chr - '0' - 1;
    if ((index < 0) || (index >= 8))
    {
        // Out of bounds.
        return -1;
    }
    while (s[i] != '\0')
    {
        if ((i/2) == index)
        {
            s[i] = repl_chr;
        }

        // Note: since every second char is to get ignored,
        // the increment must actually be 2 here.
        i += 2;
    }
    puts(s);
    return 0;
}

【讨论】:

  • 输入3 作为第一个字符,B 作为第二个字符产生输出* * B *\n* * * *
【解决方案2】:

尝试将循环中的while(s[i]!=' ') 更改为
while (s[i] != '\0') 以检查是否已到达字符串末尾

int strreplace(char s[], char chr, char repl_chr);

 int main(void)
 {
     char s[17] = "* * * *\n* * * * ";
     char chr, repl_chr;

     printf("%s", s);

     printf("\nEnter character to be replaced: ");
     scanf("%c", &chr);

     printf("\nEnter replacement character: ");
     scanf(" %c", &repl_chr);
     printf("\nModified string after replacement is: \n");

     strreplace(s, chr, repl_chr);

     return 0;
 }

 int strreplace(char s[], char chr, char repl_chr)
 {
     int i = 0;

     while (s[i] != '\0') {
         if (s[i] == chr)
             s[i] = repl_chr;
         i++;
     }

     puts(s);
     return 0;
 }

输出:

* * * *
* * * * 
Enter character to be replaced: *

Enter replacement character: B

Modified string after replacement is: 
B B B B
B B B B

【讨论】:

    猜你喜欢
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 2021-01-25
    • 1970-01-01
    • 2017-01-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多