fork download
  1. // Author: Nicholas Dornelles Franceschini
  2. // Filename:
  3. // Date: September, 2014
  4. // Compiler: Quincy 2005 ver. 1.3
  5. // OS: Windows 7 SP1
  6. // Assigned: September 9th
  7. // Due: September 19th
  8. // Description: This program will mimic the cstring header
  9. // file, giving access to functions that will
  10. // count the characters in a string, compare
  11. // strings, copy one string onto another and
  12. // concatenate two strings.
  13. ///////////////////////////////////////////////////////////
  14.  
  15.  
  16. // System header files
  17. #include <iostream>
  18.  
  19.  
  20. // C-String Function Prototypes
  21. int ndfStrLen(const char * const);
  22. void ndfStrCpy(char * const, const char * const);
  23. void ndfStrCat(char [], const char []);
  24. int ndfStrCmp(const char [], const char []);
  25. void ndfOutLen(const char * const, const char * const);
  26.  
  27.  
  28. // control function
  29. int main()
  30. {
  31. char array1[100] = "lol";
  32. char array2[100] = "lol";
  33.  
  34. std::cout << "Please enter two strings. Hit enter after each one." << std::endl;
  35. std::cin.getline(array1, 99, '\n');
  36. std::cin.getline(array2, 99, '\n');
  37. std::cout << std::endl;
  38.  
  39. ndfOutLen(array1, array2);
  40. std::cout << std::endl;
  41.  
  42. ndfStrCmp(array1, array2);
  43. std::cout << std::endl;
  44.  
  45. ndfStrCpy(array2, array1);
  46. std::cout << array1 << std::endl;
  47. std::cout << array2 << std::endl;
  48. std::cout << std::endl;
  49.  
  50. ndfOutLen(array1, array2);
  51. std::cout << std::endl;
  52.  
  53. ndfStrCat(array2, array1);
  54. std::cout << "String \"" << array2 << "\" is " << ndfStrLen(array2) << " characters long." << std::endl;
  55.  
  56. ndfStrCmp(array1, array2);
  57. std::cout << std::endl;
  58.  
  59.  
  60. return 0;
  61. }
  62.  
  63.  
  64. ///////////////////////////////////////////////////////////
  65. // Purpose: This function counts the number of characters
  66. // in a string.
  67. // Input: One char array containing a string.
  68. // Output: An integer of value equal to the number of
  69. // characters, minus the null, in the provided
  70. // char array.
  71. int ndfStrLen(const char * const chArray1)
  72. {
  73. int counter = 0;
  74. char content = '\0';
  75. bool isNull = false;
  76.  
  77. // This loops through the char array until it finds a
  78. // null char. If it does not, it adds one to counter,
  79. // if it does, isNull becomes TRUE, closing the loop
  80. // and returning counter, which will contain a number
  81. // equivalent to the number of chars excluding the \0
  82. while(isNull == false)
  83. {
  84. content = *(chArray1 + counter);
  85. if(content == '\0')
  86. {
  87. isNull = true;
  88. }
  89. counter++;
  90. }
  91.  
  92. // returns the total number of chars in the array minus
  93. // the null char.
  94. return counter - 1;
  95. }
  96.  
  97. ///////////////////////////////////////////////////////////
  98. // Purpose: This function copies the second char array
  99. // into the first, overwriting what it originally
  100. // had in it and assuming it is big enough.
  101. // Input: Two char arrays.
  102. // Output: None, this function will edit the array.
  103. void ndfStrCpy(char * const chArray1, const char * const chArray2)
  104. {
  105. int counter = 0;
  106. char content = '\0';
  107. bool isNull = false;
  108.  
  109. // This loops through the second char array, copying
  110. // the char into the first array, until it finds a
  111. // null char. If it does not; it adds one to counter,
  112. // if it does; isNull becomes TRUE, closing the loop.
  113. while(isNull == false)
  114. {
  115. content = *(chArray2 + counter);
  116. *(chArray1 + counter) = content;
  117. if(content == '\0')
  118. {
  119. isNull = true;
  120. }
  121. counter++;
  122. }
  123.  
  124. return;
  125. }
  126.  
  127. ///////////////////////////////////////////////////////////
  128. // Purpose: This funtion adds the second char array to the
  129. // first one and assumes that there is enough
  130. // space for it.
  131. // Input: Two char arrays.
  132. // Output: None, this function will edit the array.
  133. void ndfStrCat(char chArray1[], const char chArray2[])
  134. {
  135. int counter = 0;
  136. char content = '\0';
  137. bool isNull = false;
  138. int length = 0;
  139.  
  140. // This figures out the size of the first array
  141. length = ndfStrLen(chArray1);
  142.  
  143. // This loops through both arrays, assigning chars from
  144. // second array to the end of the first array.
  145. while(isNull == false)
  146. {
  147. content = chArray2[counter];
  148. chArray1[length + counter] = content;
  149. if(content == '\0')
  150. {
  151. isNull = true;
  152. }
  153. counter++;
  154. }
  155.  
  156. return;
  157. }
  158.  
  159. ///////////////////////////////////////////////////////////
  160. // Purpose: This function will compare 2 char arrays.
  161. // Input: Two char arrays.
  162. // Output: An integer; positive if array 1 is bigger, 0 if
  163. // they are equal or negative is array 2 is bigger
  164. int ndfStrCmp(const char chArray1[], const char chArray2[])
  165. {
  166. int counter = 0;
  167. char contentArr1 = '\0';
  168. char contentArr2 = '\0';
  169. bool isDone = false;
  170. int result = 0;
  171.  
  172.  
  173. while(isDone == false)
  174. {
  175. contentArr1 = chArray1[counter];
  176. contentArr2 = chArray2[counter];
  177.  
  178. if(contentArr1 == '\0' || contentArr2 == '\0')
  179. {
  180. isDone = true;
  181. }
  182. else
  183. {
  184. result = contentArr1 - contentArr2;
  185.  
  186. if(result > 0)
  187. {
  188. isDone = true;
  189. std::cout << chArray1 << " is greater than " << chArray2 << "." << std::endl;
  190. }
  191. else if(result < 0)
  192. {
  193. isDone = true;
  194. std::cout << chArray1 << " is less than " << chArray2 << "." << std::endl;
  195. }
  196. }
  197. if(result == 0)
  198. {
  199. std::cout << "The strings are equal." << std::endl;
  200. }
  201. counter++;
  202. }
  203.  
  204. return result;
  205. }
  206.  
  207. ///////////////////////////////////////////////////////////
  208. // Purpose: This function prints both arrays and their
  209. // lengths.
  210. // Input: Two char arrays.
  211. // Output: Array and size to screen, nothing internally.
  212. void ndfOutLen(const char * const chArray1, const char * const chArray2)
  213. {
  214. std::cout << "String \"" << chArray1 << "\" is " << ndfStrLen(chArray1) << " characters long." << std::endl;
  215. std::cout << "String \"" << chArray2 << "\" is " << ndfStrLen(chArray2) << " characters long." << std::endl;
  216.  
  217. return;
  218. }
Success #stdin #stdout 0.01s 5292KB
stdin
// C program to implement a simple quiz game
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

// max number of questions defined as macro
#define MAX_QUESTIONS 5

// Structure to store question details
typedef struct {
    char question[256];
    char options[4][64];
    int correct_option;
} Question;

// function to display question to the user
void displayQuestion(Question q)
{
    printf("%s\n", q.question);
    for (int i = 0; i < 4; i++) {
        printf("%d. %s\n", i + 1, q.options[i]);
    }
}

// function to check the answer
int checkAnswer(Question q, int user_answer)
{
    return (user_answer == q.correct_option);
}

// driver code
int main()
{

    // random number generator
    srand(time(NULL));

    // Initializing questions, options and the correct
    // answer
    Question original_questions[MAX_QUESTIONS] = {
        { "Which bird lays the largest egg?",
          { "Owl", "Ostrich", "Kingfisher", "Woodpecker" },
          2 },
        { "How many legs does a spider have?",
          { "7", "8", "6", "5" },
          2 },
        { "Where does the President of the United States "
          "live while in office?",
          { "The White House", "The Parliament",
            "House of Commons", "Washington DC" },
          1 },
        { "Which state is famous for Hollywood?",
          { "Sydney", "California", "New York", "Paris" },
          2 },
        { "What is a group of lions called?",
          { "Drift", "Pride", "Flock", "Drove" },
          2 }
    };

    // Array of struct data-type
    Question questions[MAX_QUESTIONS];
    memcpy(questions, original_questions,
           sizeof(original_questions));

    int num_questions = MAX_QUESTIONS;

    int score = 0;

    printf("Hola! Here's your Quiz Game!\n");

    for (int i = 0; i < MAX_QUESTIONS; i++) {
        int random_index = rand() % num_questions;
        Question current_question = questions[random_index];
        displayQuestion(current_question);

        int user_answer;
        printf("Enter your answer (1-4): ");
        scanf("%d", &user_answer);

        if (user_answer >= 1 && user_answer <= 4) {
            if (checkAnswer(current_question,
                            user_answer)) {
                printf("Correct!\n");
                score++;
            }
            else {
                printf("Incorrect. The correct answer is "
                       "%d. %s\n",
                       current_question.correct_option,
                       current_question.options
                           [current_question.correct_option
                            - 1]);
            }
        }
        else {
            printf("Invalid choice. Please enter a number "
                   "between 1 and 4.\n");
        }

        questions[random_index]
            = questions[num_questions - 1];
        num_questions--;
    }

    printf("Well Done Champ !!!! Quiz completed! Your "
           "score: %d/%d\n",
           score, MAX_QUESTIONS);

    return 0;
}
stdout
Please enter two strings. Hit enter after each one.

String "// C program to implement a simple quiz game" is 44 characters long.
String "#include <stdio.h>" is 18 characters long.

// C program to implement a simple quiz game is greater than #include <stdio.h>.

// C program to implement a simple quiz game
// C program to implement a simple quiz game

String "// C program to implement a simple quiz game" is 44 characters long.
String "// C program to implement a simple quiz game" is 44 characters long.

String "// C program to implement a simple quiz game// C program to implement a simple quiz game" is 88 characters long.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.
The strings are equal.