Code:
#include <stdio.h>
void conv1 ( const char *str, char *newstr ) {
int lp;
char newchar;
for (lp = 0; str[lp] != '\0' ; lp++) {
if (str[lp] == 's') {
newstr[lp] = '$';
} else {
newstr[lp] = str[lp];
}
}
newstr[lp] = '\0';
}
char *conv2 ( char *str ) {
int lp;
char newchar;
for (lp = 0; str[lp] != '\0' ; lp++) {
if (str[lp] == 's') {
str[lp] = '$';
}
}
return str;
}
int main()
{
char string[256];
char *pstr;
char mstring[256]; // modified string
printf("Enter a string:");
fgets(string,sizeof(string),stdin);
// this doesn't modify the original copy
conv1 ( string, mstring );
printf( "Old=%sNew=%s", string, mstring );
// this does
pstr = conv2 ( string );
printf( "Old=%sNew=%s", string, pstr );
getchar();
return 0;
}
$ ./a.out
Enter a string:this is a test
Old=this is a test
New=thi$ i$ a te$t
Old=thi$ i$ a te$t
New=thi$ i$ a te$t