文字列の\n
はエスケープ シーケンスであり、1 文字で表されます。
コードは次のようになります。
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);
}
編集
質問を変更することにしたので (ところで - 明確にするために質問に追加するだけで、元の OP の巨大なチャンクを削除しないでください。回答は将来の訪問者にとって意味をなさないため) - コードは次のとおりです。
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.