#include "types.h" #include void read_print_string() { // TODO: 1. Read a string from stdin of max-length of 100 characters. printf("Enter a string (100 chars max):\n"); char input[101]; scanf("%100[^\n]", input); // TODO: 2. Print read string to stdout. printf("%s\n", input); return; } void read_print_string_length() { // TODO: 1. Read a string from stdin of max-length of 100 characters. printf("Enter a string (100 chars max):\n"); char input[101]; scanf(" %100[^\n]", input); // TODO: 2. Print the length of the read string to stdout. (do not count // '\0'). for (int i = 0; i < 101; ++i) { if (input[i] == '\0') { printf("Length of your string is %d", i); break; } } return; } void read_print_string_alphnum() { // TODO: 1. Read a string from stdin of max-length of 100 characters. printf("Enter a string (100 chars max):\n"); char input[101]; scanf(" %100[^\n]", input); // TODO: 2. Print both the number of letters (a-z, A-Z) and the number of // numerals (0-9) in the read string to stdout. int letters = 0; int numbers = 0; for (int i = 0; i < 101; ++i) { if (input[i] == '\0') { break; } if ((input[i] >= 'a' && input[i] <= 'z') || (input[i] >= 'A' && input[i] <= 'Z')) { letters++; } else if (input[i] >= '0' && input[i] <= '9') { numbers++; } } printf("Your string contains %d letters and %d numbers", letters, numbers); return; } void read_print_string_lower() { // TODO: 1. Read a string from stdin of max-length of 100 characters. printf("Enter a string (100 chars max):\n"); char input[101]; scanf(" %100[^\n]", input); // TODO: 2. Print the read string with all letters in lowercase (e.g., A => a) // to stdout. for (int i = 0; i < 101; ++i) { if ((input[i] >= 'A' && input[i] <= 'Z')) { input[i] += 32; } } printf("%s\n", input); return; } void read_print_string_replace() { // TODO: 1. Read a character and a string of max-length of 100 characters from // stdin. printf("Enter a single character:\n"); char c; scanf(" %c", &c); printf("Enter a string (100 chars max):\n"); char input[101]; scanf(" %100[^\n]", input); // TODO: 2. Print the read string with all occurences of read string replaced // with '_' (e.g., ('a', "abba") => _bb_) to stdout. for (int i = 0; i < 101; ++i) { if (input[i] == '\0') { break; } if (input[i] == c) { input[i] = '_'; } } printf("%s\n", input); return; } void read_print_string_compare() { // TODO: 1. Read 2 string, each of max-length of 100 characters, from stdin. printf("Enter a string (100 chars max):\n"); char inputA[101]; scanf(" %100[^\n]", inputA); printf("Enter another string (100 chars max):\n"); char inputB[101]; scanf(" %100[^\n]", inputB); // TODO: 2. Determine whether strings are equal (two strings are equal if they // have the same length and characters in both strings on a given index are // identical). int is = 1; for (int i = 0; i < 101; ++i) { if (inputA[i] == '\0' && inputB[i] == '\0') { break; } if (inputA[i] != inputB[i] || (inputA[i] == '\0') || (inputB[i] == '\0')) { is = 0; break; } } printf("%s", (is == 1) ? "The two strings ARE equal" : "The two strings are NOT equal"); return; } #ifndef TEST_BUILD int main(int argc, char *argv[]) { printf("-- read_print_string --\n"); read_print_string(); printf("\n-- read_print_string_length --\n"); read_print_string_length(); printf("\n-- read_print_string_alphnum --\n"); read_print_string_alphnum(); printf("\n-- read_print_string_lower --\n"); read_print_string_lower(); printf("\n-- read_print_string_replace --\n"); read_print_string_replace(); printf("\n-- read_print_string_compare --\n"); read_print_string_compare(); return 0; } #endif