Question

In: Computer Science

Write a C program that does the following: 1) Asks the user to enter an integer...

Write a C program that does the following:
1) Asks the user to enter an integer N
2) Asks the user to enter three words with at least N letters and at most 20 letters
3) Prints the first N letters from the first word
4) Prints the last N letters from the second word
5) Prints all letters in the odd position in the third word

Output:
Please enter an integer N: 3
Please enter a word with at least 3 and at most 20 letters: Germany
Please enter a second word with at least 3 and at most 20 letters: China
Please enter a third word with at least 3 and at most 20 letters: Argentina
Germany starts with Ger
China ends with ina
Odd letters in the third word are: A g n i a

Solutions

Expert Solution

C PROGRAM:

#include<stdio.h>

#include <string.h>

// main() function

int main(int argc, char *argv[])

{

    // Declare variable N

    int N;

   // Declare variable word1,word2 and word3

    char word1[20];

    char word2[20];

    char word3[20];

    int len;

    // Asks the user to enter an integer N

    printf("Please enter an integer N: ");

    scanf("%d",&N);

    // Asks the user to enter first word with at least N letters and at most 20 letters

    do{

        printf("Please enter a word with at least %d and at most 20 letters: ",N);

        scanf("%s", word1);

        len = strlen(word1);

        // check input word len greater then N

    }while(len<N);

    // Asks the user to enter second word with at least N letters and at most 20 letters

    do{

         printf("Please enter a second word with at least %d and at most 20 letters: ",N);

         scanf("%s", word2);

        len = strlen(word2);

    // check input word len greater then N

    }while(len<N);

   //  Asks the user to enter third word with at least N letters and at most 20 letters

    do{

        printf("Please enter a third word with at least %d and at most 20 letters: ",N);

        scanf("%s", word3);

        len = strlen(word3);

    // check input word len greater then N

    }while(len<N);

    // Prints the first N letters from the first word

    printf("%s starts with ", word1);

    for(int i=0;i<N;i++){

        printf("%c",word1[i]);

    }

    // Prints the last N letters from the second word

    // for last letters word + strlen(word)-N

    printf("\n%s ends with %s\n", word2, word2 + strlen(word2) - N);

    // Prints all letters in the odd position in the third word

    printf("Odd letters in the third word are: ");

    for(int i=0;i<strlen(word3);i+=2){

        printf("%c ",word3[i]);

}

   return 0;

}

OUTPUT:

NOTE: If you don't understand any step please tell me.


Related Solutions

Question : Write a C program that asks the user to enter an integer between 1...
Question : Write a C program that asks the user to enter an integer between 1 and 7 that represents a weekday number (1 = Sunday, 2 = Monday , …… , 6 = Friday , 7 = Saturday) and it prints the day (i.e., Sunday, Monday, …… , Friday or Saturday). The program continuously asks the user to enter a weekday number till the user responds by ‘N’. and give me an output please use printf and scanf #include...
Write a program that does the following in order: 1. Asks the user to enter a...
Write a program that does the following in order: 1. Asks the user to enter a name 2. Asks the user to enter a number “gross income” 3. Asks the user to enter a number “state tax rate” 4. Calculates the “Federal Tax”, “FICA tax” and “State tax” 5. Calculates the “estimated tax” and round the value to 2 decimal places 6. Prints values for “name”, “gross income” and “estimated tax” The program should contain three additional variables to store...
Write a Java program that asks the user to enter an integer that is used to...
Write a Java program that asks the user to enter an integer that is used to set a limit that will generate the following four patterns of multiples of five using nested loops •Ascending multiples of five with ascending length triangle •Ascending multiples of five with descending length (inverted) triangle •Descending multiples of five with ascending length triangle •Descending multiples of five with descending length (inverted) triangle Use error checking to keep asking the user for a positive number until...
Write a program that does the following. It will ask the user to enter an integer...
Write a program that does the following. It will ask the user to enter an integer larger than 1, and the if entered integer is not larger than 1, it keeps prompting the user. After the user enters a valid integer, the program prints all the prime factors of the integer (including the repeated factors). For example, if the entered integer is 24, the program prints: 2 2 2 3 Run your program with the test cases where the entered...
a). Write a program that asks the user to enter an integer N and prints two...
a). Write a program that asks the user to enter an integer N and prints two integers, root and power, such that 1 < power < 6 and N = root ** power. If no such pair of integers exists, it should print a message to that effect. There are two loops, one for power and one for root. Order the loops so that if N = 64, then your program find that N = 8 ** 2 rather than...
In a file called DivisibilityTests.java, write a program that: Asks the user to enter an integer....
In a file called DivisibilityTests.java, write a program that: Asks the user to enter an integer. It is OK for your program to crash when the user does not enter a valid integer. If the integer is less than 0, the program prints: "The number is negative." If the integer is divisible by 2 and by 3, the program prints: "The number is even and divisible by 3." If the integer is divisible by 2 but not by 3, the...
Write a C program that asks the user to enter 15 integer numbers and then store them in the array.
Write a C program that asks the user to enter 15 integer numbers and then store them in the array. Then, the program will find the second largest element in array and its index without sorting the array. For example, In this array {-55,-2,1, 2, -3, 0, 5, 9, 13, 1, 4, 3, 2, 1, 0}, the second largest element is 9 [found at index 7].
Write a C++ program that asks the user to enter the monthly costs for the following...
Write a C++ program that asks the user to enter the monthly costs for the following expenses incurred from operating your automobile: loan payment, insurance, gas, oil, tires, and maintenance. The program should then display the total monthly cost of these expenses, and a projected total annual cost of these expenses. Label each cost. The labels should be left aligned and have a column width of 30 characters. The cost should be aligned right and displayed with two decimal places...
Using Python Write a program that does the following in order: 1.     Asks the user to enter...
Using Python Write a program that does the following in order: 1.     Asks the user to enter a name 2.     Asks the user to enter a number “gross income” 3.     Asks the user to enter a number “state tax rate” 4.     Calculates the “Federal Tax”, “FICA tax” and “State tax” 5.     Calculates the “estimated tax” and round the value to 2 decimal places 6.     Prints values for “name”, “gross income” and “estimated tax” The program should contain three additional variables to store the Federal tax, FICA...
Program specifics: Write a C++ program that does the following: a. Asks the user for the...
Program specifics: Write a C++ program that does the following: a. Asks the user for the distance to the pin and the depth of the green (both in yards). (Note: The pin is the hole in the green and the depth is the diameter of the green, assuming it is circular.) b. Asks the user for an integer club number from 2 to 10, where 10 is the pitching wedge (this club lifts the ball out of rough, sand, etc)....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT