0

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!

Alex
  • 39
  • 5
  • 1
    If you copy the valid characters to the same position as they are in the original, you will get gaps of uninitialized data. You'll need two indices: if you strip out "e" from "beer", the "r" at position 3 will be in position 1 in the new string. Make sure to terminate the new string with the null character `'\0'`, so that it is a proper C string. – M Oehm Dec 08 '20 at 19:11
  • (By the way, you cannot scan into an array of length 1, because `scanf` wants to place a null character at the end. You also don't know wheter the user just inputs a single letter.) – M Oehm Dec 08 '20 at 19:15
  • @MOehm Oh riiight. That explains why I got weird characters if I just used ```newWord[i] = 'z';``` – Alex Dec 08 '20 at 19:20
  • As I have already pointed out in [your other question](https://stackoverflow.com/q/65191581/12149471), it would be better to write `char letter;` instead of `char letter[1];` and to write `scanf(" %c", &letter);` instead of `scanf("%s", letter);`. – Andreas Wenzel Dec 08 '20 at 19:52
  • @AndreasWenzel yeah got that part! thanks! – Alex Dec 08 '20 at 20:11

1 Answers1

1

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"

MED LDN
  • 684
  • 1
  • 5
  • 10
  • Thanks a bunch this really helped me express the idea I've had in my mind with inner loop but had a hard time doing so! – Alex Dec 08 '20 at 21:23