0% found this document useful (0 votes)
6 views15 pages

String and Array Manipulation Cheat Sheet

This document is a cheat sheet for string and array manipulation in C++ and Java, covering initialization, iteration, sorting, removing duplicates, frequency counting, and string operations. It provides both standard code examples and syntactic sugar shortcuts for various operations. The document serves as a comprehensive guide for competitive programming related to string and vector/array manipulations.

Uploaded by

trpranto007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

String and Array Manipulation Cheat Sheet

This document is a cheat sheet for string and array manipulation in C++ and Java, covering initialization, iteration, sorting, removing duplicates, frequency counting, and string operations. It provides both standard code examples and syntactic sugar shortcuts for various operations. The document serves as a comprehensive guide for competitive programming related to string and vector/array manipulations.

Uploaded by

trpranto007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🪡

string and array

🧩FullString & Array / Vector Manipulation —


CP Cheat Sheet

1️⃣ Initialization & Declaration


Standard / Real Code:
C++

int arr[5]; // uninitialized


vector<int> v; // empty vector
string s = "abc";

Java

string and array 1


int[] arr = new int[5]; // zeros by default
ArrayList<Integer> list = new ArrayList<>();
String s = "abc";

Shortcut / Syntactic Sugar:


C++

vector<int> v(5,0); // 5 zeros


vector<int> v2 = {1,2,3};
string s2 = "hello";

Java

int[] arr = {1,2,3,4};


List<Integer> list = new ArrayList<>([Link](1,2,3));

2️⃣ Iteration / Loops


Standard / Real Code:

C++

for(int i=0;i<[Link]();i++) cout << v[i];


for(char c:s) cout << c;

Java

for(int i=0;i<[Link];i++) [Link](arr[i]);


for(char c:[Link]()) [Link](c);

string and array 2


Shortcut / Syntactic Sugar:
C++

for(int x : v) cout << x; // range-for


for(auto &c : s) c = toupper(c); // modify string

Java

for(int x : arr) [Link](x);


for(int x : list) [Link](x);

3️⃣ Sorting / Reverse / Shuffle


Standard / Real Code:
C++

sort([Link](), [Link]());
reverse([Link](), [Link]());

Java

[Link](arr);
[Link](list);

Shortcut / Syntactic Sugar:


C++

sort([Link](), [Link]()); // descending


random_shuffle([Link](), [Link]()); // shuffle

string and array 3


rotate([Link](), [Link]()+k, [Link]()); // rotate left by k

Java

[Link](arr, [Link]()); // descending


[Link](list); // shuffle
[Link](list, k); // rotate right by k

4️⃣ Remove Duplicates / Seen Elements


Standard / Real Code:

C++

set<int> seen;
for(int x : v) [Link](x); // track unique elements

Java

Set<Integer> seen = new HashSet<>();


for(int x: arr) [Link](x);

Shortcut / Syntactic Sugar:


C++

sort([Link](), [Link]());
[Link](unique([Link](), [Link]()), [Link]()); // remove consecutive duplicates

Java

string and array 4


List<Integer> uniqueList = new ArrayList<>(new LinkedHashSet<>(list)); // pr
eserves order

5️⃣ Frequency Counting


Standard / Real Code:

C++

map<int,int> freq;
for(int x:v) freq[x]++;

Java

Map<Integer,Integer> freq = new HashMap<>();


for(int x: arr) [Link](x,[Link](x,0)+1);

Shortcut / Syntactic Sugar:

C++

unordered_map<int,int> freq;
for(int x:v) freq[x]++;

Java

int[] count = new int[100]; // if range known


for(int x: arr) count[x]++;

6️⃣
string and array 5
6️⃣ Find / Count / Index
Standard / Real Code:

C++

auto it = find([Link](), [Link](), x);


int cnt = count([Link](), [Link](), x);
int idx = distance([Link](), it);

Java

int idx = -1;


for(int i=0;i<[Link];i++) if(arr[i]==x) {idx=i; break;}
long cnt = [Link](arr).filter(a->a==x).count();

Shortcut / Syntactic Sugar:

C++

bool exists = find(all(v), x) != [Link]();

Java

boolean exists = [Link](arr).anyMatch(a->a==x);

7️⃣ Prefix / Suffix / Accumulate


Standard / Real Code:
C++

string and array 6


vector<int> pre(n+1,0);
for(int i=0;i<n;i++) pre[i+1] = pre[i] + v[i];

Java

int[] pre = new int[n+1];


for(int i=0;i<n;i++) pre[i+1] = pre[i]+arr[i];

Shortcut / Syntactic Sugar:

C++

int sum = accumulate([Link](), [Link](), 0);

Java

int sum = [Link](arr).sum();

8️⃣ Transform / Map Elements


Standard / Real Code:
C++

for(int &x:v) x = x*x;

Java

for(int i=0;i<[Link];i++) arr[i] = arr[i]*arr[i];

string and array 7


Shortcut / Syntactic Sugar:

C++

transform([Link](), [Link](), [Link](), [](int x){return x*x;});

Java

arr = [Link](arr).map(x->x*x).toArray();

9️⃣ String Manipulations


Standard / Real Code:
C++

string s = "abc";
s += 'd'; // append
[Link](1,2); // substring
[Link]('b'); // index
[Link](1,1); // remove char

Java

String s = "abc";
s = s + "d";
[Link](1,3);
[Link]('b');
s = [Link](0,1)+[Link](2);

Shortcut / Syntactic Sugar:

C++

string and array 8


for(auto &c : s) c = toupper(c);
sort([Link](), [Link]());
[Link](unique([Link](), [Link]()), [Link]()); // remove duplicates

Java

String rev = new StringBuilder(s).reverse().toString();


s = [Link]("(.)\\1+", "$1"); // remove consecutive duplicates via regex

🔟 Lambda / Functional Shortcuts


C++

for_each([Link](), [Link](), [](int &x){x++;}); // increment all


sort([Link](), [Link](), [](int a,int b){return a>b;}); // custom sort

Java

arr = [Link](arr).map(x->x+1).toArray(); // increment all


[Link](arr, (a,b)->b-a); // custom comparator

✅ This sheet now covers literally everything you’ll use for string/vector/array
manipulations in CP:

Initialization, iteration

Sorting, reversing, shuffling, rotating

Removing duplicates & “seen” tracking

Frequency counting

Finding, counting, indexing

string and array 9


Prefix sums, accumulation

Transform / map / lambda

String manipulations + substr / erase / regex

Functional shortcuts / STL / Streams

🧠 BASIC STRING OPERATIONS


1️⃣ Input and Output
string s;
cin >> s; // takes one word
getline(cin, s); // takes a whole line (with spaces)
cout << s;

2️⃣ String Length


int len = [Link](); // or [Link]();

3️⃣ Access Characters


cout << s[0]; // first character
s[2] = 'x'; // change a character

4️⃣ Concatenate Strings


string a = "Hello", b = "World";
string c = a + " " + b; // "Hello World"

string and array 10


5️⃣ Compare Strings
if (a == b) cout << "Equal";
else if (a < b) cout << "a is smaller (lexicographically)";

🔡 CASE CONVERSION
6️⃣ Convert to Lowercase
transform([Link](), [Link](), [Link](), ::tolower);

7️⃣ Convert to Uppercase


transform([Link](), [Link](), [Link](), ::toupper);

8️⃣ Toggle Case (manual loop)


for (char &c : s)
c = islower(c) ? toupper(c) : tolower(c);

✂️ SUBSTRINGS & SEARCH


9️⃣ Substring

string and array 11


string sub = [Link](2, 4); // start at index 2, length 4

🔟 Find a substring
int pos = [Link]("abc"); // returns index or npos if not found
if (pos != string::npos)
cout << "Found at " << pos;

1️⃣1️⃣ Replace part of string


[Link](2, 3, "xyz"); // replace 3 chars starting at index 2

🧹 MODIFYING STRINGS
1️⃣2️⃣ Erase
[Link](3, 2); // remove 2 chars starting at index 3

1️⃣3️⃣ Insert
[Link](3, "wow"); // insert at index 3

1️⃣4️⃣ Append
[Link]("!!!");

🔍
string and array 12
🔍 CHARACTER CHECKS
isalpha(c); // true if letter
isdigit(c); // true if number
isspace(c); // true if space
isupper(c); // true if uppercase
islower(c); // true if lowercase

🪄 REVERSE, SORT, UNIQUE


1️⃣5️⃣ Reverse String
reverse([Link](), [Link]());

1️⃣6️⃣ Sort Characters


sort([Link](), [Link]()); // alphabetical order

1️⃣7️⃣ Remove Duplicates (must be sorted first)


sort([Link](), [Link]());
[Link](unique([Link](), [Link]()), [Link]());

🧮 COUNTING & ITERATING


1️⃣8️⃣ Count occurrences of a char

string and array 13


int cnt = count([Link](), [Link](), 'a');

1️⃣9️⃣ Iterate through string


for (char c : s) cout << c << " ";

🧰 STRING TO NUMBER & BACK


2️⃣0️⃣ String → Integer / Double
int n = stoi("123");
double d = stod("3.14");

2️⃣1️⃣ Number → String


string x = to_string(123);

🔁 SPLITTING STRING (by spaces or delimiters)


C++ doesn’t have built-in split, but you can do:

stringstream ss(s);
string word;
while (ss >> word) cout << word << endl;

Or by custom delimiter:

string and array 14


getline(ss, word, ','); // split by comma

string and array 15

You might also like