< Nafees />
github
  • Assignments - DSA
  • Long Integer Operations
  • OOP
    • Introduction
    • Implementation
  • Sorting Algorithms
    • Selection Sort
    • Bubble Sort
    • Insertion Sort
    • Shell Sort
    • Shuffle
    • Merge Sort
    • Convex Hull
    • Quick sort
    • System Sort
    • Heap-Sort
  • Binary Search
  • Binary Search By Recursion
  • Two-Pointer
  • String
  • LeetCode 75
    • Array/String
    • Hash Map
    • BST
    • Binary Search
  • Top Interview 150
    • Linked-List
  • Leetcode Programming Skill
    • Math
  • Leetcode 75
  • 🤡Leet Code Extra
  • Arrays
    • 1-D Array
    • 2-D Arrays
  • 👨‍🍳Basic Math - Codechef
  • ☠️Recursion
  • 😁Public Member Functions
    • Strings Functions
  • 👾Linked List
    • What's Linked List & Implementation?
      • Singly Linked List
      • Doubly Linked List
    • Problems
  • 📚Stack
    • What's Stack & Implementation?
      • Stack Using Array
      • Stack using LL
    • Problems
  • 🏧Queue
    • What's Queue & Implementation?
      • Simple Queue
      • Queue using LL
      • Circular Queue
      • Deque using Linked List
      • STL Deque
    • Problems
  • 🏧Priority Queue
    • What's Priority Queue & Implementation
      • OrderedArrayMaxPQ.Java
      • Maximum-Oriented PQ using Binary Heap
      • Minimum-Oriented PQ using Binary Heap
    • Problems
  • 🗓️Hash Table
    • What's Hash Table & Implementation
      • ST - Seperate Chaining
      • ST - Linear Probing
    • Problems
  • 🎴Symbol Table
    • What's Symbol Table and implementation
      • ST Using Binary search (ordered array)
      • ST Using Binary Search Tree
      • ST Using Left-Leaning Red-Black Tree
      • ST Using Hash Table
    • Problems
  • 🔗Union-Find (Dynamic Connectivity problem)
    • What is a Union Find Data Structure?
    • Implementation
  • 🎋Binary Tree
    • What's Binary Tree & Implementation?
      • Traversal
      • Red-Black BST
  • 🌴Trie
    • What's Trie & Implementation?
    • Problems
  • 😎Project
    • Expression Evaluation
Powered by GitBook
On this page

Was this helpful?

  1. Public Member Functions

Strings Functions

PreviousRecursionNextWhat's Linked List & Implementation?

Last updated 9 months ago

Was this helpful?

// string::substr
#include <iostream>
#include <string>
using namespace std;

int main (){
    string str="We think in generalities, but we live in details.";

    string str2 = str.substr (3,5);     // "think"

    size_t pos = str.find("live");      // position of "live" in str

    string str3 = str.substr (pos);     // get from "live" to the end

    cout << str2 << ' ' << str3 << '\n';

    return 0;
}
bool findStr(string haystack, string needle) {
    
    size_t found = haystack.find(needle);
   
    //The position of the first character of the first match.
    //If no matches were found, the function returns string::npos.
   
    if(found != string::npos)
       return true;
    else
       return false;
}

int main(){
    string haystack = "sabutsad", needle = "sadl";
    
    if(findStr(haystack, needle))
       cout << "String found!!";
    else
       cout << "String not found!!";
 }
Find last occurrence of content in string
int main(){
    string haystack = "sadbutsad", needle = "sad";
    
    size_t found = haystack.rfind(needle);
    
    // Return the position of the first character of the last match.
    
    if(found != string::npos)
       cout << "Last Occurence of String needle in haystack: " << found;
}
// string::find_first_of
#include <iostream>       // cout
#include <string>         // string
#include <cstddef>        // size_t
using namespace std;

int main () {
    string str ("Please, replace the vowels in this sentence by asterisks.");
    
    size_t found = str.find_first_of("aeiou");
    
    // Return the position of the first character that matches.
    
    while (found!=string::npos){
        str[found]='*';
        found=str.find_first_of("aeiou", found+1);
    }

    cout << str << '\n';

    return 0;
}
Find absence of character in string
// string::find_first_not_of
#include <iostream>       // cout
#include <string>         // string
#include <cstddef>        // size_t
using namespace std;

int main (){
    string str ("look for non-alphabetic characters...");

    size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");
    
    // The position of the first character that does not match.
    // If no such characters are found, the function returns string::npos.

    if (found!=string::npos)
    {
        cout << "The first non-alphabetic character is " << str[found];
        cout << " at position " << found << '\n';
    }
    else
        cout << "Whole string contain Alphabetic Characters";
}
// string::find_last_of
#include <iostream>       
#include <string>         
#include <cstddef>        
using namespace std;

void SplitFilename (string& str){
    cout << "Splitting: " << str << '\n';
    
    size_t found = str.find_last_of("/\\");
    
    cout << " path: " << str.substr(0,found) << '\n';
    
    cout << " file: " << str.substr(found+1) << '\n';
}

int main (){
    string str1 ("/usr/bin/man");
    string str2 ("c:\\windows\\delete");

    SplitFilename (str1);
    SplitFilename (str2);

    return 0;
}
// string::find_last_not_of
#include <iostream>       
#include <string>         
#include <cstddef>        
using namespace std;

int main (){
    string str ("Please, erase trailing white-spaces   \n");
    string whitespaces (" \t\f\v\n\r");
   
    // Return the position of the first character that does not match.
    
    size_t found = str.find_last_not_of(whitespaces);
    if (found!=string::npos)
        str.erase(found+1);
    else
        str.clear();            // str is all whitespace

    cout << '[' << str << "]\n";

}
// replacing in a string
#include <iostream>
#include <string>
using namespace std;

int main (){
    
    string base="this is a test string.";
    string str2="n example";
    string str3="sample phrase";
    string str4="useful.";

    // replace signatures used in the same order as described above:

    // Using positions:                 0123456789*123456789*12345
    string str=base;           // "this is a test string."
    str.replace(9,5,str2);          // "this is an example string." (1)
    str.replace(19,6,str3,7,6);     // "this is an example phrase." (2)
    str.replace(8,10,"just a");     // "this is just a phrase."     (3)
    str.replace(8,6,"a shorty",7);  // "this is a short phrase."    (4)
    str.replace(22,1,3,'!');        // "this is a short phrase!!!"  (5)

    // Using iterators:                                               0123456789*123456789*
    str.replace(str.begin(),str.end()-3,str3);                    // "sample phrase!!!"      (1)
    str.replace(str.begin(),str.begin()+6,"replace");             // "replace phrase!!!"     (3)
    str.replace(str.begin()+8,str.begin()+14,"is coolness",7);    // "replace is cool!!!"    (4)
    str.replace(str.begin()+12,str.end()-4,4,'o');                // "replace is cooool!!!"  (5)
    str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful."    (6)
    
    cout << str << '\n';
}
Online Compiler
string :: substr
string :: find()
string :: findr()
string :: find_first_of
string :: find_first_not_of
string :: find_last_of
string::find_last_not_of
string::replace
😁
Page cover image