ok I have now made some changes to the function to process the incoming string write each element to an output string and copy the updated string back to original.
However when it runs I get out as follows
Hell?
where does the question mark come from?
can any one offer me some concise accurate answer?
can anyone comment constructively on whether this is a good way of handling this scenario and offer advise?
many thanks and here is the code
Code:
#include <stdio.h>
#include <strings.h>
void remchar(char *strin, char chr);
int main()
{
char s[10]={'H','e','l','l','o'}; //init a char string
char c = 'o'; //character var to pass to function to look for
printf("\nstring before function call\t:>%s", s); //print the string pre call
remchar(s, c); //call remchar func
printf("\nstring after function call\t:>%s\n", s); //string post call
return 0;
}
void remchar(char *strin, char chr)
{
int i, j=0;
char cupdate[10]; //temp storage for updated string
for(i = 0; strin[i] != '\0'; i++) //start for loop
{
if(strin[i] != chr) //check its not the char i want
{
cupdate[j] = strin[i]; //copy element to temp string
j++; //move along the index one
}
else
{
; //do nothing
}
}
j++; //move along the index one
cupdate[j] = '\0'; //terminate the string
printf("strin = %s\tcupdate = %s", strin, cupdate); //show what its doing
strcpy(strin, cupdate); //copy updated string back to original
}