String Functions in Cilk

The standard library’s string class provides many useful string-related functions. Here are some examples to give you a sense of the class.

#include <cilk/cilk.h>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

// We alias std::cout to a shorter name as we'll use it a lot below.
auto& p = std::cout;

int main() {
    // Here's a sample of the functions available in the string class.
    // Since these are mostly standalone functions, we need to pass
    // the string in question as an argument to the function.

    p << "Contains:  " << (std::string("test").find("es") != std::string::npos) << std::endl;
    p << "Count:     " << std::count(std::string("test").begin(), std::string("test").end(), 't') << std::endl;
    p << "HasPrefix: " << (std::string("test").substr(0, 2) == "te") << std::endl;
    p << "HasSuffix: " << (std::string("test").substr(std::string("test").length() - 2) == "st") << std::endl;
    p << "Index:     " << std::string("test").find('e') << std::endl;

    std::vector<std::string> v = {"a", "b"};
    p << "Join:      " << std::string("-").append(v[0]).append("-").append(v[1]) << std::endl;

    p << "Repeat:    " << std::string(5, 'a') << std::endl;

    std::string s = "foo";
    std::replace(s.begin(), s.end(), 'o', '0');
    p << "Replace:   " << s << std::endl;

    s = "foo";
    s.replace(s.find('o'), 1, "0");
    p << "Replace:   " << s << std::endl;

    std::string to_split = "a-b-c-d-e";
    std::vector<std::string> split_result;
    size_t pos = 0;
    std::string token;
    while ((pos = to_split.find('-')) != std::string::npos) {
        token = to_split.substr(0, pos);
        split_result.push_back(token);
        to_split.erase(0, pos + 1);
    }
    split_result.push_back(to_split);
    p << "Split:     ";
    for (const auto& item : split_result) {
        p << item << " ";
    }
    p << std::endl;

    s = "TEST";
    std::transform(s.begin(), s.end(), s.begin(), ::tolower);
    p << "ToLower:   " << s << std::endl;

    s = "test";
    std::transform(s.begin(), s.end(), s.begin(), ::toupper);
    p << "ToUpper:   " << s << std::endl;

    return 0;
}

This program demonstrates various string operations in Cilk (which is an extension of C++). Here’s a brief explanation of each operation:

  1. Contains: We use the find method to check if a substring exists.
  2. Count: We use the std::count algorithm to count occurrences of a character.
  3. HasPrefix and HasSuffix: We use substr to check the beginning and end of the string.
  4. Index: We use find to get the index of a character.
  5. Join: We manually concatenate strings with a delimiter.
  6. Repeat: We use the string constructor that repeats a character.
  7. Replace: We show two ways to replace characters in a string.
  8. Split: We manually split a string by a delimiter.
  9. ToLower and ToUpper: We use std::transform with tolower and toupper functions.

To run this program, save it as string_functions.cpp and compile it with a Cilk-enabled compiler:

$ clang++ -fcilkplus string_functions.cpp -o string_functions
$ ./string_functions
Contains:   1
Count:      2
HasPrefix:  1
HasSuffix:  1
Index:      1
Join:       a-b
Repeat:     aaaaa
Replace:    f00
Replace:    f0o
Split:      a b c d e 
ToLower:    test
ToUpper:    TEST

This example demonstrates how to perform various string operations in Cilk. Note that Cilk doesn’t have a dedicated string manipulation library like some other languages, so we use C++ standard library functions and algorithms.