From 390fc888bdbe41cdaeaace524aaeeaa2e540b186 Mon Sep 17 00:00:00 2001 From: Ujjwal <75884061+UG-SEP@users.noreply.github.com> Date: Sun, 18 Apr 2021 21:49:00 +0530 Subject: [PATCH] Redirected C Folder (#228) * redirected bad or Good string.cpp * redirected C Algorithms * created readme of c folder * correct URL of linked list and string algorithm * one line space after C title --- algorithms/C/README.md | 23 ++++ .../C/arrays}/even-and-odd.c | 80 +++++++------- .../C/arrays}/unique-elements-in-an-array.c | 0 .../C/graphs}/Prim's-algorithm.c | 0 .../Insert-and-delete-beginning.c | 0 .../C/linked-lists}/josephus-problem.c | 0 .../queues}/double-ended-queue-using-array.c | 0 .../C/sorting}/merge-sort.c | 0 .../C/strings}/Permutation-of-String.c | 0 .../C/strings}/count-words.c | 0 .../C/strings}/palindrome.c | 60 +++++----- .../CPlusPlus/Strings/Bad-or-Good-string.cpp | 104 ++++++++++++++++++ arrays/README.md | 8 -- graphs/README.md | 6 - linked-lists/README.md | 5 - queues/README.md | 5 - sorting/README.md | 5 - strings/README.md | 7 -- 18 files changed, 197 insertions(+), 106 deletions(-) create mode 100644 algorithms/C/README.md rename {arrays/c-or-cpp => algorithms/C/arrays}/even-and-odd.c (94%) rename {arrays/c-or-cpp => algorithms/C/arrays}/unique-elements-in-an-array.c (100%) rename {graphs/c-or-cpp => algorithms/C/graphs}/Prim's-algorithm.c (100%) rename {linked-lists/c-or-cpp => algorithms/C/linked-lists}/Insert-and-delete-beginning.c (100%) rename {linked-lists/c-or-cpp => algorithms/C/linked-lists}/josephus-problem.c (100%) rename {queues/c-or-cpp => algorithms/C/queues}/double-ended-queue-using-array.c (100%) rename {sorting/c-or-cpp => algorithms/C/sorting}/merge-sort.c (100%) rename {strings/c-or-cpp => algorithms/C/strings}/Permutation-of-String.c (100%) rename {strings/c-or-cpp => algorithms/C/strings}/count-words.c (100%) rename {strings/c-or-cpp => algorithms/C/strings}/palindrome.c (95%) create mode 100644 algorithms/CPlusPlus/Strings/Bad-or-Good-string.cpp delete mode 100644 arrays/README.md delete mode 100644 graphs/README.md delete mode 100644 linked-lists/README.md delete mode 100644 queues/README.md delete mode 100644 sorting/README.md delete mode 100644 strings/README.md diff --git a/algorithms/C/README.md b/algorithms/C/README.md new file mode 100644 index 00000000..5ca9b8f3 --- /dev/null +++ b/algorithms/C/README.md @@ -0,0 +1,23 @@ +# C + +## Arrays +- [Even and Odd](arrays/even-and-odd.c) +- [Unique Elements in an array](arrays/unique-elements-in-an-array.c) + +## Graphs +- [Prim's Algorithm](graphs/Prim's-algorithm.c) + +## Linked Lists +- [Insert and Delete at Beginning](linked-lists/Insert-and-delete-beginning.c) +- [Josephus Problem](linked-lists/josephus-problem.c) + +## Queues +- [Double Ended Queue using array](queues/double-ended-queue-using-array.c) + +## Sorting +- [Merge Sort](sorting/merge-sort.c) + +## Strings +- [Count Words](strings/count-words.c) +- [Palindrome](strings/palindrome.c) +- [Permutation of String](string/Permutation-of-String.c) diff --git a/arrays/c-or-cpp/even-and-odd.c b/algorithms/C/arrays/even-and-odd.c similarity index 94% rename from arrays/c-or-cpp/even-and-odd.c rename to algorithms/C/arrays/even-and-odd.c index 96481b20..d993a798 100644 --- a/arrays/c-or-cpp/even-and-odd.c +++ b/algorithms/C/arrays/even-and-odd.c @@ -1,40 +1,40 @@ -#include -int main() -{ - int n; - printf("Enter n : "); //n is number of elements in the array - scanf("%d", &n); - int arr[n]; - for (int i = 0; i < n; i++) - { - scanf("%d", &arr[i]); - } - int arr_odd[n], arr_even[n]; //arr_odd[] for odd elements and arr_even[] for even elements - int o = 0, e = 0; - for (int i = 0; i < n; i++) - { - if (arr[i] % 2 != 0) - { - arr_odd[o] = arr[i]; //Putting odd numbers in arr_odd[] array. - o++; - } - else if (arr[i] % 2 == 0) - { - arr_even[e] = arr[i]; //Putting even numbers in arr_even[] array. - e++; - } - } - - printf("Even array : [ "); - for (int i = 0; i < e; i++) - { - printf("%d ", arr_even[i]); - } - printf("]\nOdd array : [ "); - for (int i = 0; i < o; i++) - { - printf("%d ", arr_odd[i]); - } - printf("]"); - return 0; -} +#include +int main() +{ + int n; + printf("Enter n : "); //n is number of elements in the array + scanf("%d", &n); + int arr[n]; + for (int i = 0; i < n; i++) + { + scanf("%d", &arr[i]); + } + int arr_odd[n], arr_even[n]; //arr_odd[] for odd elements and arr_even[] for even elements + int o = 0, e = 0; + for (int i = 0; i < n; i++) + { + if (arr[i] % 2 != 0) + { + arr_odd[o] = arr[i]; //Putting odd numbers in arr_odd[] array. + o++; + } + else if (arr[i] % 2 == 0) + { + arr_even[e] = arr[i]; //Putting even numbers in arr_even[] array. + e++; + } + } + + printf("Even array : [ "); + for (int i = 0; i < e; i++) + { + printf("%d ", arr_even[i]); + } + printf("]\nOdd array : [ "); + for (int i = 0; i < o; i++) + { + printf("%d ", arr_odd[i]); + } + printf("]"); + return 0; +} diff --git a/arrays/c-or-cpp/unique-elements-in-an-array.c b/algorithms/C/arrays/unique-elements-in-an-array.c similarity index 100% rename from arrays/c-or-cpp/unique-elements-in-an-array.c rename to algorithms/C/arrays/unique-elements-in-an-array.c diff --git a/graphs/c-or-cpp/Prim's-algorithm.c b/algorithms/C/graphs/Prim's-algorithm.c similarity index 100% rename from graphs/c-or-cpp/Prim's-algorithm.c rename to algorithms/C/graphs/Prim's-algorithm.c diff --git a/linked-lists/c-or-cpp/Insert-and-delete-beginning.c b/algorithms/C/linked-lists/Insert-and-delete-beginning.c similarity index 100% rename from linked-lists/c-or-cpp/Insert-and-delete-beginning.c rename to algorithms/C/linked-lists/Insert-and-delete-beginning.c diff --git a/linked-lists/c-or-cpp/josephus-problem.c b/algorithms/C/linked-lists/josephus-problem.c similarity index 100% rename from linked-lists/c-or-cpp/josephus-problem.c rename to algorithms/C/linked-lists/josephus-problem.c diff --git a/queues/c-or-cpp/double-ended-queue-using-array.c b/algorithms/C/queues/double-ended-queue-using-array.c similarity index 100% rename from queues/c-or-cpp/double-ended-queue-using-array.c rename to algorithms/C/queues/double-ended-queue-using-array.c diff --git a/sorting/c-or-cpp/merge-sort.c b/algorithms/C/sorting/merge-sort.c similarity index 100% rename from sorting/c-or-cpp/merge-sort.c rename to algorithms/C/sorting/merge-sort.c diff --git a/strings/c-or-cpp/Permutation-of-String.c b/algorithms/C/strings/Permutation-of-String.c similarity index 100% rename from strings/c-or-cpp/Permutation-of-String.c rename to algorithms/C/strings/Permutation-of-String.c diff --git a/strings/c-or-cpp/count-words.c b/algorithms/C/strings/count-words.c similarity index 100% rename from strings/c-or-cpp/count-words.c rename to algorithms/C/strings/count-words.c diff --git a/strings/c-or-cpp/palindrome.c b/algorithms/C/strings/palindrome.c similarity index 95% rename from strings/c-or-cpp/palindrome.c rename to algorithms/C/strings/palindrome.c index d6e1c250..cda7e841 100644 --- a/strings/c-or-cpp/palindrome.c +++ b/algorithms/C/strings/palindrome.c @@ -1,30 +1,30 @@ - -#include -#include - -// A function to check if a string str is palindrome -void isPalindrome(char str[]) -{ - // Start from leftmost and rightmost corners of str - int l = 0; - int h = strlen(str) - 1; - - // Keep comparing characters while they are same - while (h > l) - { - if (str[l++] != str[h--]) - { - printf("%s is Not Palindrome", str); - return; - } - } - printf("%s is palindrome", str); -} - -// Driver program to test above function -int main() -{ - isPalindrome("abba"); - isPalindrome("abbccbba"); - return 0; -} + +#include +#include + +// A function to check if a string str is palindrome +void isPalindrome(char str[]) +{ + // Start from leftmost and rightmost corners of str + int l = 0; + int h = strlen(str) - 1; + + // Keep comparing characters while they are same + while (h > l) + { + if (str[l++] != str[h--]) + { + printf("%s is Not Palindrome", str); + return; + } + } + printf("%s is palindrome", str); +} + +// Driver program to test above function +int main() +{ + isPalindrome("abba"); + isPalindrome("abbccbba"); + return 0; +} diff --git a/algorithms/CPlusPlus/Strings/Bad-or-Good-string.cpp b/algorithms/CPlusPlus/Strings/Bad-or-Good-string.cpp new file mode 100644 index 00000000..df0b9ec1 --- /dev/null +++ b/algorithms/CPlusPlus/Strings/Bad-or-Good-string.cpp @@ -0,0 +1,104 @@ +/* +Program to check whether a string is bad or good +Criteria: +1: If a string contain 3 more than three constant or more than 5 vowels continuously than it is a bad string else good +2: '?' can be replaced by any alphabets means it can be both constant and vowel +*/ +#include +#include +using namespace std; + +//Function to check whether the character is not vowel +int isVowel(char c) +{ + if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u') + return 1; + else + return 0; +} +// Function to check whether the entered string is good or bad +int Bad_or_Good(string s1) +{ +// Initializing all variable with 0 +int vowel=0,res=0,constant=0; +// until the i is less than s1 length run the loop +for(int i=0; i5) + { + res=1; + break; + } + //assign 0 to res + else + res=0; + } + // if s[i]th character is '?' +else if(s1[i] == '?') + { + //add 1 in both constant and vowel + constant++; + vowel++; + // check whether it touch the bad string criteria or not + if(constant>3||vowel>5) + { + res=1; + break; + } + else res=0; + } + // if the s[i]th character is not a vowel nor '?' that means it is a constant + else + { + //assign 0 to vowel and increment constant + vowel = 0; + constant++; + // check whether the string touch the bad string criteria or not + if(constant>3) + { + res=1; + break; + } + else + res=0; + } + } +return res; +} +//driver code +int main() +{ + int t,res; + string s1; + cout<<"Enter the number of test case: "; + cin>>t; + //cleaning buffer and going to a new line + cin.ignore(1, '\n'); + // until t is not equal to 0 + while(t--) + { + cout<<"Enter string: "; + //taking input + getline(cin, s1); + //calling Bad_or_Good Function to check whether the string is bad or good + res=Bad_or_Good(s1); + if(res==1) + cout<<0<