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 titlepull/229/head
parent
0c6d011ab1
commit
390fc888bd
|
@ -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)
|
|
@ -1,40 +1,40 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
int n;
|
int n;
|
||||||
printf("Enter n : "); //n is number of elements in the array
|
printf("Enter n : "); //n is number of elements in the array
|
||||||
scanf("%d", &n);
|
scanf("%d", &n);
|
||||||
int arr[n];
|
int arr[n];
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
scanf("%d", &arr[i]);
|
scanf("%d", &arr[i]);
|
||||||
}
|
}
|
||||||
int arr_odd[n], arr_even[n]; //arr_odd[] for odd elements and arr_even[] for even elements
|
int arr_odd[n], arr_even[n]; //arr_odd[] for odd elements and arr_even[] for even elements
|
||||||
int o = 0, e = 0;
|
int o = 0, e = 0;
|
||||||
for (int i = 0; i < n; i++)
|
for (int i = 0; i < n; i++)
|
||||||
{
|
{
|
||||||
if (arr[i] % 2 != 0)
|
if (arr[i] % 2 != 0)
|
||||||
{
|
{
|
||||||
arr_odd[o] = arr[i]; //Putting odd numbers in arr_odd[] array.
|
arr_odd[o] = arr[i]; //Putting odd numbers in arr_odd[] array.
|
||||||
o++;
|
o++;
|
||||||
}
|
}
|
||||||
else if (arr[i] % 2 == 0)
|
else if (arr[i] % 2 == 0)
|
||||||
{
|
{
|
||||||
arr_even[e] = arr[i]; //Putting even numbers in arr_even[] array.
|
arr_even[e] = arr[i]; //Putting even numbers in arr_even[] array.
|
||||||
e++;
|
e++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("Even array : [ ");
|
printf("Even array : [ ");
|
||||||
for (int i = 0; i < e; i++)
|
for (int i = 0; i < e; i++)
|
||||||
{
|
{
|
||||||
printf("%d ", arr_even[i]);
|
printf("%d ", arr_even[i]);
|
||||||
}
|
}
|
||||||
printf("]\nOdd array : [ ");
|
printf("]\nOdd array : [ ");
|
||||||
for (int i = 0; i < o; i++)
|
for (int i = 0; i < o; i++)
|
||||||
{
|
{
|
||||||
printf("%d ", arr_odd[i]);
|
printf("%d ", arr_odd[i]);
|
||||||
}
|
}
|
||||||
printf("]");
|
printf("]");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -1,30 +1,30 @@
|
||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
// A function to check if a string str is palindrome
|
// A function to check if a string str is palindrome
|
||||||
void isPalindrome(char str[])
|
void isPalindrome(char str[])
|
||||||
{
|
{
|
||||||
// Start from leftmost and rightmost corners of str
|
// Start from leftmost and rightmost corners of str
|
||||||
int l = 0;
|
int l = 0;
|
||||||
int h = strlen(str) - 1;
|
int h = strlen(str) - 1;
|
||||||
|
|
||||||
// Keep comparing characters while they are same
|
// Keep comparing characters while they are same
|
||||||
while (h > l)
|
while (h > l)
|
||||||
{
|
{
|
||||||
if (str[l++] != str[h--])
|
if (str[l++] != str[h--])
|
||||||
{
|
{
|
||||||
printf("%s is Not Palindrome", str);
|
printf("%s is Not Palindrome", str);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("%s is palindrome", str);
|
printf("%s is palindrome", str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Driver program to test above function
|
// Driver program to test above function
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
isPalindrome("abba");
|
isPalindrome("abba");
|
||||||
isPalindrome("abbccbba");
|
isPalindrome("abbccbba");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
|
@ -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<iostream>
|
||||||
|
#include <string>
|
||||||
|
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; i<s1.length(); i++)
|
||||||
|
{
|
||||||
|
//if i th character of string is vowel
|
||||||
|
if(isVowel(s1[i]))
|
||||||
|
{
|
||||||
|
// assign 0 to constant
|
||||||
|
constant=0;
|
||||||
|
// increment vowel
|
||||||
|
vowel++;
|
||||||
|
// check whether a bad string if yes assign 1 to res and break
|
||||||
|
if(vowel>5)
|
||||||
|
{
|
||||||
|
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<<endl;
|
||||||
|
else
|
||||||
|
cout<<1<<endl;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
Input: Enter string: aeiou??
|
||||||
|
Output: 0(bad string)
|
||||||
|
Time complexity: O(N)
|
|
@ -1,8 +0,0 @@
|
||||||
|
|
||||||
|
|
||||||
# Algorithms related to arrays
|
|
||||||
|
|
||||||
### C
|
|
||||||
|
|
||||||
1. [Unique Elements in an Array](c-or-cpp/unique-elements-in-an-array.c)
|
|
||||||
2. [Even and odd no. ](c-or-cpp/even-and-odd.c)
|
|
|
@ -1,6 +0,0 @@
|
||||||
# Graphs
|
|
||||||
|
|
||||||
### C
|
|
||||||
|
|
||||||
1. [Prim's Algorithm](c-or-cpp/Prim's-algorithm.c)
|
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
# Linked Lists
|
|
||||||
|
|
||||||
### C
|
|
||||||
|
|
||||||
1. [Josephus Problem Using Circular Linked List](c-or-cpp/josephus-problem.c)
|
|
|
@ -1,5 +0,0 @@
|
||||||
# Queues
|
|
||||||
|
|
||||||
### C
|
|
||||||
|
|
||||||
1. [Double Ended Queue (using arrays)](c-or-cpp/double-ended-queue-using-array.c)
|
|
|
@ -1,5 +0,0 @@
|
||||||
# Sorting algorithms
|
|
||||||
|
|
||||||
### C
|
|
||||||
|
|
||||||
1. [Merge Sort](c-or-cpp/merge-sort.c)
|
|
|
@ -1,7 +0,0 @@
|
||||||
# String Algorithms
|
|
||||||
|
|
||||||
### C or C++
|
|
||||||
|
|
||||||
1. [Palindrome Check](c-or-cpp/palindrome.c)
|
|
||||||
2. [Permutation of String](c-or-cpp/Permutation-of-String.c)
|
|
||||||
3. [Count Words](c-or-cpp/count-words.c)
|
|
Loading…
Reference in New Issue