Unit 4 Strings and User Defined Functions
Unit 4 Strings and User Defined Functions
RV College of
Engineering
Unit -4
Strings
1
RV College of
Engineering Go, change the world
Contents
2
RV College of
Engineering Strings Go, change the world
4
RV College of
Engineering
Declaration of a string Go, change the world
• Since we cannot declare string using String Data Type, instead of which
we use array of type “char” to create String.
Syntax :
Examples :
• char city[30];
• char name[20];
• char message[50]; 5
RV College of
Engineering
Declaration of a string cont… Go, change the world
6
RV College of
Engineering Rules for declaring a string Go, change the world
7
RV College of
Engineering
Initializing String (Character Array) Go, change the world
• Process of Assigning some legal default data to String is Called Initialization
of String.
•A string can be initialized in different ways. We will explain this with the help
of an example.
• Below is an example to declare a string with name as str and initialize it with
“HelloWorld”.
1. char str[] = “HelloWorld";
2.char str[50] = “HelloWorld";
3.char str[] = {‘H','e',’l',’l',’o',’W','o','r',’l',’d','\0'};
4.char str[11] = {‘H','e',’l',’l',’o',’W','o','r',’l',’d','\0'}; 8
RV College of
Engineering
String Example Go, change the world
#include <stdio.h>
int main () {
When the above code is compiled and executed, it produces the following result −
Greeting message: Hello
9
RV College of
Engineering Reading Strings Go, change the world
10
RV College of
Engineering Writing Strings Go, change the world
11
RV College of
Engineering Suppressing Input Go, change the world
12
RV College of
Engineering Length Go, change the world
13
RV College of
Engineering Converting Character of Strings to Upper Case Go, change the world
14
RV College of
Engineering
Appending Go, change the world
15
RV College of
Engineering Comparing Two Strings Go, change the world
16
RV College of
Engineering
Reversing a String Go, change the world
17
RV College of
Engineering Extracting a substring from Right of the String Go, change the world
18
RV College of
Engineering Extracting a substring from Middle of the StringGo, change the world
19
RV College of
Engineering Insertion Go, change the world
20
RV College of
Engineering Indexing Go, change the world
21
RV College of
Engineering Deletion Go, change the world
22
RV College of
Engineering Replacement Go, change the world
23
RV College of
Engineering Array of Strings Go, change the world
24
RV College of
Engineering Go, change the world
25
RV College of
Engineering Pointers and Strings Go, change the world
• int puts(const char *s); Here the constant modifier is used to assure the
user that the function will not modify the contents pointed to by the
source pointer. Note that the address of the pointer is passed to the
26
function as the argument.
RV College of
Engineering
Functions of string.h Go, change the world
• If destination string length is less than source string, entire source string value won’t be
copied into destination string. For example, consider destination string length is 20 and
source string length is 30. Then, only 20 characters from source string will be
copied into destination string and remaining 10 characters won’t be copied and will
be truncated.
28
RV College of
Engineering String Concat (strcat) Go, change the world
• strncat( ) function in C language concatenates (appends) portion of one string at the end of
another string.
strcmp( ) function in C compares two given strings and returns zero if they are same.
• Syntax : strcmp (str1 , str2 );strcmp( ) function is case sensitive. i.e, “A” and “a” are
treated as different characters.
30
RV College of
Engineering String Concat (strcat) Go, change the world
• strncat( ) function in C language concatenates (appends) portion of one string at the end of
another string.
•Syntax : strlen(str);
•It stops counting the character when null character is found. Because,
null character indicates the end of the string in C.
32
RV College of
Engineering Go, change the world
33
Go, change the world
RV College of
Engineering
UNIT IV
Functions
(User Defined Functions)
RV College of Go, change the world
Engineering
FUNCTIONS
It has a name and it is reusable i.e. it can be executed from as many different parts in a C
Program as required.
Properties of C Functions:
Every function has a unique name. This name is used to call function from “main()” function.
A function can be called from within another function.
A function is independent and it can perform its task without intervention from or interfering
with other parts of the program.
A function performs a specific task. A task is a distinct job that your program must perform as
a part of its overall operation, such as adding two or more integer, sorting an array into
numerical order, or calculating a cube root etc.
RV College of Go, change the world
Engineering
Properties of C Functions:
This is optional and depends upon the task your function is going to accomplish.
Suppose you want to just show few lines through function then it is not necessary to return a value.
But if you are calculating area of rectangle and wanted to use result somewhere in program then you
have to send back (return) value to the calling function.
RV College of Go, change the world
Engineering
Types of functions in C
1. void function(void)
2. void function(int)
3. int function(void)
4. int function(int)
RV College of Go, change the world
Engineering
Type-1
void function(void)
RV College of Go, change the world
Engineering
Output:
Rvce
#include<conio.h>
#include<stdio.h>
void rvce(void);
void main()
{
clrscr();
rvce();
rvce();
getch();
}
void rvce(void)
{
printf(”rvce");
}
RV College of Go, change the world
Engineering
Output:
rvce rvce
#include<stdio.h>
void rvce(void);
void college(void);
void main()
{
clrscr();
rvce();
college();
}
void rvce(void)
{
printf(”rvce");
}
void college (void)
{
printf(“college is rvce");
}
RV College of Go, change the world
Engineering
Output
rvce
college is rvce
RV College of Go, change the world
Engineering
Type-2
void function(int)
RV College of Go, change the world
Engineering
RV College of Go, change the world
Engineering
#include<conio.h>
#include<stdio.h>
void square(int);
void main()
{
clrscr();
square(5);
getch();
}
void square(int x)
{
RV College of Go, change the world
Engineering
Output
Square is 25
52
RV College of Go, change the world
Engineering
#include<conio.h>
#include<stdio.h>
void table(int);
void main()
{
clrscr();
table(5);
getch();
}
void table(int x)
{
for(int a=1;a<=10;a++)
printf("%d*%d=%d\n",a,x,a*x);
}
RV College of Go, change the world
Engineering
RV College of Go, change the world
Engineering
Output
1*5=5
2*5=10
3*5=15
4*5=20 We can pass more than one arguments to a function, each
5*5=25
6*5=30 can have different type/same type.
7*5=35
8*5=40
9*5=45
10*5=50
RV College of Go, change the world
Engineering
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
add(5, 7);
getch();
}
Output
Addition is 12
RV College of Go, change the world
Engineering
void main()
{
clrscr();
getch();
}
Output
2,3,4,5,6,7,8,9,10,11,12,13,14,15
RV College of Go, change the world
Engineering
Edit same program, get two numbers input from user, then
make a loop between these numbers.
Ex:
Please input two numbers: 5 20
5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20
RV College of Go, change the world
Engineering
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int start, end
getch();
}
Type-3
int function(void)
RV College of Go, change the world
Engineering
RV College of Go, change the world
Engineering
#include<conio.h>
#include<stdio.h>
int sum(void);
void main()
{
clrscr();
int a;
a=sum();
printf(“Sum is %d”, a);
getch();
}
int sum(void)
{
int x=2, y=3, z;
z=x+y;
return(z);
}
RV College of Go, change the world
Engineering
#include<conio.h>
#include<stdio.h>
int sum(void);
void main()
{
clrscr();
int a;
a=sum();
printf(“Sum is %d”, a);
getch();
int sum(void)
{
int x=2, y=3, z;
z=x+y;
return(z);
}
RV College of Go, change the world
Engineering
Return Keyword
In functions, where we return any value, we usually use a keyword
“return”, and the value, which is to be returned is written inside braces,
shown after return.
Example: return(z);
The above statement will pass the value of z to the left side variable of
calling function.
ie: a=sum();
Hence the value in z, which is 5 in our program is passed to a.
RV College of Go, change the world
Engineering
Type-4
Returns a value.
int function(int)
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
int a;
a=sum(2,3);
printf(“Sum is %d”, a);
getch();
z=x+y;
return(z);
}
RV College of Go, change the world
Engineering
#include<conio.h>
#include<stdio.h>
int sum(int, int);
void main()
{
clrscr();
int a;
a=sum(2,3);
printf(“Sum is %d”, a);
getch();
z=x+y;
return(z);
}
RV College of Go, change the world
Engineering