user1888502
user1888502

Reputation: 373

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"

Upvotes: 1

Views: 3296

Answers (4)

Ed Heal
Ed Heal

Reputation: 60007

The \n in your string is an escape sequence and is represented by one character.

Your code should be like this:

int main(void)
{
    char str[] = "Hi\n, How are you \n, are you okay\n";
    char *strPtr = str;

    int i, j;
    int count=0;

    for(i = 0; str[i]!='\0'; ++i)
    {
       if (`\n` == str[i]) ++count;
    }
    strPtr = malloc(i + 1 + count);
    for(i = j = 0; str[i]!='\0'; ++i)
    {
       if ('\n' == str[i]) strPtr[j++] = `\r`;
       strPtr[j++] = str[i];
    }
    strPtr[j] = 0;

    printf("This many times we changed it", count);
}

EDIT

As you have decided to change the question (BTW - Just add to the question for clarification and not delete huge chunks of the original OP as the answers will not make any sense for future visitors) - here is the code:

int main(void)
{
    char str[] = "Hi\r\n, How are you \r\n, are you okay\r\n";

    int i, j;
    for (i = j = 0; 0 != str[i]; ++i)
    {
       if ('\r' == str[i] && '\n' == str[i + 1])
       {
          ++count;
       }
       else
       {
          str[j++] = str[i];
       }
    }
    str[j] = 0;

    .. etc - str is without \r\n but \n, count is the number of lines.

Upvotes: 0

rashok
rashok

Reputation: 13434

All the escape sequence characters in C language are one character which is stored in one byte of memory only, dont consider it as two.

So you can directly check for a byte to \n as you are checking for \0.

If you want to replace \n(1 character) with \r\n(2 character) means, str should have additional memory, but in your program its not having additional memory.

char *a = "\n"; //This is one byte
char *b = "\\\n"; //This is two byte, 1st byte for '\' and 2nd byte for new line
char *c = "\\\r\n"; //Similarly this is three byte
char *c = "\r\n"; //Similarly this is two byte

All the below escape sequence characters are single byte character in C language.

\n – New line
\r – Carriage return
\t – Horizontal tab
\\ – Backslash
\' – Single quotation mark
\" – Double quotation mark

Upvotes: 1

Charles Salvia
Charles Salvia

Reputation: 53289

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.

Upvotes: 4

PQuinn
PQuinn

Reputation: 1022

You can't do this in-place. You're adding a new character ('\r') for every '\n' which means the string must expand. The worst case scenario is that every character is a '\n' which means we would double the size of the string. Thus, let's make a buffer twice the size of the original string.

strtmp = malloc(strlen(str) * 2 + 1); /* +1 for null */
strcpy(strtmp, str);
strptr = strtmp;

for (i = 0; str[i] != 0; i++)
{
    if ((str[i] == '\\') && (str[i+1] == 'n'))
    {
        *strptr++ = '\\';
        *strptr++ = 'r';
    }

    *strptr++ = str[i];
}

printf(strtmp);
free(strtmp);

Upvotes: 0

Related Questions