0% found this document useful (0 votes)
34 views

Assignment-5 (Strings, Structures & Unions) : - Strings

This document provides assignments on C strings, structures, unions, and related functions: 1) Implement functions for reading, printing, finding length, copying, concatenating, comparing, and reversing strings. Study functions like strncpy, strncat, strncmp. 2) Create structures for student information and a box, perform operations on structure variables, and pass structures to functions. Modify a structure to have an array member. 3) Create unions to convert between IP address formats and demonstrate type punning. Calculate sizes and offsets of structure and union members. 4) Address precision issues when comparing expressions of different types and fix buffered I/O issues by adding spaces or flushing buffers between

Uploaded by

gauravlko
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

Assignment-5 (Strings, Structures & Unions) : - Strings

This document provides assignments on C strings, structures, unions, and related functions: 1) Implement functions for reading, printing, finding length, copying, concatenating, comparing, and reversing strings. Study functions like strncpy, strncat, strncmp. 2) Create structures for student information and a box, perform operations on structure variables, and pass structures to functions. Modify a structure to have an array member. 3) Create unions to convert between IP address formats and demonstrate type punning. Calculate sizes and offsets of structure and union members. 4) Address precision issues when comparing expressions of different types and fix buffered I/O issues by adding spaces or flushing buffers between

Uploaded by

gauravlko
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

C&DS

DESD Aug 2012

Assignment-5 (strings,structures & unions): Strings


Implement your own functions for reading & printing strings Define your own functions for a) finding length b) copying c) concatenation d) comparision e) reversing in memory Study the following functions strncpy, strncat, strncmp, strcasecmp, strncasecmp, strchr, strrchr,strstr, strtok Explore the following functions used for raw memory operations. memcpy, memcmp, memset, bzero Implement your own function to convert a string having only digits into an integer and vice versa Write a c program to find sum & avg of command line arguments In a table of strings swap any two rows, when table is declared as a) char tstr[5][20]; b) char *tstr[5]; Conver the string in a.b.c.d format into 32 bit unsigned integer (use pointer operations for packing purpose) Write recursive functions for a)finding string length b)displaying string in reverse order c)to count no.of occurences of a given character d)finding sub string in a main string

CDAC ACTS, Pune

C&DS

DESD Aug 2012

Structures & Unions


Create a structure for student information with the members rollno, names, marks, and perform following operations - Creating variables, input, output operations - initialization of variables - create a pointer of struct type, and assign address of variable - access members using arrow operator - calculate size of variable, offset of each member - create alias for the structure type, pointer type using typedef For the following structure calculate overall size, offset of each member. How to balance between speed, memory while using such structures. struct A { int x; char c1; double d; float f; char c2; } Create a Box structure with the members length,breadth,height.Pass the structure variable to a function to calculate volume by value, by reference In the student structure creatd above modify marks member as an array(array of 5 subjects), create array of struct variables and do some input,output operations.(Marks of ith student in jth subject etc) Whats wrong in the following code, suggest a fix for this. strcut A { int x; char *str; }; struct A a1 = { 101, abc } CDAC ACTS, Pune 2

C&DS struct A s2 = a1;

DESD Aug 2012

Can a function return structure variable?Any better alternatives to this if it is possible or not. Create an anonymous structure, create some variables from this (with & without typedef) Create a nested structure, access members of inner structure from outer one. Bit fields

Unions: Try the following code union A { int x; int y; char ch; }; union A a1; a1.x=0x10; a1.y=0x1121; print a1.x, a1.ch Calculate size of union , offset of members Convert ip address between dotted decimal format, 32 bit format using unions Anonymous unions, usage of typedef Nesting of structures, unions Union inside a structure structure inside an union etc. Miscellaneous:Precision problems Compare some int, float, double expressions, if getting precision problems rectify using the condition fabs(exp1 exp2) < 1e-5 eg:- int x = 2; float y = sqrt(4); float z=sqrt(0.1225); z==0.35 etc. Check correctness of comparisions like x==y , Buffered I/O operations int a,b,c; CDAC ACTS, Pune 3

C&DS printf(enter two no.s\n); scanf(%d%d,&,&b); //but here give input of 3 no.s separated by space. printf(ente ranother number\n); scanf(%d,&c); printf(%d,%d,%d\n,a,b,c);

DESD Aug 2012

What do you observer, if any prob fix it using %d while reading variable c (or) use __fpurge before reading c char c1,c2; printf(enter any character\n); scanf(%c,&c1); //you may use getchar(c1); here scanf(%c,&c2); printf(c1=%d,c2=%d\n,c1,c2); fix any prob using a space before %c or using __fpurge int x,y; for(int i=1;i<=5;i++) { printf(i=%d,i); //dont use \n at end of printf y=1/(x-5); } What do you observer when you run above code. Try the same using \n at the end of printf or by using fflush after printf

CDAC ACTS, Pune

You might also like