In: Computer Science
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.