You should create a complete simplified version of the code to post. Also be sure to use proper indentation.
Code:
#include <stdio.h>
#include <string.h>
int main(void)
{
int removedChar;
printf("Enter what character you wish to remove\n\n");
removedChar = getchar();
getchar();
printf("Your char is %c:\nEnter a text:\n\n", removedChar);
char str1[50], str2[50];
fgets(str1, 50, stdin);
strcpy(str2, str1);
int i, j, n;
n = strlen(str1);
for (i = 0; i<n; i++);
{
if (str1[i] != removedChar)
{
str2[j] = str1[i];
j++;
}
}
printf("your text is:\n%s", str2);
return 0;
}
Make sure you heed the warnings:
Code:
main.c|28|warning: 'j' may be used uninitialized in this function
You have a stray semi-colon after the "for" which needs to be removed. As it is, your code is actually doing this:
Code:
for (i = 0; i<n; i++)
;
{
if (str1[i] != removedChar)
{
str2[j] = str1[i];
j++;
}
}
There's no need to copy str1 into str2 if you're just going to be writing to str2 one element at a time.
Also be sure str2 is properly terminated.