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!