Question

In: Computer Science

Modify the program so that after it reads the line typed on the keyboard, it replaces...

Modify the program so that after it reads the line typed on the keyboard, it replaces the ‘\n’ character with a NUL character. Now you have stored the input as a C-style string, and you can echo it with: Explain what you did.

#include <unistd.h>
#include <string.h>

int main(void)
{
  char aString[200];
  char *stringPtr = aString;

  write(STDOUT_FILENO, "Enter a text string: ",
        strlen("Enter a text string: "));  // prompt user

  read(STDIN_FILENO, stringPtr, 1);    // get first character
  while (*stringPtr != '\n')           // look for end of line
  {
    stringPtr++;                       // move to next location
    read(STDIN_FILENO, stringPtr, 1);  // get next character
  }

  // now echo for user
  write(STDOUT_FILENO, "You entered:\n",
        strlen("You entered:\n"));
  stringPtr = aString;
  do
  {
    write(STDOUT_FILENO, stringPtr, 1);
    stringPtr++;
  } while (*stringPtr != '\n');
  write(STDOUT_FILENO, stringPtr, 1);

  return 0;
}

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

#include <unistd.h>
#include <string.h>

int main(void)
{
    char aString[200];
    char* stringPtr = aString;

    write(STDOUT_FILENO, "Enter a text string: ",
        strlen("Enter a text string: ")); // prompt user

    read(STDIN_FILENO, stringPtr, 1); // get first character
    while (*stringPtr != '\n') // look for end of line
    {
        stringPtr++; // move to next location
        read(STDIN_FILENO, stringPtr, 1); // get next character
    }
    //right now stringPtr is pointing to the location on array that contain \n character, so
    //replacing '\n' character with null terminator '\0' to indicate end of string
    *stringPtr = '\0';

    write(STDOUT_FILENO, "You entered:\n",
        strlen("You entered:\n"));

    //now simply printing aString. the strlen method returns the number of characters found
    //before the end of string. in c strings, null terminator or \0 indicate end of string,
    //so the system returns the count of characters upto \0, and those characters will be printed.
    write(STDOUT_FILENO, aString, strlen(aString));

    //uncomment below line if you want to print a newline after the string.
    //write(STDOUT_FILENO, '\n', 1);
    return 0;
}

/*OUTPUT*/

Enter a text string: hello world
You entered:
hello world

Related Solutions

Create a C Program that reads a file and replaces every other letter with an asterisk....
Create a C Program that reads a file and replaces every other letter with an asterisk. The first integer 'num' is the number of lines. Include an "Error" Message and exit the program if:    The wrong name for the input/output file is given    The input/output file can not be opened input.txt 7 Never Have We Ever Played A Game output.txt 7 N*v*r *a*e W* *v*r *l*y*d * G*m*
Write a program that reads a file line by line, and reads each line’s tokens to...
Write a program that reads a file line by line, and reads each line’s tokens to create a Student object that gets inserted into an ArrayList that holds Student objects.  Each line from the file to read will contain two strings for first and last name, and three floats for three test grades.  After reading the file and filling the ArrayList with Student objects, sort the ArrayList and output the contents of the ArrayList so that the students with the highest average...
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
Modify the following C++ program to count the number of correct and incorrect responses typed by...
Modify the following C++ program to count the number of correct and incorrect responses typed by the student. After the student types 5 answers, your program should calculate the percentage of correct responses. If the percentage is lower than 75 percent, your program should print “Please ask for extra help” and then terminate. If the percentage is 75 percent or higher, your program should print “Good work!” and then terminate. The program is as follows: #include<iostream> #include<iomanip> #include<cstdlib> #include<time.h> using...
(C++) Write a program that reads a list of integers from the keyboard and print out...
(C++) Write a program that reads a list of integers from the keyboard and print out the smallest number entered. For example, if user enters 0 3 -2 5 8 1, it should print out -2. The reading stops when 999 is entered.
ite a C program that prompts for and reads in a non-negative integer from the keyboard....
ite a C program that prompts for and reads in a non-negative integer from the keyboard. Read it into an int variable x. Then display the 32 bits in x from the lsb to the msb (so the display will show the value in x in binary with the bits in reverse order). For example, if you input 6, then your program displays 00000000000000000000000000000110 Use the algorithm given in class (repeatedly divide by 2). Use the / and % operators....
Using c++, write a program that reads a sequence of characters from the keyboard (one at...
Using c++, write a program that reads a sequence of characters from the keyboard (one at a time) and creates a string including the distinct characters entered and displays the string on the screen. The input terminates once the user enters a white-space character or the user has entered 50 distinct characters. Do not use C-Strings. 2. Use the following function to append character “ch” to the string “s”: s.push_back(ch); 3. Read the input characters one by one, i.e. do...
In C++, The following program reads one character from the keyboard and will display the character...
In C++, The following program reads one character from the keyboard and will display the character in uppercase if it is lowercase and does the opposite when the character is in uppercase. If the character is a digit, it displays a message with the digit. Modify the program below such that if one of the whitespaces is entered, it displays a message and tells what the character was. // This program reads one character from the keyboard and will //...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4 ME 2 finish 2! Output: Consonants: WMfnsh 1) Name your program command_consonants.c. 2) If no command line arguments was provided, your program should print a usage message “Usage: ./a.out input #include<stdio.h> int main() { printf("Input: "); int i = 0; char ch; // array to store consonants char consonant[100]={""}; //do loop to take input do { ch = getchar(); //checks to see if consonants...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year,...
Modify the Movie List 2D program -Modify the program so it contains four columns: name, year, price and rating (G,PG,R…) -Enhance the program so it provides a find by rating function that lists all of the movies that have a specified rating def list(movie_list): if len(movie_list) == 0: print("There are no movies in the list.\n") return else: i = 1 for row in movie_list: print(str(i) + ". " + row[0] + " (" + str(row[1]) + ")") i += 1...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT