【问题标题】:Difficulty assigning char array characters to another char array while excluding user input character在排除用户输入字符时难以将 char 数组字符分配给另一个 char 数组
【发布时间】:2020-12-08 19:07:46
【问题描述】:

总的来说,我还是 C 语言和编程的新手。

作为家庭作业,我需要制作一个程序,让用户输入一个单词,然后输入一个字母,该程序打印出这些字母在该单词中的位置,并打印出没有这些字母的单词(这就是我在正在苦苦挣扎)。

下面是代码:

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


int main()
{
    char word[20];

    printf("Enter a word: ");
    scanf("%s", word);

    int length = strlen(word);

    char letter[1];
    printf("Enter a letter: ");
    scanf("%s", letter);

    char newWord[20];

    for(int i = 0; i < length; i++){

        if(word[i] != letter[0]){

            newWord[i] = word[i];

        } else
        if(word[i] == letter[0]){

            printf("%s is at %d\n", letter, i+1);
            //newWord[i] = word[i+1];  // I've commented this out because I was testing the first part 
                                       // of the loop which is assigning those letters to the variable
                                       // and yet still can't think of an idea that skips that letter 
                                       // I want it to skip.

        }

    }

    printf("%s", newWord);

}

基本上我从上次打印printf("%s", newWord); 得到的只是空白。但我想要它做的是例如我输入“worlrdr”和“r”,所以它打印“wold”。甚至像newWord[i] = 'z'; 这样基本的东西:

for(int i = 0; i < length; i++){

        if(word[i] != letter[0]){

            newWord[i] = word[i];

        }

打印出“zz`zz(一些奇怪的字符)”。我真的很困惑,如果有人能解释一下,我将不胜感激。

谢谢你!

【问题讨论】:

  • 如果将有效字符复制到与原始字符相同的位置,则会出现未初始化数据的间隙。您将需要两个索引:如果从“beer”中去掉“e”,则位置 3 的“r”将位于新字符串中的位置 1。确保以空字符 '\0' 终止新字符串,使其成为正确的 C 字符串。
  • (顺便说一下,你不能扫描到长度为1的数组,因为scanf想在最后放置一个空字符。你也不知道用户是否只输入了一个信。)
  • @MOehm 哦,对了。这就解释了为什么如果我只使用 newWord[i] = 'z'; 会得到奇怪的字符
  • 正如我在your other question 中已经指出的,最好写char letter; 而不是char letter[1];,写scanf(" %c", &amp;letter); 而不是scanf("%s", letter);。
  • @AndreasWenzel 是的!谢谢!

标签: arrays c char assign behavior


【解决方案1】:

我已经粉碎(删除)在字符串中找到的所有字母并将字符从长度转移到找到的字母索引

这段代码可以帮助你:

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

int main()
{
    char word[20];
    printf("Enter a word: ");
    scanf(" %s", word);
    int length = strlen(word);
    char letter;
    printf("Enter a letter: ");
    scanf(" %c", &letter);
    
    for(int i=0;i<length;i++)
    {
        if(word[i]== letter )
        {
             printf("%c is at %d\n", letter, i+1);
        }
    }

     //here is the for loop of crushed
    for(int i=0;i<length;i++)
    {
        if(word[i]== letter )
        {
            for(int j=i+1;j<length;j++)
            {
                  word[j-1]=word[j];
            }
            length--;
            i--;
        }
    }

    printf("\nDisplay of the string :\n\n");

    for(int i=0;i<length;i++)
    {
         printf("%c", word[i]);
    }
    printf("\n");
}

例子:

输入字符串="MLMAZELMLMMLLAMMMMAR"

字母 ='M'

输出字符串:“LAZELLLLAAR”

或者,您可以使用第二个字符串将单词 1 的字母复制到单词 2,除了与给定字母相同的字符

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

int main()
{
    char word[20];
    printf("Enter a word: ");
    scanf(" %s", word);
    int length = strlen(word);
    char letter;
    printf("Enter a letter: ");
    scanf(" %c", &letter);
    
    for(int i=0;i<length;i++)
    {
        if(word[i]== letter )
        {
              printf("%c is at %d\n", letter, i+1);
        }
    }
    char word2[20];
    int j=0;
 
    for(int i=0;i<length;i++)
    {
        if(word[i]!= letter )
        {
            word2[j]=word[i];
            j++;
        }
    }
 
    printf("\nDisplay of the new string(word 2) :\n\n");

    for(int i=0;i<j;i++)
    {
         printf("%c", word2[i]);
    }
    printf("\n");
}

例子:

输入字符串(word 1)="MLMAZELMLMMLLAMMMMAR"

字母 ='M'

输出字符串(单词 2):“LAZELLLLAAR”

【讨论】:

  • 非常感谢,这真的帮助我表达了我脑海中关于内部循环的想法,但很难做到!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-25
  • 1970-01-01
  • 1970-01-01
  • 2011-02-07
  • 1970-01-01
  • 2014-06-08
  • 2012-10-16
相关资源
最近更新 更多