int Return the number of characters in string s.

Similar documents
Unit III Functions. C functions can be classified into two categories, namely, library functions and user defined functions.

Unit IV & V Previous Papers 1 mark Answers

// CSE/CE/IT 124 JULY 2015, 2.a. algorithm to inverse the digits of a given integer. Step 1: Start Step 2: Read: NUMBER Step 3: r=0,rev=0

Computer Programming Unit 3

Principles of C and Memory Management

dynamic memory allocation

C Pointers. Abdelghani Bellaachia, CSCI 1121 Page: 1

Memory Allocation in C

Arrays and Pointers (part 1)

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

2.2 Pointers. Department of CSE

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Arrays and Pointers (part 1)

Dynamic memory allocation

A. Year / Module Semester Subject Topic 2016 / V 2 PCD Pointers, Preprocessors, DS

int marks[10]; // fixed size and fixed address No change in Memory address.

Pointers, Arrays, and Strings. CS449 Spring 2016

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Q 1. Attempt any TEN of the following:

'C' Programming Language

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

Scheme of valuations-test 3 PART 1

Computer Programming: Skills & Concepts (CP) Strings

CSCI 171 Chapter Outlines

UNDERSTANDING THE COMPUTER S MEMORY

Advanced C Programming and Introduction to Data Structures

MODULE V: POINTERS & PREPROCESSORS

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

Dynamic Memory Allocation

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

C programming basics T3-1 -

Write a C program using arrays and structure

SRI VIDYA COLLEGE OF ENGINEERING & TECHNOLOGY, VIRUDHUNAGAR Department of CSE & IT Internal Test I

Programming. Pointers, Multi-dimensional Arrays and Memory Management

UNIT-I Input/ Output functions and other library functions

Bangalore South Campus

Memory Management. CSC215 Lecture

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

SAE1A Programming in C. Unit : I - V

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

C mini reference. 5 Binary numbers 12

Lectures 5-6: Introduction to C

Procedural programming with C

I BCA[ ] SEMESTER I CORE: C PROGRAMMING - 106A Multiple Choice Questions.

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Array Initialization

C Tutorial. Pointers, Dynamic Memory allocation, Valgrind, Makefile - Abhishek Yeluri and Yashwant Reddy Virupaksha

MODULE 5: Pointers, Preprocessor Directives and Data Structures

Memory, Arrays & Pointers

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

PERIYAR CENTENARY POLYTECHNIC COLLEGE Periyar Nagar- Vallam Thanjavur

Lectures 5-6: Introduction to C

Arrays, Pointers and Memory Management

PESIT Bangalore South Campus Hosur road, 1km before ElectronicCity, Bengaluru -100 Department of Basic Science and Humanities

Jagannath Institute of Management Sciences Lajpat Nagar. BCA II Sem. C Programming

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Unit 6 Files. putchar(ch); ch = getc (fp); //Reads single character from file and advances position to next character

Pointers and File Handling

IV Unit Second Part STRUCTURES

C: Pointers. C: Pointers. Department of Computer Science College of Engineering Boise State University. September 11, /21

F.Y. Diploma : Sem. II [CO/CD/CM/CW/IF] Programming in C

MODULE 1. Introduction to Data Structures

211: Computer Architecture Summer 2016

Sri vidya college of engineering and technology UNIT II FUNCTIONS, POINTERS, STRUCTURES AND UNIONS

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

Subject: Fundamental of Computer Programming 2068

Example: Structure, Union. Syntax. of Structure: struct book { char title[100]; char author[50] ]; float price; }; void main( )

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Character Array. C Programming. String. A sequence of characters The last character should be \0 that indicates the end of string 5 \0

CS61C Machine Structures. Lecture 5 C Structs & Memory Mangement. 1/27/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Darshan Institute of Engineering & Technology for Diploma Studies Unit 5

Pointers. Introduction

Language comparison. C has pointers. Java has references. C++ has pointers and references

High Performance Programming Programming in C part 1

Memory Allocation. General Questions

Module 6: Array in C

PDS Class Test 2. Room Sections No of students

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

E.G.S. PILLAY ENGINEERING COLLEGE (An Autonomous Institution, Affiliated to Anna University, Chennai) Nagore Post, Nagapattinam , Tamilnadu.

Preprocessing directives are lines in your program that start with `#'. The `#' is followed by an identifier that is the directive name.

ECE551 Midterm Version 1

CS1100 Introduction to Programming

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Darshan Institute of Engineering & Technology for Diploma Studies Unit 6

Dynamic Allocation in C

C Structures & Dynamic Memory Management

Arrays, Strings, and Pointers

***************OUTPUT*************************** Enter the two number The addition of two number is =100

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

CSI 402 Lecture 2 Working with Files (Text and Binary)

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

SYSC 2006 C Winter 2012

Pointers, Dynamic Data, and Reference Types

Chapter 14. Dynamic Data Structures. Instructor: Öğr. Gör. Okan Vardarlı. Copyright 2004 Pearson Addison-Wesley. All rights reserved.

A3-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH 'C' LANGUAGE

Introduction to C Language (M3-R )

C Tutorial Pointers, Dynamic Memory allocation, Makefile

Transcription:

1a.String handling functions: Function strcmp(const char *s1, const char *s2) strcpy(char *s1, const char *s2) strlen(const char *) strcat(char *s1, Data type returned int Task Compare two strings lexicographically. Return a negative value if s1<s2; 0 if s1 and s2 are identical; and a positive value if s1>s2. char* Copy string s2 to string s1. int Return the number of characters in string s. char* Concatenation s2 is appended at the end of s1. const char *s2) strlwr(char *s) char* Converts the string s to lowercase. strupr(char *s) char* Converts the string s to uppercase. strrev(char *s) char* Reverses the string s. *strncpy(char *s1, const char *s2, size_t n) strcmpi(const char *s1, const char *s2) char* int The strncpy() function is similar to strcpy(), except that not more than n bytes of s2 are copied. Thus, if there is no null byte among the first n bytes of s2, the result will not be null-terminated. Does a case insensitive comparison between two strings. //Program to demonstrate 1 st four functions char s1[20],s2[20],s3[50]; int len; printf("enter two strings\n"); scanf("%[^\n] %[^\n]",s1,s2); len=strlen(s1); printf("length of %s=%d\n",s1,len); strcpy(s3,s1); printf("s3=%s\n",s3); if(strcmp(s1,s2)==0) printf("s1 and s2 are equal\n"); else if(strcmp(s1,s2)>0) printf("s1 is greater than s2\n"); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 1

else printf("s2 is greater than s1\n"); strcat(s3,s2); printf("concatenated string=%s\n",s3); 1b. char str[20]; int l,j,flag=1,i; printf("enter any string\n"); scanf("%s",str); for(l=0;str[l]!='\0';l++) ; printf("given string in reverse order\n"); for(i=l-1;i>=0;i--) putchar(str[i]); for(l=l-1,j=0;j<l;l--,j++) if(str[l]!=str[j]) flag=0; break; if(flag==1) printf("\n%s is palindrome\n",str); else printf("\n%s not palindrome\n",str); 2. Storage classes: The storage class determines the part of memory where storage allocated for a variable and how long the storage allocation continues to exist. Storage classes are of 4 types: 1. Automatic variables 2. Static variables 3. Register variables 4. External variables Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 2

Storage specifier auto static register extern Description Storage place: CPU Memory Initial value: garbage Scope: local Lifetime: within the function only Storage place: CPU Memory Initial value: zero Scope: local Lifetime: Retains the value of the variable between different function calls. Storage place: CPU Registers Initial value: garbage Scope: local Lifetime: within the function only Storage place: CPU Memory Initial value: zero Scope: global Lifetime: Till end of the main program. Variable definition might be anywhere in the C program. Automatic variables: int i; void autov(); for(i=1;i<=3;i++) autov(); void autov() auto int x=0; x=x+1; printf("x=%d\n",x); Output: x=1 x=1 Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 3

x=1 When i=1, autov() function is called for 1 st time: Because of the initial value of the automatic variables is garbage, explicitly assigning 0 to x. When x=x+1, x is incremented by 1. Then x=1 is printed. When i=2, autov() function is called for 2 nd time: Because Automatic variable s life time is local and initial value of the automatic variables is garbage, again explicitly assigning 0 to x. When x=x+1, x is incremented by 1. Then x=1 is printed. Static variables: int i; void staticv(); for(i=1;i<=3;i++) staticv(); void staticv() static int x; x=x+1; printf("x=%d\n",x); Output: x=1 x=2 x=3 When i=1, staticv() function is called for 1 st time: Because of the initial value of the static variables is zero (0), there is no need to explicit assignment of x. When x=x+1, x is incremented by 1. Then x=1 is printed. When i=2, staticv() function is called for 2 nd time: Because of static variable s life time is throughout the program, x value is retained as 1. When x=x+1, then x=1+1=2. Then x=2 is printed. Register variables: Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 4

register int i; void regv(); for(i=1;i<=3;i++) regv(); void regv() register int x=0; x=x+1; printf("\nx=%d",x); Output: x=1 x=1 x=1 When i=1, regv() function is called for 1 st time: Because of the initial value of the register variables is garbage, explicitly assigning 0 to x. When x=x+1, x is incremented by 1. Then x=1 is printed. When i=2, regv() function is called for 2 nd time: Because Register variable s life time is local and initial value of the automatic variables is garbage, again explicitly assigning 0 to x. When x=x+1, x is incremented by 1. Then x=1 is printed. Extern variables: extern int y; // external declaration void globe1(); void globe2(); globe1(); y=y+10; printf("in main y=%d\n",y); globe2(); void globe1() y=y+1; printf("in globe1 y=%d\n",y); int y; //definition Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 5

void globe2() y=y*5; printf("in globe2 y=%d\n",y); Output: In globe1 y=1 In main y=11 In globe2 y=55 Here, y is a global variable and by default initial value of global variable is zero (0). But global variable y is declared after the and globe1() functions. So, y is unavailable to those two functions even it is declared as global variable. That variable y is made available to those two functions by declaring that variable as external variable. extern int y; //external declaration (no memory is allocated to this variable). This declaration informs the compiler, that y is declared as external variable and that variable is declared as a global variable somewhere in the program. int y; //definition When this statement is encountered in the program, compiler allocates memory to that variable. 3a. Function calling mechanisms: 1. Call by value 2. Call by reference Call by value: When the function is called by value, then the actual parameters are just copied to the formal parameters. So, any operations performed on formal parameters do not reflect in the actual parameters. Here is an example to swap two numbers using call by value. In the function definition variables, the formal parameters are changed. Nut those changes are not reflected to calling function. After completion of the swap function, the actual values remain same. int a,b; Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 6

void swap(int,int); printf("enter a,b valuea\n"); scanf("%d%d",&a,&b); printf("before swaping a=%d\tb=%d\n",a,b); swap(a,b); printf("after swaping a=%d\tb=%d\n",a,b); void swap(int x,int y) int t=x; x=y; y=t; Output: Enter a,b values 4 5 Before swaping a=4 b=5 After swaping a=4 b=5 Call by reference: When we call the function using call by reference mechanism, the addresses of actual parameters are passed as arguments to the function call. In call by reference, the operation performed on formal parameters, affects the value of actual parameters because all the operations performed on the value stored in the address of actual parameters. Here is an example to swap two numbers using call by reference by passing addresses of variables a and b. The values of the variables have been changed after calling the swap() function because the swap happened on the addresses of the variables a and b. int a,b; void swap(int*,int*); printf("enter a,b values\n"); scanf("%d%d",&a,&b); printf("before swaping a=%d\tb=%d\n",a,b); swap(&a,&b); printf("after swaping a=%d\tb=%d\n",a,b); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 7

void swap(int *x,int *y) int t=*x; *x=*y; *y=t; Output: Enter a,b values 4 5 Before swaping a=4 b=5 After swaping a=5 b=4 3b. Recursion: When the function called by itself, then that function is called recursion. Recursion function must contain termination condition. int a,b; int gcd(int,int); printf("enter 2 numbers:\n"); scanf("%d%d",&a,&b); printf("\ngreatest Common Divisor is %d\n",gcd(a,b)); int gcd(int a,int b) if(b==0) return a; else return gcd(b,a%b); 4a. Dynamic memory allocation: The process of allocating memory at run time is known as dynamic memory allocation. Dynamic memory allocation functions: 1. malloc() Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 8

malloc(): 2. callc() 3. realloc() 4. free() Allocates requested size of bytes and returns a pointer to the first byte of the allocated space. The malloc function reserves a block of memory of specified size and returns a pointer type of void. This means that we can assign it to any type of pointer. Initial (default) values in the allocated memory are garbage. General form: ptr=(cast type *)malloc(byte size); Example: Int *x; x=(int *)malloc(10*sizeof(int)); The function reserves a block of memory whose size (in bytes) is equivalent to 10 integer quantities. The function returns a pointer to an integer. This pointer indicates the beginning of the memory block. calloc(): Calloc allocates multiple blocks of memory each of same size and sets all bytes to zero. (Initial values in the allocated memory are zero.) - where ptr is a pointer of type cast type. - The malloc returns pointer (of cast type) to an area of memory with size byte size. General form: ptr=(cast type *)calloc(n,byte size); realloc(): Example: Int *x; x=(int *)malloc(10,sizeof(int)); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 9

If the previously allocated memory is insufficient or more than required, you can change the previously allocated memory size using realloc(). General form: ptr=(cast type *)relloc(ptr, n*byte size); Here, ptr is reallocated with size of byte size (new size). free(): Already allocated memory is deallocated for further use of the memory location by another variables. General form: free(pointer); Example: free(ptr); //Example Program #include <stdio.h> #include <stdlib.h> int *ptr, i, n1, n2,*ptr1; printf("enter size of array: "); scanf("%d", &n1); ptr = (int*) calloc(n, sizeof(int)); ptr1 = (int*) malloc(n1 * sizeof(int)); printf("enter array elements\n"); for(i=0;i<n1;i++) scanf("%d",(ptr+i)); printf("array elements are\n"); for(i=0;i<n1;i++) printf("%d\t",*(ptr+i)); printf("\nenter new size of array: "); scanf("%d", &n2); ptr = (int*)realloc(ptr, n2*sizeof(int)); if(n2>n1) printf("enter %d array elements\n",n2-n1); Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 10

for(i=n1;i<n2;i++) scanf("%d",(ptr+i)); for(i = 0; i < n2; ++i) printf(" %d\t",*(ptr + i)); free(ptr); free(ptr1); 4b. int *a,n,i,pos=-1; void bsort(int *,int); printf("enter n value\n"); scanf("%d",&n); a=(int *)malloc(n*sizeof(int)); printf("\nenter array elements\n"); for(i=0;i<n;i++) scanf("%d",a+i); bsort(a,n); printf("array elements after sorting\n"); for(i=0;i<n;i++) printf("%d\t",*(a+i)); void bsort(int *a,int n) int i,j,t; for(i=0;i<n;i++) for(j=0;j<n-i-1;j++) if(*(a+j) > *(a+j+1)) t=*(a+j); *(a+j)=*(a+j+1); *(a+j+1)=t; Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 11

5a. 1 2 3 Structure Structure is a user defined data type and Heterogeneous. Structure has members with different data types. Syntax: structure <tagname> datatype1 member1; datatype2 member2;... datatypen membern; ; Structure members are accessed through structure variable and dot operator. In structure each member has separate space in memory. Take below example. struct student int rollno; char gender; float marks; s1; The total memory required to store a structure variable is equal to the sum of size of all the members. In above case 9 bytes (4+1+4) will be required to store structure variable s1. We can access any member in any sequence. s1.rollno = 20; Union Union is a user defined data type and Heterogeneous. Union has members with different data types. Syntax: union <tagname> datatype1 member1; datatype2 member2;... datatypen membern; ; Union members are accessed through Union variable and dot operator. In union, all members share the same memory space. This is the biggest difference between structure and union. union student int rollno; char gender; float marks; s1; In above example variable marks is of float type and have largest size (4 bytes). So the total memory required to store union variable s1 is 4 bytes. We can access only that variable whose value is recently stored. s1.rollno = 20; Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 12

4 s1.marks = 90.0; printf( %d,s1.rollno); The above code will work fine but will show erroneous output in the case of union. All the members can be initialized while declaring the variable of structure. s1.marks = 90.0; printf( %d,s1.rollno); The above code will show erroneous output. The value of rollno is lost as most recently we have stored value in marks. This is because all the members share same memory space. Only first member can be initialized while declaring the variable of union. In above example we can initialize only variable rollno at the time of declaration of variable. 5b. Pointer: A pointer is a variable that contains an address which is a location of another variable in memory. A pointer is a variable; its value is also stored in the memory in another location. Declaring and initializing pointers: datatype *ptrname; This tells the compiler 3 things about the variable ptrname. i. The asterisk (*)tells that the vaiable ptr_name is a pointer variable. ii. Ptrname needs a memory location. iii. Ptrname points to a variable of type data type. Example: int *p; declares the variable p as a pointer variable that points to an integer data type. int a,*p; p=&a; a=10; Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 13

6a. We assign the address of 'a' to a variable. The address of a=5000 and p=5048. Since the value of the variable p is the address of the variable a, we may access the value of 'a' by using the value of p. Therefore, the variable p points to the variable a. Program to illustrate pointer arithmetic (expressions): int x,y,*p1,*p2,s1,s2,s3; p1=&x; p2=&y; printf("enter x,y values\n"); scanf("%d%d",&x,&y); s1 = *p1 * *p2; s2 = x + *p2; s3 = *p2 / *p1-5; printf("s1=%d\ns2=%d\ns3=%d\n",s1,s2,s3); Output: Enter x,y values 4 6 s1=24 s2=10 s3=-4 I am giving only table, remaining from material. Function description Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 14

fopen() fclose() fgetc() fputc() fscanf() fprintf() getw() putw() fseek() ftell() rewind() create a new file or open a existing file closes a file reads a character from a file writes a character to a file reads a set of data from a file writes a set of data to a file reads a integer from a file writes a integer to a file set the position to desire point gives current position in the file set the position to the beginning of a file Prepared by Aravinda Kasukurthi, CSE, RVRJCCE 15