If you truly want to remove the newline (not replace it with a space) here are a few different ways to do it. Each of the functions below is made more generic by allowing you to remove a specified character and letting the user know how many characters were removed.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int
remove_char1(char *dest,char c)
{
int removed=0;
char *tmp;
while (*dest) {
tmp=strchr(dest,c);
if (NULL==tmp) {
break;
} else {
size_t len=strlen(tmp+1);
memmove(tmp,tmp+1,len);
tmp[len]=0;
++removed;
dest=tmp;
}
}
return removed;
}
This one changes the string in place. Generally this is the way *not* to do it due to inefficiency (calling memmove() for each occurance of c) and its only advantage is that you don't have to use another variable.
char *
remove_char2(const char *src,char c,int *removed)
{
char *dest=malloc(strlen(src)+1);
char * const start=dest;
*removed=0;
if (dest!=NULL) {
while (*src) {
if (*src!=c) {
*dest++=*src;
} else {
++(*removed);
}
++src;
}
*dest=0;
}
return start;
}
This is an improvement. The function dynamically allocates a buffer large enough to hold the entire source string and then only copies in the characters that do not match c. The disadvantage is that the user must call free() on the pointer returned eventually. Since the function returns the result string, the # of characters removed is stuffed in a pointer provided by the user.
int
remove_char3(const char *src,char *dest,char c)
{
int removed=0;
while (*src) {
if (*src!=c) {
*dest++=*src;
} else {
++removed;
}
++src;
}
*dest=0;
return removed;
}
This one stores the result in a user-supplied pointer that is assumed to point to enough memory to hold the result string and is similar to how the standard library functions behave.
HTH,
Russ
bobbitts@hotmail.com