strcmp
compare two strings and return an integer value indicating their order
Requires
#include "tiktag.h"
Definition
int strcmp(const char *str1, const char *str2);
Description
The strcmp()
function is a tiktag equivalent of the C standard library function that compares two strings and returns an integer value indicating their order.
The function takes two arguments, pointers to two strings of characters (const char *str1 and const char *str2)
and returns an integer value.
The return value can have three possible values:
0 if the two strings are equal a negative value if the first differing character in str1 is less than the corresponding character in str2 a positive value if the first differing character in str1 is greater than the corresponding character in str2
Parameters
str1
: first zero terminated ASCII String-
str2
: second zero terminated ASCII String-
Return Value
int
Remarks
-
Example: output 3 cases of string comparison results
main.js:
import {_wasmInitA, _strToCPP} from "./tt/ttWASM.js";
import wasm from "./web.wasm";
async function main()
{
const wasmInstance = await _wasmInitA(wasm, {}, null);
wasmInstance.exports.compareStrings (
_strToCPP ("These Strings are equal!"),
_strToCPP ("These Strings are equal!"));
wasmInstance.exports.compareStrings (
_strToCPP ("These Strings are equal!"),
_strToCPP ("These Strings nearly are equal!"));
wasmInstance.exports.compareStrings (
_strToCPP ("A Horse is a Horse is a Horse"),
_strToCPP ("A Horse is a Horse"));
}
main();
myCppExample.cpp:
#include "tiktag.h"
_export void compareStrings(char* str1, char* str2)
{
int res;
consoleLog ("compare two Strings:");
consoleStr ("String 1: ", str1);
consoleStr ("String 2: ", str2);
res = strcmp(str1, str2);
if (res == 0)
consoleLog ("Both Strings are identical");
else
if (res > 0)
consoleLog ("String1 is shorter or has a lower value character which differs from String2");
else
consoleLog ("String1 is longer or has a higher value character which differs from String1");
}
Output:
