Complete the solution so that it returns true if the first argument(string) passed in ends with the 2nd argument (also a string).
Examples:
solution("abc", "bc") # returns true
solution("abc", "d") # returns false
#include <stdio.h> #include <stdlib.h> //#include <string.h> #include <stdbool.h> bool solution(const char *string, const char *ending) { bool war=true; int dl1=strlen(string); int dl2=strlen(ending); if (dl2>dl1) return false; dl1-=1; dl2-=1; for (int i=dl2;i>=0;i--) { if(string[dl1--]!=ending[i]) war=false; } return war; } int main() { bool wynik=solution("abc","bc"); printf("wynik %d\n",wynik); wynik=solution("abc","d"); printf("wynik2 %d\n",wynik); return 0; }
2
Are the numbers in order?
In this Kata, your function receives an array of integers as input. Your task is to determine whether the numbers are in ascending order. An array is said to be in ascending order if there are no two adjacent integers where the left integer exceeds the right integer in value.
For the purposes of this Kata, you may assume that all inputs are valid, i.e. non-empty arrays containing only integers.
Note that an array of 1 integer is automatically considered to be sorted in ascending order since all (zero) adjacent pairs of integers satisfy the condition that the left integer does not exceed the right integer in value. An empty list is considered a degenerate case and therefore will not be tested in this Kata - feel free to raise an Issue if you see such a list being tested.
For example:
in_asc_order({1,2,4,7,19}, 5); // returns true
in_asc_order({1,2,3,4,5}, 5); // returns true
in_asc_order({1,6,10,18,2,4,20}, 7); // returns false
in_asc_order({9,8,7,6,5,4,3,2,1}, 9); // returns false because the numbers are in DESCENDING order
#include <stdio.h> #include <stdlib.h> #include <stdbool.h> const int arr0[] = { 1,2,4,7,19 }; const int arr1[] = { 1,2,3,4,5 }; const int arr2[] = { 1,6,10,18,2,4,20 }; const int arr3[] = { 9,8,7,6,5,4,3,2,1 }; bool in_asc_order(const int *arr, size_t arr_size) { bool wyn=true; if (arr_size==1) return true; arr_size-=1; for (int i=0;i<arr_size;i++) { if(arr[i+1]<=arr[i]) wyn=false; } return wyn; } int main() { printf("wynik1= %d\n",in_asc_order(arr0,5)); printf("wynik1= %d\n",in_asc_order(arr1,5)); printf("wynik1= %d\n",in_asc_order(arr2,7)); printf("wynik1= %d\n",in_asc_order(arr3,9)); return 0; }
3
Write a function called repeatStr which repeats the given string string exactly n times.
repeatStr(6, "I") // "IIIIII"
repeatStr(5, "Hello") // "HelloHelloHelloHelloHello"
#include <stdio.h> #include <stdlib.h> #include <string.h> char *repeat_str(size_t count, char *src) { int dl=strlen(src); dl=count*dl; char* nowy=malloc(dl); strcpy(nowy,""); for (int i=0;i<count;i++) strcat(nowy,src); return nowy; } int main(void) { printf("%s\n",repeat_str(5,"ABC")); return 0; }.
4
The number n is Evil if it has an even number of 1's in its binary representation.
The first few Evil numbers: 3, 5, 6, 9, 10, 12, 15, 17, 18, 20
The number n is Odious if it has an odd number of 1's in its binary representation.
The first few Odious numbers: 1, 2, 4, 7, 8, 11, 13, 14, 16, 19
You have to write a function that determine if a number is Evil of Odious, function should return "It's Evil!" in case of evil number and "It's Odious!" in case of odious number.
#include <stdio.h> #include <string.h> #include <stdlib.h> const char *evil(int value) { unsigned int count = 0; while (value) { count += value & 1; value >>= 1; } if (count%2) return "It's Odious!"; else return "It's Evil!"; } int main(void) { printf("%s\n",evil(1)); printf("%s\n",evil(2)); printf("%s\n",evil(3)); printf("%s\n",evil(8)); printf("%s\n",evil(14)); return 0; }
5 Sum of Triangular Numbers
Your task is to return the sum of Triangular Numbers up-to-and-including the nth Triangular Number.
Triangular Number: "any of the series of numbers (1, 3, 6, 10, 15, etc.) obtained by continued summation of the natural numbers 1, 2, 3, 4, 5, etc."
[01]
02 [03]
04 05 [06]
07 08 09 [10]
11 12 13 14 [15]
16 17 18 19 20 [21]
e.g. If 4 is given: 1 + 3 + 6 + 10 = 20.
Triangular Numbers cannot be negative so return 0 if a negative number is given.
#include <stdio.h> #include <string.h> #include <stdlib.h> int sumTriangularNumbers(int n) { if (n<=0) return 0; int temp[n]; for (int i=0;i<n;i++) temp[i]=i+1; int parzyste=n%2 ? 1:0; int m=n; m>>=1; int sum=temp[m]+temp[m-1]; sum=sum*(m+parzyste); int sum2=sum; m=n; for (int i=1;i<n;i++) { sum=sum-m; m=m-1; sum2=sum2+sum; } return sum2; } int main(void) { //printf("%d\n",sumTriangularNumbers(1)); //printf("%d\n",sumTriangularNumbers(2)); //printf("%d\n",sumTriangularNumbers(3)); //printf("%d\n",sumTriangularNumbers(4)); printf("%d\n",sumTriangularNumbers(4)); //printf("%d\n",sumTriangularNumbers(6)); //printf("%d\n",sumTriangularNumbers(7)); return 0; }
6 Count all the sheep on farm in the heights of New Zealand
Every week (Friday and Saturday night), the farmer and his son count amount of sheep returned to the yard of their farm.
They count sheep on Friday night, the same goes for Saturday (suppose that sheep returned on Friday are not feeding back on hills on Saturday).
As sheep are not coming in one flock, you will be given two arrays (one for each night) representing number of sheep as they were returning to the yard during the evenings (entries are positive ints, higher than zero).
Farmer and his son know the total amount of their sheep, you will be given this number as third parameter.
Your goal is to calculate the amount of sheep lost (not returned) to the farm after Saturday night counting.
Example 1: Input: {1, 2}, {3, 4}, 15 --> Output: 5
Example 2: Input: {3, 1, 2}, {4, 5}, 21 --> Output: 6
#include<stdio.h> #include<string.h> // friday and saturday are 0-terminated arrays, i.e. the last element (and only that) will be 0. int lostSheep(const int *friday, const int* saturday, int total) { int sum=0; while(*friday) { sum=sum+*friday; friday++; } while(*saturday) { sum=sum+*saturday; saturday++; } return total-sum; } int friday1[]={1,2,0}; int saturday1[]={3,4,0}; int friday2[]={3,1,2,0}; int saturday2[]={3,4,0}; int main(void) { printf("%d\n",lostSheep(friday1,saturday1,15)); printf("%d\n",lostSheep(friday2,saturday2,21)); return 0; }
7 Exclamation marks series #4: Remove all exclamation marks from sentence but ensure a exclamation mark at the end of string
Remove all exclamation marks from sentence but ensure a exclamation mark at the end of string. For a beginner kata, you can assume that the input data is always a non empty string, no need to verify it.
Examples
remove("Hi!") === "Hi!"
remove("Hi!!!") === "Hi!"
remove("!Hi") === "Hi!"
remove("!Hi!") === "Hi!"
remove("Hi! Hi!") === "Hi Hi!"
remove("Hi") === "Hi!"
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <stddef.h> char* remove_bang(char* s) { int dl=strlen(s); char *wynik = malloc(dl+1); int j=0; for (int i=0;i<dl;i++) { if (s[i]==33) wynik[j]='\0'; else { wynik[j++]=s[i]; wynik[j]='\0'; } } strcat(wynik,"!"); return wynik; } int main(void) { printf("%s\n",remove_bang("Hi!")); printf("%s\n",remove_bang("Hi!!!")); printf("%s\n",remove_bang("!Hi")); printf("%s\n",remove_bang("!Hi!")); printf("%s\n",remove_bang("Hi! Hi!")); printf("%s\n",remove_bang("Hi")); return 0; }
8 Sum without highest and lowest number
Sum all the numbers of the array (in F# and Haskell you get a list) except the highest and the lowest element (the value, not the index!).
(The highest/lowest element is respectively only one element at each edge, even if there are more than one with the same value!)
Example:
{ 6, 2, 1, 8, 10 } => 16
{ 1, 1, 11, 2, 3 } => 6
If array is empty, null or None, or if only 1 Element exists, return 0.
Note:In C++ instead null an empty vector is used. In C there is no null. ;-)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <stddef.h> int tab1[]={6,2,1,8,10}; int tab2[]={1,1,11,2,3}; int sum(int* numbers, int numbersCount) { if (numbersCount==1) return 0; int temp; int zamiana=1; while(zamiana) { zamiana=0; for(int i=0;i<numbersCount-1;i++) { if (numbers[i]>numbers[i+1]) { temp=numbers[i]; numbers[i]=numbers[i+1]; numbers[i+1]=temp; zamiana=1; } } } temp=0; for (int i=1;i<numbersCount-1;i++) temp+=numbers[i]; return temp; } int main(void) { printf("%d\n",sum(tab1,5)); printf("%d\n",sum(tab2,5)); return 0; }
9 Powers of i
i is the imaginary unit, it is defined by i² = -1, therefore it is a solution to x²+1=0.
Your Task
is to write a function pofi that returns i to the power of a given non-negative integer in its simplest form as a string (answer may contain i).
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stdbool.h> #include <stddef.h> char * pofi(unsigned n) { static const char *solution[] = {"1","i","-1","-i"}; return solution[n % 4]; } int main() { printf("%s\n",pofi(0)); printf("%s\n",pofi(1)); printf("%s\n",pofi(2)); printf("%s\n",pofi(3)); printf("%s\n",pofi(4)); return 0; }
10 Partial Word Searching
Write a method that will search an array of strings for all strings that contain another string, (bez ego!! ignoring capitalization.) Then return an array of the found strings.
The method takes two parameters, the query string and the array of strings to search, and returns an array.
If the string isn't contained in any of the strings in the array, the method returns an array containing a single string: "Empty" (or Nothing in Haskell, or "None" in Python and C)
Examples
If the string to search for is "me", and the array to search is ["home", "milk", "Mercury", "fish"], the method should return ["home", "Mercury"].
#include <string.h> #include <stdio.h> #include <stdlib.h> #include <stddef.h> size_t ResultCount=1; const char *string[] = { "home", "milk", "mercury", "fish", "melania" }; const char *quer = "me"; char ** word_search(const char *query,const char **strings,size_t count, size_t *pResultCount) { char **tablica=malloc(count); *pResultCount=0; for(int i=0;i<(int)count;i++) { if(strstr(strings[i],query)) { tablica[*pResultCount]=malloc(strlen(strings[i])+1); strcpy(tablica[*pResultCount],strings[i]); (*pResultCount)++; } } return tablica; } int main() { for (int i=0;i<ResultCount;i++) printf("%s \n", word_search(quer, string, sizeof(string)/sizeof(string[0]),&ResultCount)[i] ); return 0; }