If I stick to using strcpy to copy ... notice how here I can't and even strncpy can't work
instead I would need concatenation of this new character to the rest of the string.
I also tried char backup[strlen(s)+1]; it fails too.
to try again..
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void replaceFirstCharacter(char *s,char replaceWith);
int main()
{
char *x;
char t[10]= "telephone";
x=(char *)malloc( sizeof(char) *10 ); /*9 characters plus null byte.*/
strcpy(x,t);
/*so that it works with any string... changing the first character only*/
replaceFirstCharacter(x,'z');
printf("%s\n",x);
return 0;
}
void replaceFirstCharacter(char *s,char replaceWith)
{
char *backup;
if (strlen(s) >=1)
{
backup=(char *)malloc( sizeof(char) * (strlen(s)+1));
strcpy(backup,s);
backup[0]=replaceWith; /*I guess I should somehow avoid this?*/
strcpy(s,backup);
}
}