Programming in C & Matlab: Strings & Functions
Programming in C & Matlab: Strings & Functions
& MATLAB
1
What is a String?
2
String - examples
3
Strings – storage in arrays
D E P A R T M E N T O F T E C H N I C A L E
D U C A T I O N A N D T R A I N I N G \0
4
Strings – storage in arrays
S T A T E B O A R D O F T E C H N I C A L
E D U C A T I O N A N D T R A I N I N G \0
5
Strings – NULL (0) character
6
6
NULL characters - example
7
String Literals
Evaluating ″dog″ results in memory allocated for
three characters ′d ′, ′ o ′, ′ g ′, plus terminating NUL
8
8
Strings - Declaration
9
Strings – storage in arrays
P O L Y T E C H N I C \0
4002 4009
4004 4007
4000 4005 4010
4001 4006 4011
4003 4008
10
C Strings (Character Arrays)
11
Strings – usage with arrays
char student_name[21];
12
String initialization
char
string_name[length]={‘literal1’,’literal2’,’literal3’,
….,’literal(length-1),’\0’};
or
char string_name[length]=“literals of maximum
length (length-1)”
13
Declarations and Initializing strings
Array of char:
char str[5] = {'l', 'i', 'n', 'u', 'x'};
char str[6] = {'l', 'i', 'n', 'u', 'x', '\0'};
Declarations
14
String initialization - example
Or
char student_name[21]=“SRINIVAS”;
15
String Handling functions
16
String i/o functions
17
String Handling
scanf() function:
It is used to read a string from keyboard wordwise as
follows
ex:
char name[15];
scanf(“%s” ,name);
18
String Handling
printf() function
A string(word) can be printed using this
function as follows
printf(“%s”,name);
19
String Handling
gets()
puts()
20
String Handling functions
21
String Handling functions
Ex:
char name[15];
gets(name);
puts(name);
These instructions read an entire line of
text from keyboard and print on to the
monitor
22
String i/o functions -program
23
String i/o functions -program
24
String i/o
25
String I/O functions
getchar()
It appends NULL character upon
encountering a newline character
putchar()
It prints out an entire line of text ending
with a new line character
26
String functions
27
C-style strings
Comparing strings
#include <string.h>
if (s1 = = s2) { /* do s1 and s2 point to the same array?
(typically not what you want) */
}
if (strcmp(s1,s2) = = 0) { /* do s1 and s2 hold the same characters? */
}
Finding the lengths of a string
int lgt = strlen(s); /* note: goes through the string at run time
looking for the terminating 0 */
Copying strings
strcpy(s1,s2); /* copy characters from s2 into s1
be sure that s1 can hold that many characters */
28
28
String functions - program
29
String functions - program
30
String functions - program
31
String functions - program
32
String functions - program
33
Function techniques in C
34
Function definition
35
Function Types
36
Library Functions
37
User defined functions
38
Advantages of user defined functions
39
Advantages of user defined functions
Contd..
40
Features of functions
41
Syntax of function
return-type function-name(datatype
arg1,datatype arg 2,…datatype arg n)
{
local variable declarations;
statement sequence;
return(expression);
}
42
Syntax of a function
contd….
43
Syntax of a function
contd….
44
Syntax of a function
contd….
Function may be without arguments.
Example:2
int demo()
{
-----------
------------
}
45
Syntax of a function
contd….
If function returns nothing or takes no
arguments we use void
Example
void display(void)
{
------------
------------
}
46
Function Explanation
Function body consists of local variable
declarations
47
Return statement
Syntax : return(expression); or
return();
Causes an immediate exit from the function
in which it occurs
48
Function parameters
49
Calling a function
- It’s name,
50
Calling a function
Contd..
There will be one actual parameter for each
formal parameter
51
Calling a function
contd..
Examples
main()
{
int a,b,c;
--------
max(a,b,c);/* a,b,c are actual parameters*/
--------
}
52
Calling a function
contd..
Example:-1
int max(int x, int y, int z) /* x,y,z are formal
parameters*/
{
------
------
}
53
Calling a function
contd..
54
Calling a function
contd..
Example:-2
#include<stdio.h>
main()
{
int product = mul(10,10);
printf(“The result is:%d”, product);
}
int mul(int i, int j)
{
return(i*j);
}
55
Function prototype
Tells the compiler the type of data returned by the
function
Types of arguments
56
Function prototype
Syntax
Return_ type function_name(dt1,dt2..dtn);
57
Function prototype
Example:1
#include<stdio.h>
int big(int,int,int);/*function proto type*/
main()
{
int a,b,c;
scanf(“%d%d%d”,&a,&b,&c);
printf(“biggest is:%d”,big(a,b,c));
}
58
Function prototype
Contd..
59
Local variables
60
Local variables
Example Contd..
var(int i,int j)
{
int a,b;
---------
}
61
Local variables
Contd..
62
Global variables
63
Global variables
Contd..
64
Global variables
Contd..
Example
#include<stdio.h>
int a,b=6;
void fun();
main()
{
a=8;
fun();
}
65
Global variables
Contd..
void fun()
{
int area;
area = a*b;
printf(“%d”, area);
}
66
Global variables
Contd..
67
Differences between local and global variables
68
Local variables Global variables
Contd..
4)Same variables can be Variables have constant
used in different values throughout the
programs program
69
Parameters can be passed to a function in two
ways
Call by value
Call by reference
70
Call by value
71
Call by value
Write a program to compute the area, circumference of a circle with given radius
#include<stdio.h>
main()
{
float r,a,c;
void compute(float);
printf(“Enter the radius of the circle”);
compute(r);
}
void compute(float x)
{
float area,circum;
area=3.14*x*x;
circum=2*3.14*x;
printf(“area = %f ; circumference = %f \n”,area,circum);
}
72
Call by reference
73
Call by reference
Contd..
74
Call By Reference
Write a program to calculate are and circumference of a circle for given
radius
#include<stdio.h>
main()
{
float r,a,c;
void compute(float,float *,float *);
printf(“Enter the radius of the circle”);
compute(r,&a,&c);
printf(“ \n area = %f circumference= %f \n”,a,c);
}
void compute(float x,float *y,float *z)
{
*y=3.14*x*x;
*z=2*3.14*x;
}
75
Call By Reference
76
Call By Reference
77
Call By Reference
78
Call By Reference
79
Call By Reference
void sort(m,x)
int m,x[];
{
int I,j,t;
for(i=1;i<=m-1;i++)
for(j=1;j<=m-I;j++)
if(x[j-1]>=x[j])
{
t=x[j-1];x[j-1]=t;x[j]=t;
}
}
80
Call By Reference
Output
marks before sorting
40 90 73 81 35
81
Recursion
82
Advantage of recursion
Recursive function is
Clear
Short
83
Advantage of recursion
Contd..
Commonly used example of a recursive
procedure is finding the factorial of a number
N! = N*(N-1)*(N-2)*……..1
84
Advantage of recursion
Contd..
N! = N*(N-1)! with 0! = 1
85
Program for factorial of a number
#include<stdio.h>
int fact(int);
main()
{
int n;
scanf(“%d”,&n);
printf(“factorial of number %d is %d\n”,n,fact(n));
}
86
Program for factorial of a number
int fact (int x) contd,..
{
int f =1;
if(x==1)
return(1);
else if(x>0)
f=x*fact(x-1);
return(f);
}
87
Program to find sum of digits of a number
#include<stdio.h>
int sumdigit(int);
main()
{
int n;
scanf(“%d”,&n);
printf(“sum of digits is %d:”,sumdigit(n));
}
88
Program to find sum of digits of a number
Contd..
int sumdigit(int x)
{
int sum =0;
if(x==0)
return(sum);
else
sum=(x%10 + sumdigit(x/10));
return(sum);
}
89