diff --git a/challenge_12/c/karanchawla/README.md b/challenge_12/c/karanchawla/README.md new file mode 100644 index 000000000..59d482518 --- /dev/null +++ b/challenge_12/c/karanchawla/README.md @@ -0,0 +1,8 @@ +``` +Karan Chawla +Challenge #12 +``` + +## Approach +Implemented a counter array to count the occurence of each character in the input string and returned the output in the +given format to the console. \ No newline at end of file diff --git a/challenge_12/c/karanchawla/challenge_12.c b/challenge_12/c/karanchawla/challenge_12.c new file mode 100644 index 000000000..d68cc5403 --- /dev/null +++ b/challenge_12/c/karanchawla/challenge_12.c @@ -0,0 +1,54 @@ +/* +Karan Chawla +Challenge 12 +*/ + +#include +#include +#include + +#ifndef MAX +#define MAX 256 +#endif + + +//function that counts the ocurrence of each element in the string +void zip(const char *a, size_t size) +{ + int count[MAX] = { 0 }; + int flag[MAX]; + + for(int i=0;i1 && flag[(int)a[i]]==1) + { + printf("%c#%d",a[i],count[(int)a[i]]); + flag[a[i]]=0; + } + else if(count[(int)a[i]]==1) + printf("%c",a[i]); + } + + return; +} + +//driver program +int main(void) +{ + //example 1 + const char *str = "The quick brown fox jumped over the lazy dog."; + size_t size = strlen(str); + + //function call to zip + zip(str,size); + + return 0; +} \ No newline at end of file