Q:

converting new line character

I am trying to convert new line character  \n to dos style which is \r\n , without using libc.
here is my attempt, what am I doing wrong?
       for(i = 0; str[i]!='\0'; ++i)
{
   if ('\r' == str[i] && '\n'==str[i+1]) 
       ++count;
}

strPtr = malloc(i + 1 + count);

for(i = j = 0; str[i]!='\0'; ++i)
{
   if ('\r' == str[i]) 
   strPtr[j++] = "";
}

strPtr[j] = 0;

output should now be
"Hi\r\n, How are you \r\n, are you okay\r\n"

A:

There are many problems here.  Firstly, you are modifying the string in place using the original buffer.  However, the original buffer does not have enough space to store the additional \r characters.  You'll need to allocate a larger buffer.
Secondly, a UNIX-style carriage return character is not stored as two separate \ and n characters.  It is a single ASCII character with a value of 0xA, which can be represented using the escape sequence \n.  So to check if your current character is a newline character, you want to say strPtr[i] == '\n'
Finally, you are overwriting the old buffer when you say strPtr[i-1] = '\r'.  This will replace the character before the \n, (such as the i in Hi).
Basically, what you want to do is create a second buffer for output, and copy the string character by character to the output buffer.  When you encounter a \n character, instead of copying a single \n to the new buffer, you copy \r\n.  
The size of the output buffer needs to be twice the size of the input buffer to handle the case of an input string where every character is \n, plus 1 for the NULL terminator.  However, you can compute an optimal size for the output buffer by counting the number of \n characters in the original string beforehand.

