Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added C solutions of Arrays & Strings #12

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Ch 01. Arrays and Strings/1.1_isUnique.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <stdio.h>
#include <string.h>
int isUnique(char*, int);

int main()
{
char s[1000];
printf("Input: ");
scanf("%[^\n]", s);
if(isUnique(s, strlen(s)) == 1)
printf("%s is unique.\n", s);
else
printf("%s is not unique.\n", s);
return 0;
}

int isUnique(char* s, int len)
{
int ascii[128], i;
if(len > 128)
return 0;
for(i = 0; i < 128; i++)
ascii[i] = 0;
for(i = 0; i < len; i++)
{
if(ascii[s[i]] != 0)
return 0;
else
ascii[s[i]]++;
}
return 1;
}
38 changes: 38 additions & 0 deletions Ch 01. Arrays and Strings/1.2_checkPermutation.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include<stdio.h>
#include<string.h>
int checkPermutation(char*, char*, int, int);

int main()
{
char s1[1000], s2[1000];
printf("String 1: ");
scanf("%s", s1);
printf("String 2: ");
scanf("%s", s2);
if(checkPermutation(s1, s2, strlen(s1), strlen(s2)) == 1)
printf("YES\n");
else
printf("NO\n");
return 0;
}

int checkPermutation(char* s1, char* s2, int len1, int len2)
{
int ascii[128], i;
if(len1 != len2)
return 0;
else
{
for(i = 0; i < 128; i++)
ascii[i] = 0;
for(i = 0; i < len1; i++)
ascii[s1[i]]++;
for(i = 0; i < len2; i++)
{
ascii[s2[i]]--;
if(ascii[s2[i]] < 0)
return 0;
}
}
return 1;
}
38 changes: 38 additions & 0 deletions Ch 01. Arrays and Strings/1.3_URLify.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <string.h>
void URLify(char*, int);

int main()
{
char s[1000];
printf("Input: ");
scanf("%[^\n]", s);
printf("URLify(\"%s\") = ", s);
URLify(s, strlen(s));
printf("%s\n", s);
return 0;
}

void URLify(char* s, int len)
{
int i, j = len - 1;
while(s[j] == ' ' && j>=0)
j--;
if(j >= 0)
{
i = len - 1;
for(; j >= 0; j--)
{
if(s[j] == ' ')
{
s[i--] = '0';
s[i--] = '2';
s[i--] = '%';
}
else
s[i--] = s[j];
}
for(j = 0; j < len; j++)
s[j] = s[j+i+1];
}
}
36 changes: 36 additions & 0 deletions Ch 01. Arrays and Strings/1.4_isPermutationOfPalindrome.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <stdio.h>
#include <string.h>
int isPermutationOfPalindrome(char*, int);

int main()
{
char s[1000];
printf("Input: ");
scanf("%[^\n]", s);
if(isPermutationOfPalindrome(s, strlen(s)) == 1)
printf("YES\n");
else
printf("NO\n");
return 0;
}

int isPermutationOfPalindrome(char* s, int len)
{
char ascii[128];
int i, odd = 0;
for(i = 0; i < 128; i++)
ascii[i] = 0;
for(i = 0; i < len; i++)
ascii[s[i]]++;
for(i = 0; i < 128; i++)
{
if(i >= 65 && i <= 90 || i >= 97 && i <=122)
{
if(ascii[i]%2 != 0)
odd++;
if(odd > 1)
return 0;
}
}
return 1;
}
93 changes: 93 additions & 0 deletions Ch 01. Arrays and Strings/1.5_oneAway.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void oneAway(char*, char*, int, int);

int main()
{
char s1[1000], s2[1000];
printf("String 1: ");
scanf("%s", s1);
printf("String 2: ");
scanf("%s", s2);
oneAway(s1, s2, strlen(s1), strlen(s2));
return 0;
}

void oneAway(char* s1, char* s2, int len_a, int len_b)
{
int matches, i, flag1 = 0, flag2 = 0, len = len_a;
if(len_a != len_b && len_a + 1 != len_b && len_a != len_b + 1)
{
printf("More than an edit away.\n");
return;
}
if(len_a > len_b)
len = len_b;
for(i = 0; i < len; i++)
{
if(s1[i + flag1] == s2[i + flag2])
matches++;
else
{
if(len_a == len_b)
{
if(flag1 == 0 && flag2 == 0)
{
flag1++;
flag2++;
}
else
{
printf("More than an edit away.\n");
return;
}
}
else if(len_a == len_b + 1)
{
if(flag1 == 0)
{
flag1++;
i--;
}
else
{
printf("More than an edit away.\n");
return;
}
}
else if(len_a + 1 == len_b)
{
if(flag2 == 0)
{
flag2++;
i--;
}
else
{
printf("More than an edit away.\n");
return;
}
}
else
{
printf("More than an edit away.\n");
return;
}
}
}

if(flag1 == 0 && flag2 == 0 && len_a == len_b)
{
printf("0 edits away.\n");
return;
}
else if((flag1 == 0 && flag2 == 0 && (len_a == len_b + 1 || len_a + 1 == len_b)) || (flag1 == 0 && flag2 == 1 && len_a + 1 == len_b) || (flag1 == 1 && flag2 == 0 && len_a == len_b + 1))
{
printf("1 edit away.\n");
return;
}
else
printf("More than an edit away.\n");
return;
}
47 changes: 47 additions & 0 deletions Ch 01. Arrays and Strings/1.6_compress.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <stdio.h>
#include <string.h>
char* compress(char* s, int len);

int main()
{
char s[1000];
printf("Input: ");
scanf("%s", s);
printf("On compression: %s\n", compress(s, strlen(s)));
return 0;
}

char* compress(char* s, int len)
{
char result[len+1], temp;
int i, j = len - 1, count;
for(i = len - 1; i >= 0;)
{
count = 0;
temp = s[i];
while(s[i] == temp && i >= 0)
{
count++;
i--;
}
while(count > 0)
{
result[j--] = (count%10) + 48;
count /= 10;
if(j == -1)
return s;
}
result[j--] = temp;
if(j == -1 && i != 0)
return s;
}
i = 0;
while(i+j+1 <= len)
{
result[i] = result[i+j+1];
i++;
}
result[len] = '\0';
s = result;
return s;
}
55 changes: 55 additions & 0 deletions Ch 01. Arrays and Strings/1.7_rotate.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <stdio.h>
#include <stdlib.h>
void rotate(int**, int, int);
void rotateEdge(int**, int, int);

int main()
{
int** img, N, i, j;
printf("N: ");
scanf("%d", &N);
img = (int**)malloc(N*sizeof(int*));
for(i = 0; i < N; i++)
img[i] = (int*)malloc(N*sizeof(int));
printf("Enter the image matrix:\n");
for(i = 0; i < N; i++)
for(j = 0; j < N; j++)
scanf("%d", &img[i][j]);
rotate(img, 0, N-1);
printf("\nOn rotation:\n");
for(i = 0; i < N; i++)
{
for(j = 0; j < N; j++)
printf("%d\t", img[i][j]);
printf("\n");
}
return 0;
}

// a: first element index in submatrix of top row, b: last element index in submatrix of top row
void rotate(int** img, int a, int b)
{
if(a < b)
{
rotateEdge(img, a, b);
rotate(img, a+1, b-1);
}
}

void rotateEdge(int** img, int a, int b)
{
int i, temp;
for(i = a; i < b; i++)
{
// store right in temp
// up to right
// left to up
// bottom to left
// temp to bottom
temp = img[i][b];
img[i][b] = img[a][i];
img[a][i] = img[b-(i-a)][a];
img[b-(i-a)][a] = img[b][b-(i-a)];
img[b][b-(i-a)] = temp;
}
}
Loading