Search code examples
arraysccharassignbehavior

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


I'm still new to C language and programming in general.

As a homework assignment I need to make a program that lets the user input a word then a letter in which this program prints out positions of those letters in that word and prints out this word without those letters (that's where I'm struggling right now).

Here is the code right now:

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

}

Basically what I get from the last print printf("%s", newWord); is nothing just blank. But what I want it to do is for example I enter "worlrdr" and "r" and so it prints "wold". Even something basic like newWord[i] = 'z'; at:

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

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

            newWord[i] = word[i];

        }

prints out "zz`zz(a few weird characters)". I'm really really confused and I would appreciate if someone could explain me this.

Thank you in advanced!


Solution

  • I have crushed(remove) all the letters found in the string and shifted the characters from length to index of letter found

    This code can help you :

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

    Example :

    Input string="MLMAZELMLMMLLAMMMMAR"

    the letter ='M'

    Output string:"LAZELLLLAAR"

    Or ,you can use second string to copy the letters of word 1 to word 2 except the characters which are identical with the given letter

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

    Example :

    Input string(word 1)="MLMAZELMLMMLLAMMMMAR"

    the letter ='M'

    Output string(word 2):"LAZELLLLAAR"