Operations on strings, C/C++ Programming

Assignment Help:

1 Aims

The main purpose of the assignment is to let you practice the following programming techniques:

  • perform operations on pointers to basic and more complex types;
  • pass pointers as arguments to functions;
  • perform operations on strings;
  • parse some text.

2 General presentation

You will design and implement a program that will

  • take as command line arguments descriptions of types, possibly associated with variable names, some of those variable names possibly referring to other variable names,
  • check that those descriptions are correct according to some well defined set of rules, and
  • output appropriate C representations of those descriptions.

Four descriptions of types of increasing complexity will be considered. The ultimate objective is to be able to parse descriptions of the fourth and last, most general kind, and you are advised to have those descriptions in mind when you develop your code for dealing with the simpler descriptions. Still having four levels of complexity will help you gradually design the solution to the general version of the problem, and you should be prepared to adopt the solution to descriptions of a given level when tackling descriptions of the next level.

We refer to each type description as a phrase. So the command line arguments are expected to make up a number of phrases. The appropriate C representation of any description is assumed to require no more than 100 characters, spaces included.

When the command line arguments are of the expected form, the program should output a number of lines equal to the number of phrases, the first line corresponding to the first phrase, the second line to the second phrase, etc.

3 Descriptions of the first level

3.1 Syntax

At this level, a phrase represents a basic type, that is, a type whose default representation is one of:

  • char
  • signed char
  • unsigned char
  • short
  • int
  • long
  • long long

Such a phrase will be of the form:

  • unsigned short
  • unsigned
  • unsigned long
  • unsigned long long
  • float
  • double
  • long double

A/An basic_type_description [variable_name].

Of course, a phrase begins with A or An depending on whether the second word starts with a consonant or a vowel. The square brackets around variable_name indicate that the latter is optional; variable_name can be any valid C identifier, except for any of the words that can appear in the input, which includes int, A, array, to, etc. Note that a phrase ends in a full stop. As for basic_type_description, it follows the usual rules:

  • signed, when not used alone, is optional, and when used alone, has int as default representation;
  • int, when not used alone, is optional;
  • the order of the words is irrelevant, but words cannot be duplicated (expect for the word long in the type long long of course).

The default representation should be output.

3.2 Example

Here is an example of how your program should behave, in which the command line arguments following

a.out span the first two lines below:

$ a.out A signed. An int short x. A long unsigned long int. A double long x.

A float.

A long long

_007. int

short x

unsigned long

long long double x

float

long long _007

4 Descriptions of the second level

4.1 Syntax

At this level, a phrase represents a simple array, pointer or function type, and is of one of the following forms:

_ An array [variable_name] of N datum/data of type basic_type_description.

_ A pointer [variable_name] to void.

_ A pointer [variable_name] to a datum of type basic_type_description.

_ A function [variable_name] returning void.

_ A function [variable_name] returning a datum of type basic_type_description.

For the first form, N is a strictly positive number, and is of course followed by datum if equal to 1, and by data otherwise.

4.2 Example

Here are three examples of how your program should behave, in which the command line arguments following a.out span three, two and two lines, respectively:

$ a.out A long int long john. A pointer to a datum of type char. An array lisa2 of 10 data of type double. A pointer peter to a datum of type unsigned int. An array hanna of 1 datum of type char signed.

long long john

char *

double lisa2[10]

unsigned *peter

signed char hanna[1]

$ a.out An array of 290 data of type int. A function returning a pointer to a datum of type char. A function that_is_right returning void. A pointer may_be_2 to void.

int [290]

int [290]

char *()

void that_is_right()

void *may_be_2

$ a.out A function really returning a datum of type float. A float x. A function x returning a pointer to a datum of type unsigned int.

float really()

float x

unsigned *x()

5 Descriptions of the third level

5.1 Syntax

At this level, a phrase represents a complex array, pointer or function type, and is of one of the following forms:

_ An array [variable_name] of N array/arrays of ...

_ An array [variable_name] of N pointer/pointers to ...

_ A pointer [variable_name] to a pointer to...

_ A pointer [variable_name] to an array of ...

_ A pointer [variable_name] to a function returning ...

_ A function [variable_name] returning a pointer to ...

where ... allows for an arbitrary number of applications of legitimate type descriptions that follow the given format, and ends with the representation of a basic type. Be careful with the use of the singular versus plural in expressions involving pointers: we can have a pointer to a pointer, an array, a function or a datum, and we can have pointers to pointers, arrays, functions or data. Also note that only the leading type can be associated with a variable.

5.2 Example

Here are two examples of how your program should behave, in which the command line arguments following a.out span six and two lines, respectively:

$ a.out A pointer to a pointer to a pointer to a pointer to a datum of type int. An array gee of 10 arrays of 20 arrays of 30 pointers to arrays of 40 arrays of 50 data of type int. An array of 20 pointers to data of type int. An array that_is_an_array of 2000 pointers to arrays of 1000 data of type int. An array of 1 pointer to an array of 25 pointers to functions returning a pointer to a datum of type int.

int ****

int (*gee[10][20][30])[40][50]

int *[20]

int (*that_is_an_array[2000])[1000]

int *(*(*[1])[25])()

$ a.out A function returning a pointer to an array of 3 pointers to pointers to arrays of 60 pointers to functions returning a pointer to a datum of type int.

int *(*(**(*())[3])[60])()

6 Descriptions of the fourth level

6.1 Syntax

At this level, a phrase represents a complex array, pointer or function type with some representations possibly referring to others, by ending in:

a datum of type the type of variable_name.

Two rules have to be abided by:

_ There can be no circular references, so a variable cannot refer to itself, or to a variable that refers to the former, etc.

_ Variables referred to are unique. More precisely, a phrase can de_ne a type with no associated variable, and many phrases can de_ne types associated with a given variable as long as that variable is not referred to by any other phrase, but only one phrase can de_ne a type associated with a given variable in case that variable is referred to by other phrases.

6.2 Example

Here are two examples of how your program should behave, in which the command line arguments following a.out span three and _ve lines, respectively:

$ a.out An array U of 50 pointers to pointers to data of type the type of X. A pointer Z to a datum of type the type of U. An array Z of 70 pointers to data of type the type of U. An int Z. An unsigned V. A char. A pointer X to a datum of type double.

double ***U[50]

double ***(*Z)[50]

double ***(*Z[70])[50]

int Z

unsigned V

char

double *X

$ a.out An array of 8 data of type the type of A1. A pointer to a function returning a datum of type the type of P. An array A1 of 20 pointers to data of type the type of C. A double long R. A pointer C to a function returning a pointer to a datum of type the type of D. A pointer P to an array of 102 data of type the type of R. A function D returning a datum of type the type of P.

long double (*(*(**[8][20])())())[102]

long double (*(*)())[102]

long double (*(*(**A1[20])())())[102]

long double R

long double (*(*(*C)())())[102]

long double (*P)[102]

long double (*D())[102

7 Incorrect input

Whenever any part of the command line arguments is not of the expected form, the program should output Incorrect input and stop. Here are examples of tests with invalid command line arguments.

$ a.out A long float.

Incorrect input

$ a.out A int.

Incorrect input

$ a.out An int a.

Incorrect input

$ a.out A double

Incorrect input

$ a.out a char.

Incorrect input

$ a.out An int long int.

Incorrect input

$ a.out A signed float.

Incorrect input

$ a.out An int 2bad.

Incorrect input

$ a.out An int do-it.

Incorrect input

$ a.out An array of 20 datum of type int.

Incorrect input

$ a.out An array of +3 data of type int.

Incorrect input

$ a.out An array of data of type int.

Incorrect input

$ a.out An array of 1 int.

Incorrect input

$ a.out A pointer to an int.

Incorrect input

$ a.out A pointer to data of type int.

Incorrect input

$ a.out A function returning an array of 20 data of type int.

Incorrect input

$ a.out A pointer x to an array y of 10 data of type int.

Incorrect input

$ a.out An int x. A pointer to a datum of type the type of x. A double x.

Incorrect input

$ a.out A pointer x to a datum of type the type of y. A pointer y to a datum of type the

type of x.

Incorrect input

$ a.out An array of 10 data of type the type of x. An int.

Incorrect input

8 Format of the output for correct input

A single space is displayed after a word that is part of the description of a basic type when that word is followed by another word; no other space is output. In particular, there is no space before and after the first and last nonspace characters, respectively.

No second chance. Just mess up one space character in your output for a test and you will fail that test. No second chance given. Zero mark guaranteed. You have been warned. Wrong way, go back.

Hey, did you get that? The expected output has to be correct character for character, including spaces and absence thereof. Do the right thing. Act now! Heavy fines apply, loss of licence to use a compiler.


Related Discussions:- Operations on strings

Array, how to do an array

how to do an array

Example of switch case statement, #include #include #include void* m...

#include #include #include void* memorycopy (void *des, const void *src, size_t count) {   size_t n = (count + 7) / 8;   char* destination = (char *) des;   char* source =

Write a c program to input numbes in table format, Write a C program to inp...

Write a C program to input 16 numbers and print them out in a table format of 4 wide by 4 deep. E.g.   4    5    5    7   8    9    10    11   12    13    14    15   16    17

Train Station Schedule, Train Station C++ Program A C++ program that provi...

Train Station C++ Program A C++ program that provides a simple text based interface that will allow a user to create, edit, search, or delete a train schedule. The program will m

Arrays, how to declare arrays

how to declare arrays

C program to demonstrate call by reference, C program to demonstrate call b...

C program to demonstrate call by reference: void disp(int *,int*,int*,int*); void main() {                 int a=2,b=3, sum,mul;                 printf("Enter 1st n

Arguments passing mechanism, Arguments Passing Mechanism C++ supports th...

Arguments Passing Mechanism C++ supports the following argument passing mechanisms: i).  Pass by value: A copy of the arguments value is made and passed to the called functio

''c'' programme, Write a ''C'' program to accept any 3 digit integer number...

Write a ''C'' program to accept any 3 digit integer number from the keyboard and display the word equivalent representation of the given number.

String routines in c, Available in the string.h header file are the followi...

Available in the string.h header file are the following routines         strcat(string1,string2);   This joins string2 to string1; care must be taken to allocate enough space for

Program is to define a class as employee, Program is to define a class as e...

Program is to define a class as employee: Write a program to define a class as employee and collect information about them by using classes and object class employee   {

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd