Write a C++ program. Create a functions that prints a c-string (char array) length with…
- User
Write a C++ program. Create a functions that prints a c-string (char array) length with and without spaces. Create a function that counts consonants in the string. Create a function that cyclically shifts the string by 1 character forward (for example "abcdef" has to transform into "bcdefa".)
- Assistant
Here's a C++ program that includes the required functions:
#include <iostream> #include <cstring> void printLength(const char* str) { int lengthWithSpaces = strlen(str); std::cout << "Length (with spaces): " << lengthWithSpaces << std::endl; int lengthWithoutSpaces = 0; for (int i = 0; i < lengthWithSpaces; i++) { if (str[i] != ' ') { lengthWithoutSpaces++; } } std::cout << "Length (without spaces): " << lengthWithoutSpaces << std::endl; } int countConsonants(const char* str) { int count = 0; int length = strlen(str); for (int i = 0; i < length; i++) { char ch = std::tolower(str[i]); if ((ch >= 'a' && ch <= 'z') && ch != 'a' && ch != 'e' && ch != 'i' && ch != 'o' && ch != 'u') { count++; } } return count; } void cyclicallyShiftString(char* str) { int length = strlen(str); char temp = str[0]; for (int i = 0; i < length - 1; i++) { str[i] = str[i + 1]; } str[length - 1] = temp; } int main() { char str[] = "abcdef"; // Print length printLength(str); // Count consonants int consonantCount = countConsonants(str); std::cout << "Consonants count: " << consonantCount << std::endl; // Cyclically shift string cyclicallyShiftString(str); std::cout << "Cyclically shifted string: " << str << std::endl; return 0; } - User
Now change the countConsonants function so that it uses 2 loops, comparing the given string to the string with all alphabetic vowels "AEIOUYaeiouy". If the given string's character is not equal to any of the vowels in the second string, then increment the consonant count.
- Assistant
Here's the modified
countConsonantsfunction that uses two loops to compare the characters of the given string with a string containing all the alphabetic vowels:int countConsonants(const char* str) { const char* vowels = "AEIOUYaeiouy"; int consonantCount = 0; int length = strlen(str); for (int i = 0; i < length; i++) { bool isVowel = false; for (int j = 0; j < strlen(vowels); j++) { if (str[i] == vowels[j]) { isVowel = true; break; } } if (!isVowel) { consonantCount++; } } return consonantCount; }Now, the
countConsonantsfunction iterates through the characters of the given string and checks if each character matches any of the vowels in the string. If the character is not equal to any vowel, the consonant count is incremented. - User
Write a C++ program. Initialize a 2d array with natural numbers. Print it in following ways: 1. By rows, starting from the 1st row 2. By rows, starting from the last row 3. By columns, starting from the 1st column 4. By columns, starting from the last column 5. print it like snake-game starting from the first column (vertically)
- Assistant
- User
modify the last printing method so that it prints the matrix in snake patern
- Assistant
To modify the
printSnakeGameStylefunction to print the matrix in a snake pattern, where each row starts on the opposite side of the previous row, you can use an additional variable to control the direction of the iteration. Here's the modified function:void printSnakeGameStyle(int arr[][COLS], int numRows) { std::cout << "Printing in snake-game style, starting from the first column (vertically):" << std::endl; for (int j = 0; j < COLS; j++) { if (j % 2 == 0) { for (int i = 0; i < numRows; i++) { std::cout << arr[i][j] << " "; } } else { for (int i = numRows - 1; i >= 0; i--) { std::cout << arr[i][j] << " "; } } } std::cout << std::endl; }In this modified version, when the column index is even (
j % 2 == 0), the function iterates from top to bottom (i = 0tonumRows - 1). However, when the column index is odd, the iteration is from bottom to top (i = numRows - 1to0).