在排除用户输入字符的同时难以将 char 数组字符分配给另一个 char 数组

Difficulty assigning char array characters to another char array while excluding user input character

总的来说,我对 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'; at:

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

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

            newWord[i] = word[i];

        }

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

提前致谢!

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

此代码可以帮助您:

#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");
}

示例:

输入字符串="MLMAZELMLMMLLAMMMAR"

字母='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");
}

示例:

输入字符串(单词 1)="MLMAZELMLMMLLAMMMMAR"

字母='M'

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