C++ query whether a string or char ends with a specific character?
The Solution is Simple: Checking if a String or Character Array Ends with a Certain Substring in C++
The provided C++ functions are designed to perform operations related to filenames and file extensions. Below is an explanation of the functions with comments in English.
1.) Example 1: Checking if a String Ends with a Specific Substring
These functions allow checking and extracting filenames and file extensions. Ends_With_String checks if a given string ends with a specific suffix. Is_File_Extension_Check verifies if a file has the specified file extension. Query_File_Extension returns the file extension of a file if found in the file path. Note that _tcsrchr is used to find the file extension in the path.
#include <tchar.h>
// Checks if the given string// ends with the specified suffix
BOOL Ends_With_String(LPCTSTR string, LPCTSTR suffix){
int len1 = lstrlen(string);
int len2 = lstrlen(suffix);
if (len2 > len1)
return FALSE;
// If the suffix is longer than// the input string, it cannot be at the end.// Compares the part of the string// before the suffix (ignores case).
if (lstrcmpi(string + len1 - len2, suffix) ==0)
return TRUE; // The string ends with the given suffix.
return FALSE;
}// Checks if the file has the specified file extension
BOOL Is_File_Extension_Check(LPCTSTR file_path, LPCTSTR file_extension){
LPCTSTR lpsz = _tcsrchr(file_path, (TCHAR)_T("."));
if (lpsz <=0)
return FALSE; // If no file extension is found in the path.// Compares the found file extension// with the given extension (ignores case).
if (lstrcmpi(lpsz, file_extension) ==0)
return TRUE; // The file has the specified extension.
return FALSE;
}// Returns the file extension of a file
LPCTSTR Query_File_Extension(LPCTSTR file_path){
LPCTSTR lpsz = _tcsrchr(file_path, (TCHAR)_T("."));
if (lpsz <=0)
return NULL;
// If no file extension is found in the path.// Returns the pointer to the part of the path// that contains the file extension (excluding the dot).
return lpsz + 1;
}
2.) Example 2: Checking if a String Ends with a Specific Substring
The following C code implements a function named endsWith that checks if a given string matches a certain suffix. The function can consider case sensitivity if needed. The main part of the code demonstrates the usage of this function with various examples and outputs, showing how the function successfully determines whether a string ends with a given suffix, with or without case sensitivity.
#include <stdio.h>
#include <string.h>
// Function to check if a string ends with a specific suffix
int endsWith(const char *fullString, const char *suffix) {// Calculate the lengths of the strings
size_t fullStringLength = strlen(fullString);
size_t suffixLength = strlen(suffix);
// Check if the suffix is longer than the entire string
if (suffixLength > fullStringLength) {
return 0; // The suffix is longer than the entire string}// Comparison considering case sensitivity// return strcmpi(fullString + fullStringLength - suffixLength, suffix) == 0;// Comparison with case sensitivity
return strcmp(fullString + fullStringLength - suffixLength, suffix) ==0;
}
int main() {
const char *str = "Hello, World!";
const char *suffix = "World!";
// Check if the string ends with the suffix "World!"
if (endsWith(str, suffix)) {
printf("The string ends with \"%s\".\n", suffix);
} else {
printf("The string does not end with \"%s\".\n", suffix);
}
printf("\n");
// Test a new suffix (lowercase)
suffix = "world!";
if (endsWith(str, suffix)) {
printf("The string ends with \"%s\".\n", suffix);
} else {
printf("The string does not end with \"%s\".\n", suffix);
}
printf("\n");
return 0;
}/*
Output:
The string ends with "World!".
The string does not end with "world!".
Press any key to continue
*/
This example demonstrates the simplicity and effectiveness of checking if a string ends with a specific substring, taking into account case sensitivity when needed.
It is easy to publish existing desktop applications in the MS Store using the Desktop Bridge Info: You can convert your existing desktop application into
The solution Microsoft Visual Studio version overview, where can I find it? In Arbeit Satz1=Microsoft Visual Studio version overview, where can I find it?
The SDF file is the so-called code browsing database that SQL Server Compact Edition uses If you search for the SDF on the Internet, you will see that it
If you want to present to a large audience and show your game or graphics program in its best light, it is important that you include the Ultrabooks. These
You will find it is easy to program a 3D maze with OpenGL and C++ under Windows I have found that I have written a large number of interesting and perhaps
This website does not store personal data. However, third-party providers are used to display ads, which are managed by Google and comply with the IAB Transparency and Consent Framework (IAB-TCF). The CMP ID is 300 and can be individually customized at the bottom of the page. more Infos & Privacy Policy ....