Data Structure and C Part-6. Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Problem Solving & Program Design in C
Spring Semester 2013 Lecture 5
 A string is an array of characters.  Strings must have a 0 or null character after the last character to show where the string ends.  The null character.
C Characters & Strings Character Review Character Handling Library Initialization String Conversion Functions String Handling Library Standard Input/Output.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Character String Manipulation. Overview Character string functions sscanf() function snprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
 2000 Prentice Hall, Inc. All rights reserved Fundamentals of Strings and Characters String declarations –Declare as a character array or a variable.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Characters and Strings.
Chapter 7: User-Defined Functions II
Chapter 9 Character Strings
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Chapter 10.
Chapter 8 Characters and Strings Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Chapter 6: User-Defined Functions I
1 Modularity In “C”. 2 What is Modularity?  Modularity, is the heart of the high level, structured languages.  Means breaking down a big problem into.
To remind us We finished the last day by introducing If statements Their structure was:::::::::
Chapter 9: Arrays and Strings
CS Nov 2006 C-strings.
 2007 Pearson Education, Inc. All rights reserved C Characters and Strings.
 Introduction Introduction  Types of Function Types of Function  Library function Library function  User defined function User defined function 
1. An array is a collection of a fixed number of components wherein all of the components are of the same type Example: Suppose that there is a list of.
Introduction to C programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 8 - Characters and Strings Outline 8.1Introduction 8.2Fundamentals of Strings and Characters 8.3Character.
Project 1 Due Date: September 25 th Quiz 4 is due September 28 th Quiz 5 is due October2th 1.
Functions Why we use functions C library functions Creating our own functions.
Characters and Strings File Processing Exercise C Programming:Part 3.
USER-DEFINED FUNCTIONS. STANDARD (PREDEFINED) FUNCTIONS  In college algebra a function is defined as a rule or correspondence between values called the.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
Introduction As programmers, we don’t want to have to implement functions for every possible task we encounter. The Standard C library contains functions.
Characters, Strings, And The string Class Chapter 10.
Chapter 8: Character and String CMPD144: Programming 1.
Chapter 8 Strings. Copyright ©2004 Pearson Addison-Wesley. All rights reserved.9-2 Strings stringC implements the string data structure using arrays of.
Functions: Part 2 of /11/10: Lecture 16 CMSC 104, Section 0101 John Y. Park 1.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 10 Characters, Strings, and the string class.
String Array (Multidimensional Arrays) 1. A string array is a multidimensional array of strings. It is declared in the following syntax: char variable_name[No_of_strings][size_of_each_string];
S CCS 200: I NTRODUCTION AND G ETTING S TART IN C P ROGRAMMING Lect. Napat Amphaiphan.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
UniMAP SEM I - 10/11EKT 120 Computer Programming1 Lecture 8 – Arrays (2) and Strings.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Characters and Strings Functions.
21. THE STANDARD LIBRARY. General Rules The C standard library is divided into 15 parts (24 in C99), with each part described by a header. The names of.
CMSC 104, Version 8/061L25Strings.ppt Strings Topics String Libraries String Operations Sample Program Reading Sections
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
In C programming, one of the frequently arising problem is to handle similar types of data. For example: If the user want to store marks of 100 students.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Programming Fundamentals Enumerations and Functions.
Functions Chapter 5. Function A set of instructions that are designed to perform specific task. A complete and independent program. It is executed by.
UNIT - 3 ARRAYS AND STRINGS. Array An Array is a collection of similar data items, that are stored under a common name. Types –One-Dimensional array –Two-Dimensional.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
C Characters and Strings
Functions, Part 2 of 2 Topics Functions That Return a Value
C Programming:Part 3 Characters and Strings File Processing Exercise.
User-Defined Functions
Formatted and Unformatted Input/Output Functions
Functions, Part 2 of 3 Topics Functions That Return a Value
In C Programming Language
Chapter 9: Pointers and String
Functions Imran Rashid CTO at ManiWeber Technologies.
Characters and Strings Functions
C Characters and Strings
Functions, Part 2 of 3 Topics Functions That Return a Value
Functions, Part 2 of 3 Topics Functions That Return a Value
Presentation transcript:

Data Structure and C Part-6

Naming a Function Any valid variable-name can be given to the user-defined function. The main program itself is considered as a function in C. Whenever a C program is executed, execution starts from the function. So there should be only one main function.

Example of Code Program # include main( ) {  Calling Function } } calculation() int…….; {  Called Function }

Structure of a Function The general structure is return type function name (list of parameters) Syntax: parameter declaration. { Statements : }

Body of the Function The body of the function contains a variable declaration part, processing and a return statement. The variables declared within the body of a function are called ‘local variables’. These variables can be used or referred to only within the body of that function and hence the name.

These variables have no meaning outside the body of that function. The body of the function is given within braces. Example: cube ( int x ) { int cubes; cubes = x*x*x; return (cubes); }

The Return-Statement The calculation part can contain either assignment statement or any valid arithmetic logic expressions. If there is a value that has to be returned to the calling function The return statement is used.

The general syntax is : return [value]; Example: # include main( ) { int a ; a = retfun( ); printf(“a = %d”,a) getch( ); } retfun( ) { return(1); } The output is a = 1 Example of Code Program

Function Declaration Any C function by default returns an int value. If a function should return a value other than integer. then it is necessary to explicitly mention so in the calling function declaration or function prototype.

The general format is: data-type function-name (list of parameters);

# include main ( ) { float x, y ; float div (float a, float b); printf (“enter 2 number”); scanf (“%f%f”,&x,&y); printf (“The div value is %f”,div (x,y); getch ( ); } Float div (a,b) Float a,b; { Return(a/b); }

Void Function Type If the function returns no value then it is called void function. Such function must be declared as a void function. In general void is an empty data type. General form Void function name ( )

Example-1 #include void welcome (char name [ ]); { Printf (“ Welcome % s” name); }

Example-2 # include void fun1 (void); void fun2 (viod); main ( ) { clrscr ( ) ; fun1 ( ) ; getch ( ) ; } void fun2(void) { Printf (“C Programming”); }

Parameter in Functions A function is called by specifying its name followed by a list of arguments. When a function is invoked, the formal parameters are initialized to the actual parameters used in the function call operator. The corresponding arguments in the function Program are called actual parameter.

Declaration main ( ) { a = area (l,b); } { area (x,y) …… } main ( ) { int a,b; result = mul (a,b): …… } mul(i,j); int i,j; { …… }

LIBRARY FUNCTIONS  C offers a rich collection of library functions.  Any of these functions can be called from any C program.  To invoke these functions we have to include the appropriate header file in the program.

Utility Functions Utility Functions is symbol by. It is an header file. Utility Functions has servral types. Following are given below

atof ( ) This is used to convert a string into a floating-point value. The general format is atof (string); Example: main ( ) { char a [20] = “222.33”; float c; C = atof (a); } The output will be C =

atoi ( ) This function is used to convert a string into an integer value..The general form is: atoi (string); Example: main ( ) { char a[20] = “2233”; int c; c = atof(a) + 5; } The output is C= 2238

Mathematical Functions Mathematical Functions Symbol It has several types. Following are given below;- floor ( ) This function is used to find the floor of a value x.

The floor of x is the first smaller integer. The general format is: Floor (x); Example:- floor (2,9) = 2 floor (-2.1) = 3

ceil ( ) This function returns the round value of the nearest highest integer. The general format is: ceil (x); Example: ceil(2.6) = 3 ceil(5.2) = 6 ceil(-4.3) = -4

pow ( ) This function is used to find xy. The general format is: Pow (x,y); Example: pow (2.3); The output is 8.

exp ( ) This function is used to find the exponential value. The general format is: Exp(x);

String Function String Function is symbol It also using character and words. These functions are for string manipulations. These functions can be classified according to their performance as follows:-

Joining of two strings 1) strcat 2) strncat i.strcat: This function is used to add two strings. The result is stored in the destination string that is the first string. The general form is: strcat(string 1, string 2);

ii. strncat: This function is similar to ‘strcat()’ except. That it binds only n characters of string 2 with string 1. The general form is: strncat(string 1, string 2, n);

Conversion of Strings 1) strlur: This function is used to convert a string containing uppercase to lowercase. The general Format is: strlwr (string);

2) strupr: This function is used to convert a string containing lowercase to uppercase. The general format is: strupr (string); 3) strrev: This function is used to reverse the string. The general format is strrev (string);

4) strset: This function is used to replace all the characters in a string with a particular given character. The general format is: strset(string, char); where the string will be replaced by the character.

5) strnset: This function is similar to strset ( ). The only difference is that we can specify the number of characters to be replaced in the given string. The general format is: strnset (string, char, n);

Comparison of string 1)strcmp This function is used to compare the two strings. The function is case sensitive. The general format is: strcmp (string 1, string2); Example: string 1 = “Canada” string 2 = “Africa”

2) strncmp : This function will compare just n characters of string 2 with string 1. This is case sensitive. The general format is: stricmp(string 1, string 2, n);

3) stricmp : This function is similar to strcmp(). It is an except that this is non-case sensitive. The general format is: stricmp(string 1, string 2);

Other String Functions 1)Strlen This function is used to find the length of the string. This function will not count null- terminating character. The general format is: strlen(string);

2) strcpy This function is used to copy string 2 to string 1 including the null terminating character of string. The general format is: strcpy (string1, string2); Here string 1 is the destination where the string 2 will be copied. After the execution the old characters in string 1 will be completely erased.

3) Strncpy This function is similar to strcpy(), except that it copies n characters on string 2 to string 1. The general format is: strncpy(string1, string, n);

Character Class Test Functions Character class test functions is symbol by. 1)isalnum ( ) Tests whether a character is an alphabet or a number. Isalnum(int c);

2. islower ( ) Test whether a charcter is a lower case or not. Islower (int c); 3. isupper ( ) Tests whether the charcter is a uppercase. Isupper(int c);

2. islower ( ) Test whether a character is a lower case or not. Islower (int c); 3. isupper ( ) Tests whether the charcter is a uppercase. Isupper(int c);

4) tolower ( ) This function translates character to lowercase. Tolower(int c); 5. toupper ( ) This function translates character to uppercase. Toupper (int c);