diff --git a/EDA/arrays_and_vectors/fizzbuzz.cpp b/EDA/arrays_and_vectors/fizzbuzz.cpp new file mode 100644 index 0000000..03ddab3 --- /dev/null +++ b/EDA/arrays_and_vectors/fizzbuzz.cpp @@ -0,0 +1,35 @@ +#include +#include +#include + +std::vector fizzbuzz(int n){ + + std::vector result; + + for(int i = 1; i <= n; ++i) { + + if(i%3==0 && i%5!=0) + result.push_back("Fizz"); + else if(i%3!=0 && i%5==0) + result.push_back("Buzz"); + else if(i%3==0 && i%5==0 && i != 0) + result.push_back("FizzBuzz"); + else + result.push_back(std::to_string(i)); + } + + return result; + +} + + +int main(){ + + std::vector ans = fizzbuzz(17); + + for(auto element : ans) + std::cout << element << " "; + std::cout << std::endl; + + +} \ No newline at end of file diff --git a/EDA/arrays_and_vectors/mountais.cpp b/EDA/arrays_and_vectors/mountais.cpp new file mode 100644 index 0000000..c416efd --- /dev/null +++ b/EDA/arrays_and_vectors/mountais.cpp @@ -0,0 +1,65 @@ +/* + Write a function that makesinput an array of distinct + integers, and return the length of highest mountain + + A mountain is definied as adjacent integers that are strictly + increasing until they reach a peak, at which the become + stricly decreasing + + At least 3 number are required to form a mountain + + ex: + 10 + / \ -----> This is a mountain + 5 3 + +*/ +#include +#include +#include + + +// my Implementation + +int solve(std::vector mountain) { + + int N = mountain.size(); + int peak = 0; // found the highest peak at the mountain + for(int i = 1; i < N-2;) { + + // First, we've to find a peak + if(mountain[i] > mountain[i-1] && mountain[i] > mountain[i+1]) { + + int count = 1, j = i; + + // Finding the left size of the mountain + while(j>= 1 && mountain[j]>mountain[j-1]) { + j--; + count++; + } + + // finding the right size of the moutain + while(i<=N-2 && mountain[i]>mountain[i+1]) { + i++; + count++; + } + + peak = std::max(peak,count); + + } else + i++; + + } + + return peak; + +} + +int main(){ + + std::vector nums = {5,6,1,2,3,4,5,4,3,2,0,1,2,3,-2,4}; + std::cout << solve(nums) << std::endl; + +} + + diff --git a/EDA/arrays_and_vectors/pairs.cpp b/EDA/arrays_and_vectors/pairs.cpp new file mode 100644 index 0000000..977e96d --- /dev/null +++ b/EDA/arrays_and_vectors/pairs.cpp @@ -0,0 +1,70 @@ +/* + Given an array containing N integers, and an number S denoting + a target sum. + + Find two distinct integers that can pair up to form target sum. + Let us assume there will be only one such pair + + input + arra=[10,5,2,3,-6,9,11] + S = 4 + + output + [10,-6] +*/ +#include +#include +#include +#include + +// solution with approach using sort vector +void solve(std::vector &v, int sum) { + + std::sort(v.begin(),v.end()); + int l = 0, r = v.size()-1; + + while (l <= r) { + if(v[l]+v[r] == sum) { + std::cout << "[" << v[l] << "," << v[r] << "]" << std::endl; + return; + } if (v[l]+v[r] > sum) + r--; + else + l++; + } + + std::cout << "Doesn't exist" << std::endl; + return; + +} + +// solution using unordered_set +void solve2(std::vector &v, int sum) { + + std::unordered_set us; + + for(int i = 0; i < (int)v.size(); ++i) { + if(us.find(sum-v[i])!=us.end()) { + std::cout << "[" << v[i] << "," << sum-v[i] << "]" << std::endl; + return; + } + us.emplace(v[i]); + } + + + std::cout << "Doesn't exist" << std::endl; + +} + + +int main(){ + + std::vector nums = {10,5,2,3,-6,9,11}; + int sum = 4; + + solve(nums,sum); + solve2(nums,sum); + +} + + diff --git a/EDA/arrays_and_vectors/triplets.cpp b/EDA/arrays_and_vectors/triplets.cpp new file mode 100644 index 0000000..69dd38f --- /dev/null +++ b/EDA/arrays_and_vectors/triplets.cpp @@ -0,0 +1,61 @@ +/* + Given an array containing N integers, and an Number S + denoting a target sum. + + Find all distinct integers that can add up to form target + sum. The numbers in each triplet should be ordered in + ascending order, and triplet should be ordered too. + Return empty array if no such triplet exist + +*/ +#include +#include +#include + +std::vector> solve(std::vector v, int sum){ + + std::vector> ans; + // sorting the given vector + std::sort(v.begin(),v.end()); + + // Pick every v[i] number and solve a pair sum for remaining part + for(int i = 0; i < (int)v.size()-2; ++i) { + + int j = i+1; + int k = (int)v.size()-1; + + // Two pointer approach + while(j < k) { + int current_sum = v[i]; + if(current_sum+v[j]+v[k] == sum) { + ans.push_back({current_sum,v[j],v[k]}); + j++; + k--; + } else if(current_sum+v[j]+v[k] > sum) + k--; + else + j++; + } + } + + return ans; + +} + +int main(){ + + std::vector arr = {1,2,3,4,5,6,7,8,9,15}; + int sum = 18; + + auto result = solve(arr,sum); + + for(auto rows : result) { + for(auto elements : rows) + std::cout << elements << " "; + std::cout << std::endl; + } + + +} + + diff --git a/EDA/bitwise/checkOddEven.cpp b/EDA/bitwise/checkOddEven.cpp new file mode 100644 index 0000000..a754e46 --- /dev/null +++ b/EDA/bitwise/checkOddEven.cpp @@ -0,0 +1,17 @@ +#include + +int main(void) { + + // to check if a number is even or odd, we just + // have to see if the last bit is equa lto 0 or 1 + int n; + std::cin >> n; + + // if last bit is equal to one, iss a odd number + if(n&1) + std::cout << "Odd" << std::endl; + else + std::cout << "Even" << std::endl; + + +} \ No newline at end of file diff --git a/EDA/bitwise/clearIthbit.cpp b/EDA/bitwise/clearIthbit.cpp new file mode 100644 index 0000000..8072c82 --- /dev/null +++ b/EDA/bitwise/clearIthbit.cpp @@ -0,0 +1,22 @@ +#include +#include + +int clearIthbit(int n, int i) { + + int mask = (1<> n >> i; + + std::cout << "Number(decimal) : " << n << std::endl; + std::cout << "Number(binary) : " << std::bitset<4>(n) << std::endl; + std::cout << "pposotion to change : " << i << std::endl; + std::cout << "newNumber(binary) : " << std::bitset<4>(clearIthbit(n,i)) << std::endl; + std::cout << "newNumber(decimal) : " << clearIthbit(n,i) << std::endl; + +} \ No newline at end of file diff --git a/EDA/bitwise/clearLastBits.cpp b/EDA/bitwise/clearLastBits.cpp new file mode 100644 index 0000000..3d93a7f --- /dev/null +++ b/EDA/bitwise/clearLastBits.cpp @@ -0,0 +1,28 @@ +#include +#include + + +int clearLastBits(int &n, int i) { + + int mask = (-1<> n >> i; + + std::cout << "Number(decimal) : " << n << std::endl; + std::cout << "Number(binary) : " << std::bitset<4>(n) << std::endl; + std::cout << "pposotion to change : " << i << std::endl; + std::cout << "newNumber(binary) : " << std::bitset<4>(clearLastBits(n,i)) << std::endl; + std::cout << "newNumber(decimal) : " << clearLastBits(n,i) << std::endl; + +} + + + + + diff --git a/EDA/bitwise/getIthbit.cpp b/EDA/bitwise/getIthbit.cpp new file mode 100644 index 0000000..f99a22d --- /dev/null +++ b/EDA/bitwise/getIthbit.cpp @@ -0,0 +1,22 @@ +#include +#include + +int getIthBit(int n, int i) { + + int mask = (1< 0 ? 1 : 0; + +} + + +int main(void) { + + int n, i; + std::cin >> n >> i; + + std::cout << "Number(decimal): " << n << std::endl; + std::cout << "Number(binary): " << std::bitset<8>(n) << std::endl; + std::cout << "Bit at " << i << " position: " << getIthBit(n,i) << std::endl; + + +} \ No newline at end of file diff --git a/EDA/bitwise/replaceBits.cpp b/EDA/bitwise/replaceBits.cpp new file mode 100644 index 0000000..de84ad7 --- /dev/null +++ b/EDA/bitwise/replaceBits.cpp @@ -0,0 +1,51 @@ +/* + You're given two 32-bit numbers, N and M, ando two bit positions + i and j. + Write a method to set all bits between i and j in N equal to M. + M (becomes a substring of N locationed at and starting at j) + + Example: + N = 10000000000 + M = 10101 + i = 2, j = 6 + output: 1001010100 + +*/ +#include +#include + + +// First we've to clear a range number of bits +void clearBitsInRange(int &n, int i, int j) { + + int a = (-1)<<(j+1); + int b = (1<(n) << std::endl; + +} + diff --git a/EDA/bitwise/setIthbit.cpp b/EDA/bitwise/setIthbit.cpp new file mode 100644 index 0000000..bd96025 --- /dev/null +++ b/EDA/bitwise/setIthbit.cpp @@ -0,0 +1,22 @@ +#include +#include + +int setIthbit(int n, int i) { + + int mask = (1<> n >> i; + + std::cout << "Number(decimal) : " << n << std::endl; + std::cout << "Number(binary) : " << std::bitset<4>(n) << std::endl; + std::cout << "pposotion to change : " << i << std::endl; + std::cout << "newNumber(binary) : " << std::bitset<4>(setIthbit(n,i)) << std::endl; + std::cout << "newNumber(decimal) : " << setIthbit(n,i) << std::endl; + +} \ No newline at end of file diff --git a/EDA/recursion/ladders.cpp b/EDA/recursion/ladders.cpp new file mode 100644 index 0000000..d5476e8 --- /dev/null +++ b/EDA/recursion/ladders.cpp @@ -0,0 +1,48 @@ +#include +/* + Given a ladder containing N steps, you can take a jump of + 1,2 or 3 at each step. Find the number of ways to climb the + ladder + + input + N=4 + + output + 7 +*/ + +int ladders(int n, int steps) { + + // basic case + if(steps == n) + return 1; + else if(steps > n) + return 0; + + // recursion calls + // we can jump one step + int jump1 = ladders(n,steps+1); + + // we can jump two steps + int jump2 = ladders(n,steps+2); + + // we can jump three steps + int jump3 = ladders(n,steps+3); + + + // return the total + return jump1+jump2+jump3; + +} + + +int main(void) { + + int n; + std::cin >> n; + std::cout << ladders(n,0) << std::endl; + +} + + + diff --git a/EDA/recursion/subsets.cpp b/EDA/recursion/subsets.cpp new file mode 100644 index 0000000..13e8cd5 --- /dev/null +++ b/EDA/recursion/subsets.cpp @@ -0,0 +1,40 @@ +#include +#include +#include + + +void subsets(char *input, char *output, int i, int j) { + + // base condition, when input reaches '\0' + if(input[i] == '\0') { + output[j] = '\0'; + std::cout << output << std::endl; + return; + } + + // recursions calls + // when we gets a letter from input + output[j] = input[i]; + subsets(input,output,i+1,j+1); + + // when we dont get a letter from input + output[j] = '\0'; + subsets(input,output,i+1,j); + +} + + +int main(void) { + + char input[1000], output[1000]; + std::cin.getline(input,1000); + + subsets(input,output,0,0); + +} + + + + + + diff --git a/EDA/recursion/subsetsum.cpp b/EDA/recursion/subsetsum.cpp new file mode 100644 index 0000000..53e3ed8 --- /dev/null +++ b/EDA/recursion/subsetsum.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +/* + Without loop: given a set of non-negative integers, and + a value sum, determine if there is a subset of the given + set with sum equal to given sum. + + input + arr=[10,12,15,6,19,20]; + sum=16; + + output + Yes +*/ + +bool sumExist(int *arr, int sum, int i, int j) { + + // base condition + if(arr[i]+arr[j] == sum) + return true; + + if(arr[i]+arr[j] > sum) + return sumExist(arr,sum,i,j-1); + else + return sumExist(arr,sum,i+1,j); + + return false; + +} + + +int main(){ + + int arr[] = {10,12,15,6,19,20}; + int sum = 16; + int n = sizeof(arr)/sizeof(arr[0]); + + // first we've to sort the array + std::sort(arr,arr+n); + + if(sumExist(arr,sum,0,n-1)) + std::cout << "Yes" << std::endl; + else + std::cout << "No" << std::endl; + +} + diff --git a/EDA/sliding_windows_problems/housing.cpp b/EDA/sliding_windows_problems/housing.cpp new file mode 100644 index 0000000..e8842f5 --- /dev/null +++ b/EDA/sliding_windows_problems/housing.cpp @@ -0,0 +1,108 @@ +/* + Along one sindeof a road there is a sequence of vacant + plots of land. Each plot has a different area (non-zero). + So, the areas from a sequence A[1], A[2], ... A[N]. + + You want to buy K acres of alnd to build a house, you want + to find all segments of contiguous plots (i.e., a subsection + in the sequence) of whose sum is exaclty k. + + Sample input + plots = [1 3 2 1 4 1 3 2 1 1 2] + K = 8 + + sample output + 2 5 + 4 6 + 5 9 + +*/ +#include +#include + +void housing(int *arr, int n, int k) { + + int i, j, cout; + i = j = cout = 0; + + while(j sum and i < j + while(cout > k and i < j) { + cout -= arr[i]; + i++; + } + + // check if cout is equal to k + if(cout==k) { + // printing the window + // j-1 because or current point j is pretendo to sum + // the number in the next index + std::cout << i << "-" << j-1 << std::endl; + } + + + } + + return; + +} + +void smallestHousing(int *arr, int n, int k) { + + int i, j, cout, small; + i = j = cout = small = 0; + std::pair smallpair; + + while(j k && i < j) { + cout -= arr[i]; + i++; + } + + // if we gets k + if(cout==k) { + int current = (j-1)-i+1; + if (current < small || small == 0) { + small = current; + smallpair.first = i; + smallpair.second = j-1; + } + } + + } + + std::cout << smallpair.first << "-" << smallpair.second << std::endl; + +} + + + +int main(void) { + + int plots[] = {1,3,2,1,4,1,3,2,1,1}; + int n = sizeof(plots)/sizeof(int); + int k = 8; + + housing(plots,n,k); + std::cout << "Small window: "; + smallestHousing(plots,n,k); + +} + + + + + + + diff --git a/EDA/sliding_windows_problems/uniqueSubstring.cpp b/EDA/sliding_windows_problems/uniqueSubstring.cpp new file mode 100644 index 0000000..bc00d99 --- /dev/null +++ b/EDA/sliding_windows_problems/uniqueSubstring.cpp @@ -0,0 +1,70 @@ +/* + Given a string, write a function to find the lasrgest + substring with unique characters (no duplicates) + + input + prateekbhaiya + aabcb + + output + ekhbhaiy + abc +*/ +#include +#include +#include +#include + +std::string unique_substring(std::string str) { + + int i, j, cout; + i = j = cout = 0; + + int max_window = 0; + int start_window = -1; + + std::unordered_map map; + + while(j < (int)str.length()) { + + // current character + char ch = str[j]; + + // if it is inside hashmap and index >= start of the + // current window (i); + if(map.count(ch) and map[ch]>=i) { + // starting new window + i = map[ch]+1; + cout=j-i; // updated remaining windows len exlucing current char + } + + // update the last occurrency + map[ch] = j; + cout++; + j++; + + //update max_window_len every step + if(cout > max_window) { + max_window = cout; + start_window = i; + } + + + } + + return str.substr(start_window,max_window); + +} + +int main(void) { + + std::string input; + std::cin >> input; + + std::cout << unique_substring(input) << std::endl; + + +} + + + diff --git a/EDA/sliding_windows_problems/uniqueSubstring2.cpp b/EDA/sliding_windows_problems/uniqueSubstring2.cpp new file mode 100644 index 0000000..36e84a8 --- /dev/null +++ b/EDA/sliding_windows_problems/uniqueSubstring2.cpp @@ -0,0 +1,51 @@ +#include +#include +#include +#include + +std::string unique_string(std::string str) { + + int i, j, cout; + i = j = cout = 0; + + int start_window = -1; + int max_windows = 0; + + std::unordered_map ocurrency; + + while (j < (int)str.length()){ + // current char + char ch = str[j]; + + // find a ocurrency of repettion + if(ocurrency.count(ch) && ocurrency[ch]>=i) { + i=ocurrency[ch]+1; + cout = j-i; // j-i because we exclude the nex ocurrency of j + } + + // Creating the case of ocurrency in hashmap + ocurrency[ch] = j; + j++; + cout++; + + if(cout > max_windows) { + max_windows = cout; + start_window = i; + } + } + + return str.substr(start_window,max_windows); + +} + + + +int main(void) { + + std::string input; + getline(std::cin,input); + + std::cout << unique_string(input) << std::endl; + +} + diff --git a/EDA/sorting_and_search/inversioncout.cpp b/EDA/sorting_and_search/inversioncout.cpp new file mode 100644 index 0000000..d688847 --- /dev/null +++ b/EDA/sorting_and_search/inversioncout.cpp @@ -0,0 +1,93 @@ +#include +#include +/* + Given an array containing integers, you need to count the + total number of inversions. + Inversion: Two elements a[i] and a[j] form an inversion + if a[i]>a[j] and i < j; +*/ + +// Brute force approach (O(n^2)) +int mySolution(std::vector &arr) { + + int i, j, cout; + i = cout = 0; + + while (i < (int)arr.size()-1) { + j = i+1; + while(j <= (int)arr.size()-1) { + if(arr[i]>arr[j]) + cout++; + j++; + } + i++; + } + + return cout; + +} + +// Implementation with mergesort (O(nlog n)) +int merge(std::vector &arr, int s, int e){ + + int i = s; + int m = (s+e)/2; + int j = m+1; + + std::vector tmp; + int cout = 0; + + while(i<=m and j<=e) { + if(arr[i] < arr[j]) { + tmp.push_back(arr[i]); + i++; + } else { + cout += (m-i+1); + tmp.push_back(arr[j]); + j++; + } + } + + // completing the fill from the first array + for(; i<=m; ++i) + tmp.push_back(arr[i]); + + // completing the fill from the second array + for(; j<=e; ++j) + tmp.push_back(arr[j]); + + // passing the tmp array to the original + int k = 0; + for(int idx = s; idx <=e; ++idx) + arr[idx] = tmp[k++]; + + return cout; + +} + +int inversion_cout(std::vector &arr, int s, int e){ + + // base case + if(s>=e) + return 0; + + // recursions call + int m = (s+e)/2; + int c1 = inversion_cout(arr,s,m); + int c2 = inversion_cout(arr,m+1,e); + int c3 = merge(arr,s,e); + + return c1+c2+c3; + +} + + +int main(void) { + + std::vector arr = {0,5,2,3,1}; + std::cout << mySolution(arr) << std::endl; + std::cout << inversion_cout(arr,0,arr.size()-1) << std::endl; + + +} + diff --git a/EDA/sorting_and_search/mergesort.cpp b/EDA/sorting_and_search/mergesort.cpp new file mode 100644 index 0000000..f9bbb5d --- /dev/null +++ b/EDA/sorting_and_search/mergesort.cpp @@ -0,0 +1,84 @@ +#include +#include + +// Implementing a merge sort +/* + Mergesort is an algorithm to order determinates values + folllowing a key rule + + the merge is a algorithn that divides and conquist + + 1. divide in 2 vectors + 3. mersort the vector of the left and right + 3. merge the both vectors to result + +*/ + +void merge(std::vector &arr, int s, int e) { + + // mark variable to help during loops + int i = s; + int m = (s+e)/2; + int j = m+1; + + // creating a temp vector to get the values from left and right + std::vector temp; + + while(i<=m and j<=e) { + if(arr[i] < arr[j]) { + temp.push_back(arr[i]); + i++; + } + else { + temp.push_back(arr[j]); + j++; + } + } + + // copy remaining elements from first array + for(; i <= m; ++i) + temp.push_back(arr[i]); + + // copy remaining elements from second array + for(; j<=e; ++j) + temp.push_back(arr[j]); + + // copy back the elements from temp to original array + int k = 0; + for(int idx = s; idx <= e; ++idx) + arr[idx] = temp[k++]; + +} + +void mergesort(std::vector &arr, int s, int e) { + + // base case + if(s>=e) + return; + + // recursion case + // dividing the array in two parts + int mid = (s+e)/2; + mergesort(arr,s,mid); + mergesort(arr,mid+1,e); + + // making the merge in both arrays + return merge(arr,s,e); + +} + +int main(){ + + std::vector arr = {10,5,2,0,7,6,4}; + + int s = 0; + int e = arr.size()-1; + mergesort(arr,s,e); + + for(int x : arr) { + std::cout << x << " "; + } + std::cout << std::endl; + + +} \ No newline at end of file diff --git a/EDA/strings/changeLetter.cpp b/EDA/strings/changeLetter.cpp new file mode 100644 index 0000000..39a0c44 --- /dev/null +++ b/EDA/strings/changeLetter.cpp @@ -0,0 +1,45 @@ +/* +Write a C++ program to change every letter in a given string with the letter following it in the alphabet (i.e. a becomes b, p becomes q, z becomes a). +Example: +Sample Input: w3resource +Sample Output: x3sftpvsdf + +*/ +#include +#include +#include + +std::string changeLetter(std::string s) { + + int char_code; // variable stogin ascii values + + for(int i = 0; i < (int)s.length(); ++i) { + + char_code = int(s[i]); + + // checking if the element is z, to change to a + if(char_code == 122) + s[i] = char(97); + else if(char_code == 90) // checking if it Z, to change to A + s[i] = char(65); + else + s[i] = char(char_code+1); + + } + + return s; + +} + + +int main(){ + + std::string s1; + getline(std::cin,s1); + + std::cout << "Word before: " << s1 << std::endl; + std::cout << "Word after: " < +#include +#include +#include +#include + + +std::vector words(std::string s1) { + + std::vector ans; + std::stringstream ss(s1); + std::string token; + while(getline(ss,token,' ')) { + ans.push_back(token); + } + + return ans; + +} + + +std::string bigString(std::vector vec) { + + std::string bigger = ""; + int comp = 0; + for(int i = 0; i < (int)vec.size(); ++i) { + int n = vec[i].length(); + if(n > comp) { + bigger = vec[i]; + comp = (int)vec[i].length(); + } + } + + return bigger; + +} + + +int main(){ + + std::string p; + getline(std::cin, p); + + std::vector res = words(p); + std::cout << bigString(res) << std::endl; + +} + + + + diff --git a/EDA/strings/space20.cpp b/EDA/strings/space20.cpp new file mode 100644 index 0000000..2564aaa --- /dev/null +++ b/EDA/strings/space20.cpp @@ -0,0 +1,46 @@ +/* + Given an string, write a function to replace all spaces + in a string with "%20". +*/ +#include +#include +#include + +void replace_space(char *str) { + + int spaces = 0; + for(int i = 0; str[i] != '\0'; ++i) + if(str[i] == ' ') + spaces++; + + int idx = strlen(str) + 2*spaces; + str[idx] = '\0'; + + for(int i = (int)strlen(str)-1; i>=0; --i) { + if(str[i]==' ') { + str[idx-1] = '0'; + str[idx-2] = '2'; + str[idx-3] = '%'; + idx-=3; + } else { + str[idx-1] = str[i]; + idx--; + } + } + +} + +int main(){ + + char input[1000]; + std::cin.getline(input,1000); + + replace_space(input); + std::cout << input << std::endl; + + +} + + + + diff --git a/EDA/strings/strings.cpp b/EDA/strings/strings.cpp new file mode 100644 index 0000000..629c8ca --- /dev/null +++ b/EDA/strings/strings.cpp @@ -0,0 +1,51 @@ +#include +#include +#include + +int main(){ + + // std::string s; + // getline(std::cin,s); + + // std::cout << s << std::endl; + + // for(char ch : s) + // std::cout << ch << ","; + + // std::cout << std::endl; + + // int n = 5; + // std::vector arr; + + // while(n--) { + // std::string tmp; + // getline(std::cin,tmp); + // arr.push_back(tmp); + // } + + // for(auto names : arr) + // std::cout << names << " "; + + // std::cout << std::endl; + + + // Find Function + std::string paragraph = "We are learning about STL strings. STL string class is quite powerfull"; + + std::string word; + getline(std::cin,word); + + int index = paragraph.find(word); + if(index == -1) + std::cout << "Not found" << std::endl; + else + std::cout << word << std::endl; + + // Finding more than 1 occurency of a string + for(int i = 1; index != -1; ++i) { + std::cout << i <<" occ at: " << index << std::endl; + index = paragraph.find(word,index+1); + } + + +} \ No newline at end of file diff --git a/EDA/strings/tokeniser.cpp b/EDA/strings/tokeniser.cpp new file mode 100644 index 0000000..4715399 --- /dev/null +++ b/EDA/strings/tokeniser.cpp @@ -0,0 +1,37 @@ +/* + Creating my own function to get tokens from a string +*/ +#include +#include +#include +#include +#include + +char *mystrtok(char *str, char delim) { + + static char *input = NULL; + if(str!=NULL) + input = str; + if(input == NULL) + return NULL; + + char *token = new char[strlen(input)+1]; + int i = 0; + + for(; input[i]!='\0';++i) { + if(input[i]!=delim) + token[i]=input[i]; + else { + token[i] = NULL; + input = input+i+1; + } + } + + // out of the loop + token[i] = '\0'; + + // resete the input as NULL + input = NULL; + return token; + +} \ No newline at end of file diff --git a/EDA/strings/tokenization.cpp b/EDA/strings/tokenization.cpp new file mode 100644 index 0000000..45934c4 --- /dev/null +++ b/EDA/strings/tokenization.cpp @@ -0,0 +1,59 @@ +/* + Tokenization a string denotes splitting a string with + respect to some delimiter(s). + + + input + "Today is sunny day" + + output + "Today","is","sunny","day" +*/ +#include +#include +#include +#include +#include + +int main(){ + + + // 1. Using stringstream class + std::string input; + getline(std::cin,input); + + // Create a string stream object + std::stringstream ss(input); + + // >> and << operators + std::string token; + std::vector tokens; + while(getline(ss,token,' ')) { + tokens.push_back(token); + } + + // printing the resul + for(auto string : tokens) + std::cout << "'" << string << "'" << ","; + + std::cout << std::endl; + + + + // 2. Using strtok -> from cstring + char string[1000]; + std::cin.getline(string,1000); + + + // strtok() + // Internally maintains the state of the string passed in the last + // fuction + char *tok = strtok(string," "); + + while(tok!=NULL) { + std::cout << tok << std::endl; + tok = strtok(NULL," "); + } + + +} \ No newline at end of file