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...
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.
Write a C++ program using produces Huffman code for a string of text entered by the...
Write a C++ program using produces Huffman code for a string of text entered by the user. Must accept all ASCII characters.
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public...
Can you fix the errors in this code? import java.util.Scanner; public class Errors6 {    public static void main(String[] args) {        System.out.println("This program will ask the user for three sets of two numbers and will calculate the average of each set.");        Scanner input = new Scanner(System.in);        int n1, n2;        System.out.print("Please enter the first number: ");        n1 = input.nextInt();        System.out.print("Please enter the second number: ");        n2 =...
2. Write a Java program to read a string (a password)from the user and then   check...
2. Write a Java program to read a string (a password)from the user and then   check that the password conforms to the corporate password policy.   The policy is:   1) the password must be at least 8 characters   2) the password must contain at least two upper case letters   3) the password must contain at least one digit   4) the password cannot begin with a digit   Use a for loop to step through the string.   Output “Password OK” if the password...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT