Question
Please write the program in C using scanf, printf, #include This lab focuses on the manipulation of c-strings. The user will be prompted to enter a word. They will then be given a menu with the following options: 1. Pig Latin 2. Count Vowels & Consonants 3. Quit MENU ITEM 1: Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form pig-Latin phrases. For simplicity, use the following algorithm: 1. Move the first letter of the word to the end 2. Add the letters
Explanation / Answer
#include #include #define MAX_STRING_LEN 80 int main() { /* strings are array of characters * terminated by the NULL character * which is different from '0' */ char S[MAX_STRING_LEN]; int l, i; S[0] = 'a'; S[1] = 'b'; S[2] = 'c'; S[3] = 'd'; S[4] = 'e'; S[5] = 'g'; S[6] = '0'; S[7] = 0; l = strlen(S); printf("S: %s ",S); printf("length: %d ",l); /* print characters in S */ printf("forward "); for (i = 0; i = 0; --i) printf("A[%d] = %c ",i,S[i]); } Program 3: String IO #include #include #define MAX_STRING_LEN 80 int main() { /* strings can be read using scanf with %s * string.h contains many useful functions * for working with strings: * strcmp for string comparisons * strcpy to copy strings * ... * * Check what happens if your input contains * spaces, tabs, etc * */ char S1[MAX_STRING_LEN]; char S2[MAX_STRING_LEN]; int i, l; printf("String: "); scanf("%s",S1); /* we need to copy all the characters, and * the final NULL character! */ l = strlen(S1); /* rather than writing this loop we could also write strcpy(S2,S1) */ for (i = 0; i