cs240/hw/hw2/functions.c
2018-10-15 17:20:57 -04:00

289 lines
8.3 KiB
C

#include <stdlib.h>
#include <limits.h>
#include "functions.h"
//Remember, you are not allowed to use any functions from string.h
/**
* Returns the length of a string.
* Example: "Hello World!" returns 12.
* @param char * s
* @return length of the string
*/
int mystrlen(char * s) {
int i = 0;
while (s[i] != '\0') {
i++;
}
return i;
}
/**
* MAKE SURE TO FIX mystrlen() BEFORE STARTING ANY OTHER FUNCTIONS. SOME FUNCTIONS DEPEND UPON IT
*/
/**
* Copies the contents of one string into previously allocated memory for another string
* Example: dest = mystrcpy(dest, "Hello World");, dest should equal "Hello World"
* @param char * dest (location memory should be copied to)
* @param char * src (location memory should be copied from)
* @return char * dest
*/
char * mystrcpy(char * dest, char * src) {
int len = mystrlen(src);
int i;
for (i = 0; i < len; i++) {
dest[i] = src[i];
//dest[i] = '\0';
}
dest[len] = '\0';
return dest;
}
/**
* Compares two strings and returns either 1 or -1 depending on the first difference in the string or 0 if they are the same
* Example: "Donaldtrump", "Doncorleone" returns -1, "Twitter", "Facebook" returns 1, "word", "word" returns 0
* @param char * s1
* @param char * s2
* @return 0 if strings are the same, 1 if s1 has greater first difference, -1 if s2 has greater first difference
*/
int mystrcmp(char * s1, char * s2) {
int i;
for (i = 0; s1[i] == s2[i] && s1[i] != '\0' && s2[i] != '\0'; i++) {
// Do nothing when characters are the same.
} //&& s1[i] != '\0' && s2[i] != '\0'
if (s1[i] > s2[i]) return 1;
if (s1[i] < s2[i]) return -1;
return 0;
}
/**
* Reverses all of the characters in a string and returns the modified string.
* Example: "Hello World!" becomes "!dlroW olleH".
* Note: Function DOES NOT create a new string. It only manipulates the string passed in as a parameter.
* @param char * str (The string to be reversed)
* @return char * str (The string after it is reversed)
*/
char * reverseString(char* str) {
//find length of the string
int len = mystrlen(str);
//reverse the characters in the string using a for loop
int x = 0;
for (x = 0; x < (len / 2); x++){
char temp = str[x];
str[x] = str[len - x - 1];
str[len - x - 1] = temp;
}
//return the string
return str;
}
/**
* Converts all lower cases characters in a string to be their upper case counterpart and returns the string
* Nonalpahbetic characters and letters already in uppercase should be ignored.
* Example: "Hello World!" becomes "HELLO WORLD!".
* Note: Function DOES NOT create a new string. It only manipulates the string passed in as a parameter.
* @param char * str (The string to be converted to all upper case)
* @return char * str (The string after it is converted to all upper case)
*/
char * stringToUpper (char * str){
//find the length of the string
int len = mystrlen(str);
//find the value needed to be added to a lower case character to convert it to uppercase
int dist = 'a' - 'A';
//convert characters in a string to upper case using a for loop
int x = 0;
for (x = 0; x < len; x++){
if (str[x] >= 'a' && str[x] <= 'z'){ //should be &&
str[x] = str[x] - dist;
}
}
return str;
}
/**
* Counts all of the '1' bits in the binary representation of an integer and returns the number of '1' bits
* Example: 46 in binary is "101110". The function should return 4.
* Returns -1 if the integer is greater than INT_MAX
* @param int num (The number to find the count of '1' bits in)
* @return int bits (The number of '1' bits in the integer passed as a parameter), -1 if num > INT_MAX
*/
int countBits (unsigned int num){
//check to see that input is less than INT_MAX
if (num > INT_MAX){
return -1;
}
int bits = 0;
//Uses while loop to check the value of each bit in "num"
while (num != 0){
if ((num & 1) == 1){
bits++;
}
num >>= 1;
}
return bits;
}
/**
* Determines if an array of integers is a set. A set is a collection of unique values, meaning that an
* element cannot appear more than once. An example of a set is {1, 3, 5}, whereas {2, 2, 4, 6} would not be a set.
* The function returns 1 if the int array is a set, 0 if it is not.
* Example: {3, 4, 5, 34, 23} should return 1. {23, 89, 43, 78, 43} should return 0
* @param int * nums (Array of integers)
* @param int size (The number of elements in the array)
* @return 1 if input array is a set, 0 if it is not
*/
int isSet(int* nums, int size) {
//for loop to iterate through all numbers in array
int x, y = 0;
for (x = 0; x < size; x++){
//for loop to check if there are duplicates in the array
//int y = 0;
for (y = x + 1; y < size; y++){ //should be x + 1
if (nums[x] == nums[y]){
return 0;
}
}
}
return 1;
}
/**
* This function returns the number of trailing zeroes at the end of num! Only inputs in the range [0,20] will be accepted
* Reminder that n! is equal to n * (n - 1) * (n - 2) ... * 1. 0! and 1! are both equal to 1. Eg: 4! is equal to 4 * 3 * 2 * 1
* Example: 10 as a parameter should return 2
* @param int num (number to find trailing zeroes at the end of num!)
* @return int zeroes (number of trailing zeroes at the end of num!), -1 if num is out of range
*/
int factorialZeroes(int num) {
//check that num is within range
if (num < 0 || num > 20){
return -1;
}
long int fact = 1;
int zeroes = 0;
//calculate num! using while loop
while (num > 0){
fact = fact * num;
num--;
}
//find number of trailing zeroes at the end of num! using while loop
while (fact % 10 == 0){
zeroes++;
fact /= 10; // get rid of the last digit of factorial
}
return zeroes;
}
/**
* This function checks to see if an input string is a palindrome, meaning that the revered string matches the original.
* Eg: "racecar" is a palindrome, "meme" is not a palindrome
* Returns 1 if it is a palindrome, 0 if it is not.
* Example: "abba" should return 1. "donald" should return 0.
* char * string (string to check)
* @return 1 if string is a palindrome, 0 if it is not
*/
int isPalindrome(char * string){
//find the length of the string
int len = mystrlen(string);
//check the characters in the string
int x = 0;
for (x = 0; x < len; x++){
if (string[x] != string[len - (x + 1)]){ // wrong index on right hand side
return 0;
}
}
return 1;
}
/**
* Determines if 2 strings are anagrams of each other, meaning that they both contain all of the same letters
* Returns 1 if the strings are anagrams, 0 otherwise
* Note: The function only checks lower case letters from 'a' to 'z'. No other characters will be given as input
* Example: "test" and "etts" should return 1, "car" and "cat" should return 0
* @param char * str1 (only contains lower case characters)
* @param char * str2 (only contains lower case characters)
* @return 1 if two strings are anagrams, 0 if they are not
*/
int checkAnagram(char* str1, char* str2) {
//check to see if both strings are NULL
if(str1 == NULL && str2 == NULL){
return 1;
}
//find lengths of both strings
int len1 = mystrlen(str1);
int len2 = mystrlen(str2);
if(len1 != len2){
return 0;
}
//initialize array of size 26 to represent the letters of the alphabet and set all values to 0
//Eg: alphabet[0] corresponds to 'a' , alphabet[1] corresponds to 'b'
int alphabet[26]={0};
//change values in alphabet array as they appear in the input strings
int i = 0;
for(i = 0; i < len1; i++){
alphabet[str1[i] - 'a']++;
alphabet[str2[i] - 'a']--; //should be --
}
//check to see if all elements in the array are still 0
for(i = 0; i < 26; i++){
if(alphabet[i] != 0){
return 0;
}
}
return 1;
}
/**
* This function sorts an array and returns the sorted array
* @param int * array (the unsorted array)
* @param int * result (empty array, returns the sorted array)
* @param int size (number of elements in the array)
* @return sorted array
*/
void bubbleSort(int * array, int * result, int size) {
int i, j;
int temp;
for (i = 0; i < size; i++) {
result[i] = array[i];
}
for (i = size - 1; i > 0; i--) {
for (j = 0; j < i; j++) { // array out of bounds error
if (result[j] > result[j + 1]) {
temp = result[j];
result[j] = result[j + 1];
result[j + 1] = temp;
}
}
}
}