--- tags: lab, exam, c, Uob --- # 20201112 lab exam ## a1 ``` #include "common.h" char *strnodupes(const char *s) { int i, j, current = 0; int len = strlen(s); char *str = malloc((len + 1) * sizeof(char)); bool hasSame; for (i = 0; i < len; i++) { hasSame = false; for (j = 0; j < len; j++) { if (s[i] == str[j]) { hasSame = true; } } if (hasSame == false) { str[current++] = s[i]; } } str[current] = '\0'; return str; } ``` ## a2 ``` #include "common.h" void strtogglealpha(char *s, const char c) { int i, current = 0; int len = strlen(s); for (i = 0; i < len; i++) { if (c >= 'A' && c <= 'Z') { if (tolower(s[i]) == tolower(c)) { if (s[i] >= 'a' && s[i] <= 'z') { s[current++] = toupper(s[i]); } else if (c >= 'A' && c <= 'Z') { s[current++] = tolower(s[i]); } } else { s[current++] = s[i]; } } if (c >= 'a' && c <= 'z') { if (tolower(s[i]) == tolower(c)) { s[current++] = toupper(s[i]); } else { s[current++] = s[i]; } } } } ``` ## a3 ``` #include "common.h" int isvowel(int c); void strflagvowel(const char *s1, char *s2) { int i, current = 0; int len = strlen(s1); for (i = 0; i < len; i++) { if (isvowel(s1[i]) == 1) { s2[current] = '*'; current++; s2[current] = s1[i]; current++; s2[current] = '*'; current++; } else { s2[current] = s1[i]; current++; } } s2[current] = '\0'; } int isvowel(int c) { if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' || c == 'E' || c == 'I' || c == 'O' || c == 'U') { return 1; } else { return 0; } } ``` ## b1 ``` #include "common.h" bool negativeedge(int a[][10], int h) { int row, col; for (row = 0; row < h; row++) { if (row > 0 && row < h - 1) { for (col = 0; col < 9; col++) { if (col > 0 && col < 8) { if (a[row][col] < 0) { return false; } } } } } return true; } ``` ## b2 ``` #include "common.h" #define LAST_IDX 8 bool leftright(bool a[][9], int h) { int row, col; if (h == 1) { return true; } for (row = 0; row < h; row++) { for (col = 0; col < 9; col++) { if (a[row][col] != a[row][LAST_IDX - col]) { return false; } } } return true; } ``` ## b3 ``` #include "common.h" bool samerows(bool a[][9], int h) { int i, j, k = 0; bool isSame; if (h == 1) { return false; } for (i = 0; i < h; i++) { for (j = 0; j < h; j++) { if (j != i) { k = 0; isSame = true; while (k < 9) { if (a[j][k] != a[i][k]) { isSame = false; } k++; } if (isSame == true) { return true; } } } } return false; } ```