Home | API | MFC | C++ | C | Previous | Next

Programming Windows API

String Manipulation Functions

The string manipulation functions of the Win32 API allow an application to test and manipulate a strings contents. A selection of these is listed below. For a full list of string manipulation functions – https://docs.microsoft.com/en-us/windows/win32/menurc/string-functions

CharLower

Translates a character string to lowercase. 

LPSTR CharLower(LPSTR lpsz);

Where lpsz is a null-terminated string, or specifies a single character. If the operand is a character string, the function returns a pointer to the converted string.

CharNext

Retrieves a pointer to the next character in a string. The prototype for this function is

LPSTR CharNext(LPCSTR lpsz);

Where lpsz is a character in a null-terminated string. The return value is a pointer to the next character in the string, or to the terminating null character if at the end of the string.

CharPrev

Positions pointer to the previous character in a string. 

LPSTR CharPrev(LPCSTR lpszStart,LPCSTR lpszCurrent);

where
LpszStart – The beginning of the string.
LpszCurrent – A character in a null-terminated string.
The return value is a pointer to the preceding character in the string, or to the first character in the string

CharUpper

Converts a character string to uppercase. The prototype for this function is

LPSTR CharUpper(LPSTR lpsz);

Where lpsz is a null-terminated string, or a single character. If the operand is a character string, the function returns a pointer to the converted string.

IsCharAlpha

Determines whether a character is an alphabetical character.

BOOL IsCharAlpha(CHAR ch);

Where ch is the character to be tested. If the character is alphabetical, the return value is nonzero. If the character is not alphabetical, the return value is zero.

IsCharAlphaNumberic

Determines whether a character is an alphanumeric character.

BOOL IsCharAlpha(CHAR ch);

Where ch is the character to be tested. If the character is alphanumeric, the return value is nonzero. If the character is not alphanumeric, the return value is zero.

IsCharLower

Determines whether a character is lowercase.

BOOL IsCharLower(CHAR ch);

Were ch is the character to be tested. If the character is lowercase, the return value is nonzero. If the character is not lowercase, the return value is zero.

IsCharUpper

Determines whether a character is uppercase.

BOOL IsCharLower(CHAR ch);

Where ch is the character to be tested. If the character is uppercase, the return value is nonzero. If the character is not uppercase, the return value is zero.

Lstrlen

Determines the length of the specified string excluding the terminating null character.

int lstrlen(LPCSTR lpString);

Where lpString is the null-terminated string to be checked.
The function returns the length of the string, in characters.

Example

The following short program diplays the various string manipulation options in a simple window.

#include <windows.h>
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc = {0};
MSG msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = TEXT("myWindowClass");
RegisterClassEx(&wc);
CreateWindowEx(WS_EX_CLIENTEDGE,TEXT("myWindowClass"),TEXT("String manipulation"),WS_THICKFRAME|WS_OVERLAPPEDWINDOW| WS_VISIBLE | WS_OVERLAPPEDWINDOW ,CW_USEDEFAULT, CW_USEDEFAULT, 340, 320, NULL, NULL, hInstance, NULL);
while(GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT Ps;
HDC hdc;
switch(msg)
{
case WM_PAINT://traps paint message
{
hdc = BeginPaint(hwnd, &Ps);
TCHAR OutputText[128]="STRING MANIPULATION";
int stringlength=lstrlen(OutputText);
TextOut(hdc, 0,0, OutputText,stringlength);
//change to lower case
CharLower(OutputText);
TextOut(hdc, 0,15, OutputText,stringlength);
//Retrieves text after 1st character in OutputText and stores in string newstring
LPCTSTR newstring=CharNext(OutputText);
TextOut(hdc, 0,30, newstring,stringlength-1);
//Retrieves text after 1st character in newstring and stores in newstring.
//iterates past the first 6 characters and outputs to screen
for(int i=2;i<=7;i++)
{
newstring=CharNext(newstring);
TextOut(hdc, 0,15+15*i, newstring,stringlength-i);
}
//copy newstring to outputstring
strcpy(OutputText,newstring);
stringlength=lstrlen(OutputText);
//change outputstring to upper case
CharUpper(OutputText);
TextOut(hdc, 0,135, OutputText,stringlength);
EndPaint(hwnd, &Ps);
DeleteDC(hdc);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}

Download


Home | API | MFC | C++ | C | Previous | Next
Creating a Simple Window | Common Elements | Data Types and Character Sets | The Device Context | Graphics Device Interface | Displaying Text | Displaying Graphics | Mapping Modes | Keyboard Input | Working with the Mouse | Menus | Child Windows | ScrollBar Control | The Dialog Box | Windows Message Box | Common Dialog Box | Bitmaps | Common Controls | Creating a Toolbar | Multiple Document Interface | Timers | DLL’s | Creating Custom Controls | Owner Drawn Controls | API Hooking and DLL Injection | File Management Functions | String Manipulation | System Information Functions