Question

I need writing a code that does the following: //This program will prompt the user to...

I need writing a code that does the following:

//This program will prompt the user to enter a sentence, then get an entire line all at once
//print out the 1st word of the sentence by calling the first () finction
//print out the last word of the sentence by calling the last () function
//Free the dynamically allocated memory created b first ()

In C please

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>
#include<string.h>
//definition of first function
void first(char str[])
{
    char *arr1;
    int i = 0,j=1;
    //store the first word in arr1[]
    while(str[i] != ' ')
    {
    //dynamically allocating the memory using realloc
       arr1 = (char*)realloc(arr1, j * sizeof(char));
       arr1[i] = str[i];
       i++;
       j++;
    }
    arr1[i] = '\0';
    //Displays the first word
    printf("First word: %s\n",arr1);
    //free the dynamically allocated memory
    free(arr1);
}
//definition of last function
void last(char str[])
{
    char arr[20];
    int j=0,len= 0;
    //calculating the length of the string
    len = strlen(str);
    len--;
   
    //logic to find the index of the last word
    while(str[len] != ' ')
    len--;
    //copy the last word into the another array
    while(str[len] != '\0')
    {
       arr[j] = str[len];
       len++;
       j++;
    }
    arr[j] = '\0';
    //Displays the last word
    printf("Last word: %s",arr);

}
//main function starts here
void main()
{
    char *str, c;
    int i = 0, j = 1;
    str = (char*)malloc(sizeof(char));
    printf("Enter String : ");
    while (c != '\n')
    {
        //read the input from user
        c = getc(stdin);
        //realloc is used to reallocate the memory dynamically
        str = (char*)realloc(str, j * sizeof(char));
        //store the each character in the array
        str[i] = c;
        i++;
        j++;
    }
    str[i] = '\0';
    // at the end append null character to mark end of string
    printf("\nEntered string is : %s\n", str);
   
    //calling the function first()
    first(str);
    //calling the function last()
    last(str);
   
    //free the dynamically allocated the memory
    free(str);
}

Add a comment
Know the answer?
Add Answer to:
I need writing a code that does the following: //This program will prompt the user to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • C++ Code Writing Prompt: Countdown: Write a countdown program that prompts the user for a max...

    C++ Code Writing Prompt: Countdown: Write a countdown program that prompts the user for a max value (i.e. 30). Print out a countdown from that number down to zero, then print “blast off!”

  • Python Error: Writing a program to complete the following: Prompt the user for the name of...

    Python Error: Writing a program to complete the following: Prompt the user for the name of an input file. Open that file for input. (reading) Read the file using a "for line in a file" loop For each line,  o print the line and compute and print the average word length for that line. Close the input file Sample output: MY CODE IS WRONG AND NEED HELP CORRECTING IT!!! ------------------------------------------------------------------------------------------------------------------------ DESIRED OUTPUT: Enter input file name: Seasons.txt Thirty days hath September...

  • I need this code in C programming. Can anyone pls help me in this? Project 8,...

    I need this code in C programming. Can anyone pls help me in this? Project 8, Program Design A hotel owner would like to maintain a list of laundry service requests by the guests. Each request was stored with the room number, the guest's first name, last name, and number of items. The program laundry_list.ccontains the struct request declaration, function prototypes, and the main function. Complete the function definitions so it uses a dynamically allocated linked list to store the...

  • Please code following question in Java. Q) Prompt the user to enter the first initial of...

    Please code following question in Java. Q) Prompt the user to enter the first initial of their first name. Then prompt the user to enter their full last name. Then prompt the user to enter the last 4 digits of their student ID.

  • this us python plz follow the instructions while u solving I need the code and sane...

    this us python plz follow the instructions while u solving I need the code and sane output QUESTION 1 Write a program that performs the following: 1) request a sentence (at least two words with no punctuation marks) input by the user 2) display the first word 3) display the last letter of the first word 4) display the last word 5) display the first letter of the last word input text can be any content just make sure to...

  • Complete the Python program below that performs the following operations. First prompt the user to input...

    Complete the Python program below that performs the following operations. First prompt the user to input two integers, n and m. If n<m, then print the odd positive integers that are less than m (in order, on a single line, separated by spaces). If man, then print the even positive integers that are less than n (in order, on a single line, separated by spaces). If neem, then print nothing. For instance, if the user enters 5 followed by 10,...

  • Design a modular program using pseudo-code which prompts a user for their first name, followed by...

    Design a modular program using pseudo-code which prompts a user for their first name, followed by their last name; then displays a "Hello" salutation which concatenates their first name with their last name. Sample output (user input shown in red): Please enter your first name: John Please enter your last name: Smith Hello, John Smith! Your program must include a main module and one function; this function prompts the user for either their first name or last name, using a...

  • Write a "PYTHON" program to prompt the user to enter a fist name, last name, student...

    Write a "PYTHON" program to prompt the user to enter a fist name, last name, student ID and GPA. Create a dictionary with the data. Print out the data. Then remove the GPA and print again.

  • I am writing a code in c for windows. It should be very very basic. The...

    I am writing a code in c for windows. It should be very very basic. The instructions say to use a switch case statement for processing the user input to the menu. Also I prefer the main to be at the top. Should be very basic and simple. a) Print your name. b)  Prompt the user to enter a positive number between 1-50, read the entered input, and display all numbers from 0 up to the number entered (the display should...

  • Write a program that does the following in Python Code: Stores the following three lists: last_name...

    Write a program that does the following in Python Code: Stores the following three lists: last_name = ["Smith", "Jones", "Williams", "Bailey", "Rogers", "Pyle", "Rossington"] first_name =["Lisa", "Bill", "Jay", "Sally", "Walter","Jake","Gary"] phone_number =["240-233-1921", "301-394-2745", "571-321-8934", "703-238-3432", "703-947-9987", "301-945-3593", "240-671-3221"] Has a function named combine_lists() that takes in last_names, first_names and phone_numbers as lists and returns a dictionary called phone_book that uses last_name as the key and a list consisting of first_name and phone_number. Has a main function that iterates through the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT