Help with removing \n from a string in C

void remove_new_line(char *s) {
    while (*s) {
        if (*s == '\n') {
            *s='\0';
        }
        s++;
    }
}
// i tried this code for removing a \n from a string but its not working

Code looks ok...so only explanation is that s doesnt contain a newline.

also after i do this, when i print it , nothing comes up, i dont want to create a new string to fix this, what is wrong?

Create a pointer variable and initialize it to s before entering the loop and print out t before returning from the function...

void remove_new_line(char *s) {
    char *t = s;
    while (*s) {
        if (*s == '\n') {
            *s='\0';
        }
        s++;
    }
    printf("string: t\n", t);
}

also how do i find out the length of the string excluding the \n at the end and the \0 at the end.
i am using

int str_len (char *s) {
    int count = 0;
    while ((char) *s != '\n' && (char) *s != '\0') {
        printf("%c",*s);
        s++;
        count++;
    }
    return count;
}

but when getting the lengths of strings i got from fgets, it gives me an extra count for all words except the last word. what is wrong with this?

Do a man on strlen.

char line[11];
FILE *fr;
fr = fopen(argv[1], "r");
    while(fgets(line, 11, fr) != NULL) {
        printf("length = %d\n",strlen( line ));
        printf(line);
    }
 fclose(fr); 

this is in the main function
and it reads from

helloa
bye
cake
portald
birthday
portale
hellob

this is the contents of the file
and the out put is
length = 8
helloa
length = 5
bye
length = 6
cake
length = 9
portald
length = 10
birthday
length = 9
portale
length = 6
hellob

so yeah something is still wrong, but what?

You may have hidden characters in the input file...do a cat -vet filename and post the outputhere...

cat -vet file

cat -vet new

helloa^M$
bye^M$
cake^M$
portald^M$
birthday^M$
portale^M$
hellob

The output is correct and as expected since the file appears to have been created on an MSDOS-like system, i.e. one that uses CRLF instead of LF to represent newlines. The only anomaly is the last line which does not have a newline.

i figured out whats wrong, i ad to use the dos to unix command to make it work.

Or you can do something the following:

#include <stdio.h>

void
remove_new_line(char *s) {

    char *t = s;
    int n = 0;

    while (*s) {
        if (*s == '\n' || *s == '\r') {
            *s='\0';
            break;
        }
        s++;
        n++;
    }

    printf("string: %s (%d)\n", t, n);
}


int
main(int argc, char *argv[])
{

    char line[11];
    FILE *fr;

    fr = fopen(argv[1], "r");

    while(fgets(line, 11, fr) != NULL) {
        remove_new_line(line);
    }

    fclose(fr);
}

which gives the following output:

string: helloa (6)
string: bye (3)
string: cake (4)
string: portald (7)
string: birthday (8)
string: portale (7)
string: hellob (6)

If there's a newline at the beginning of the string, it will eat it too, truncating the entire thing.