- 1 year ago
How can I determine if a char* is a windows line ending?
To determine if a char*
string represents a Windows line ending (CRLF - Carriage Return + Line Feed), you can examine the characters in the string. Here's an example code snippet in C++:
cpp#include <iostream>
bool IsWindowsLineEnding(const char* str)
{
const char* ptr = str;
while (*ptr != '\0')
{
if (*ptr == '\r' && *(ptr + 1) == '\n')
{
return true; // Windows line ending found
}
++ptr;
}
return false; // Windows line ending not found
}
int main()
{
const char* str1 = "Hello\r\nWorld"; // Windows line ending
const char* str2 = "Hello\nWorld"; // Non-Windows line ending
if (IsWindowsLineEnding(str1))
{
std::cout << "String 1 has a Windows line ending." << std::endl;
}
else
{
std::cout << "String 1 does not have a Windows line ending." << std::endl;
}
if (IsWindowsLineEnding(str2))
{
std::cout << "String 2 has a Windows line ending." << std::endl;
}
else
{
std::cout << "String 2 does not have a Windows line ending." << std::endl;
}
return 0;
}
In this example, the IsWindowsLineEnding
function iterates through the characters in the string and checks if each character is a carriage return ('\r'
) immediately followed by a line feed ('\n'
). If such a sequence is found, it indicates a Windows line ending.
You can test the function by passing different char*
strings to the IsWindowsLineEnding
function. If the function returns true
, it means the string has a Windows line ending; otherwise, it does not.
Please note that the example assumes that the input string is null-terminated. Make sure your char*
string is properly terminated with a null character ('\0'
) at the end.