Question

In: Computer Science

q7.1 Fix the errors in the code (in C) //This program should read a string from...

q7.1 Fix the errors in the code (in C)

//This program should read a string from the user and print it using a character pointer

//The program is setup to use pointer offset notation to get each character of the string

#include <stdio.h>

#include <string.h>

int main(void){

char s[1];

scanf(" %c", s);

char *cPtr = s[1];

int i=0;

while(1){

printf("%c", cPtr+i);

i++;

}

printf("\n");

}

Solutions

Expert Solution

CODE IN C:-(With documentation and required correction)

#include <stdio.h>

#include <string.h>

int main(void){

char str[100]; //STRING CAN BE OF LENGTH MORE THAN 1 so we need to take length more than 1

scanf(" %s", str);

char *cPtr = str; //to store the address of first character in string just write character array name


while(*cPtr) //Here we cannot take 1 inside while() because if we take 1 then it will always be true and run for infinite times
{

printf("%c", *cPtr++); //to access the value of address we need to use * before it and also instead of using *cPtr+i each time //we can use *cPtr++

}

printf("\n");

}

SCREENSHOTS:-


Related Solutions

q7.4 Fix the errors in the code (in C) //This program is supposed to scan 5...
q7.4 Fix the errors in the code (in C) //This program is supposed to scan 5 ints from the user //Using those 5 ints, it should construct a linked list of 5 elements //Then it prints the elements of the list using the PrintList function #include <stdio.h> struct Node{ int data; Node* next; }; int main(void){ struct Node first = {0, 0}; struct Node* second = {0, 0}; Node third = {0, 0}; struct Node fourth = {0, 0}; struct...
C++ Program for Project Alarm. ------------------------------------------------------------------------------ Fix ALL Errors in my EXISTING code for ALL 5...
C++ Program for Project Alarm. ------------------------------------------------------------------------------ Fix ALL Errors in my EXISTING code for ALL 5 files.   MUST add comments for FULL CREDIT. Take a screenshot of the working solution output. ------------------------------------------------------------------------------- Instructions: Use class composition to define and implement a new class called Alarm that contains a Time member variable. A. Define and implement the class Alarm as follows: The class Alarm consists of two private member variables: description of type string and atime of type Time. The class...
In the assignment below please fix the errors in each code. The first code should have...
In the assignment below please fix the errors in each code. The first code should have a total of 3 erros. The second and third code should have 4 errors. Thanks //DEBUG05-01 // This program is supposed to display every fifth year // starting with 2017; that is, 2017, 2022, 2027, 2032, // and so on, for 30 years. // There are 3 errors you should find. start    Declarations       num year       num START_YEAR = 2017       num...
C++ : Find the syntax errors in the following program. For each syntax error, fix it...
C++ : Find the syntax errors in the following program. For each syntax error, fix it and add a comment at the end of the line explaining what the error was. #include <iostream> #include <cmath> using namespace std; int main(){ Double a, b, c; 2=b; cout<<"Enter length of hypotenuse"<<endl; cin>>c>>endl; cout>>"Enter length of a side"<<endl; cin>>a; double intermediate = pow(c, 2)-pow(a, 2); b = sqrt(intermediate); cout<<"Length of other side is:" b<<endline; return 0; }
Write a C program that will read a character string and then encrypt the string based...
Write a C program that will read a character string and then encrypt the string based on one of the 3 different encryption methods. The type of encryption is to be selected by the user. Encryption method 1: Swapping by position. Characters in the array are swapped with the opposite characters based on their position in the string. Example: Input string – apple. Encrypted string – elppa Method: The first character ‘a’ and the last character ‘e’ – swap their...
Please fix all the errors in this Python program. import math def solve(a, b, c): """...
Please fix all the errors in this Python program. import math def solve(a, b, c): """ Calculate solution to quadratic equation and return @param coefficients a,b,class @return either 2 roots, 1 root, or None """ #@TODO - Fix this code to handle special cases d = b ** 2 - 4 * a * c disc = math.sqrt(d) root1 = (-b + disc) / (2 * a) root2 = (-b - disc) / (2 * a) return root1, root2 if...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner;...
Can you fix the errors in this code? package demo; /** * * */ import java.util.Scanner; public class Booolean0p {        public class BooleanOp {            public static void main(String[] args) {                int a = 0, b = 0 , c = 0;                Scanner kbd = new Scanner(System.in);                System.out.print("Input the first number: ");                a = kbd.nextInt();                System.out.print("Input...
Please fix all of the errors in this Python Code. import math """ A collection of...
Please fix all of the errors in this Python Code. import math """ A collection of methods for dealing with triangles specified by the length of three sides (a, b, c) If the sides cannot form a triangle,then return None for the value """ ## @TODO - add the errlog method and use wolf fencing to identify the errors in this code def validate_triangle(sides): """ This method should return True if and only if the sides form a valid triangle...
Fix this broken code? # Get our input from the command line import sys string =...
Fix this broken code? # Get our input from the command line import sys string = sys.argv[1] # Your code goes here if string 'Bingo' print('Missed') else: print('Hit!')
I need the code for a C++ program that creates an array of 5000 String objects...
I need the code for a C++ program that creates an array of 5000 String objects that will store each word from a text file. The program will read in each word from a file, and store the first 5000 words in the array. The text file should be read in from the command line.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT