0% found this document useful (0 votes)
41 views32 pages

Unit-2 Strings

Uploaded by

Ilesh Shah
Copyright
© © All Rights Reserved
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)
41 views32 pages

Unit-2 Strings

Uploaded by

Ilesh Shah
Copyright
© © All Rights Reserved
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/ 32

UNIT – 2 STRINGS

1 Marks - 7
SYLLABUS
 2.1 String representation : Reading and Writing
Strings
 2.2 String operations :

 Finding length of a string,


 Converting Characters of a string into upper
case and lower case,
 Concatenation of two strings to form a new
string,
 Appending,
 Reversing a string, Copying a string,
 Comparing strings, Insertion, 2

 Substring, Deletion
STRINGS
 String is defined as a sequence of characters.
 In terms of c language string is an array of characters.

 While storing the string in character array the size of


the array must be one more then the size of the string.
Because the last character in the string is ‘\0’ or
NULL. The string terminated with NULL or ‘\0’ is
known as null terminated string.
 For example: To store “HELLO” in array the array
must be declared as char a[5].
 For example:

String “HELLO” is stored as shown in fig 3

H E L L O \0
 String Character set:
Lower case: a to z
Upper case: A to Z
Number: 0 to 9
Special Characters: + - * % / ( ) [ ] { } $ # &,. ? Etc.
 Now we can assign a value to the string as
char name[10]=”ketan”
OR
char name[10]={‘k’,’e’,’t’,’a’,’n’,’\0’};
 For Dynamic allocation we use,
gets(string name); OR scanf(“%s”, string name);
 Example:
gets(name); OR scanf(“%s”,name); 4
puts(name); OR printf(“%s”,name);
STRING OPERATIONS
 Finding length of a string,
 Converting Characters of a string into upper case
and lower case,
 Concatenation of two strings to form a new
string,
 Appending,

 Reversing a string, Copying a string,

 Comparing strings, Insertion,

 Substring, Deletion

5
FINDING THE LENGTH OF STRING [STRLEN(STR)]
 This function finds the length of the string.
 str is the string whose length is to be find

 Example:

char str[20]={“VPMP POLYTECHNIC”};


int len;
len=strlen(str);
printf(“String length = %d”,len);
 Output:

String length = 16

6
ALGORITHM [STRLEN(STR)]
STEP 1: [initialize]
len0
STEP 2: [Process until end of the string]
Repeat step 3 until str!=NULL
STEP 3: [increment counter]
str str+1
lenlen + 1
STEP 4: [Display Length]
Write len
STEP 5:[Finished]
Exit

7
CONVERT THE CASE OF STRING: STRLWR(STR)
 This function is used to convert the case of string
from uppercase to lowercase.
 Example:

char str[20]={“VPMP POLYTECHNIC”};


printf(“Lower case String = %s”,strlwr(str));
 Output:

Lower case String = vpmp polytechnic


 Algorithm

STEP 1 [Initialization]
i0
len  strlen(STR)
STEP 2 8

repeat upto step 5 while (i<len)


STEP 3 [ convert the case of string]
if (STR[i]>=65 and STR[i]<=90) then
STR[i]  STR[i] + 32
else
STR[i]  STR[i]
STEP 4 [Increment the counter]
ii+1
STEP 5 [Display string]
Write STR
STEP 6 [Finished]
Exit 9
CONVERT THE CASE OF STRING: STRUPR(STR)
 This function is used to convert the case of string
from lowercase to uppercase.
 Example:

char str[20]={“Vpmp Polytechnic”};


printf(“Uppercase String = %s”,strupr(str));
 Output:

Uppercase String = VPMP POLYTECHNIC


 Algorithm

STEP 1 [Initialization]
i0
len  strlen(STR)
STEP 2 10

repeat upto step 5 while (i<len)


STEP 3 [ convert the case of string]
if (STR[i]>=97 and STR[i]<=122) then
STR[i]  STR[i] - 32
else
STR[i]  STR[i]
STEP 4 [Increment the counter]
ii+1
STEP 5 [Display string]
Write STR
STEP 6 [Finished]
Exit 11
CONCATENATION OF TWO STRINGS TO
FORM A NEW STRING : STRCAT(S1,S2,S3)

 This function concatenate two strings s1 and s2 in to


the string s3.
 Example:

char s1[5]={“vpmp”};
char s2[15]={“polytechnic”};
char s2[20];
strcat(s1,s2,s3);
printf(“s3 = %s”,s3);
 Output:

s3= vpmppolytechnic
12
Algorithm
STEP 1: [initialize]
i0
j0
k0
STEP 2: [process until end of first string]
Repeat step 3 until s1 [i] !=NULL
STEP 3: [copy character by character and increment
counter]
s3 [k] s1 [i]
i  i+1
k  k+1
STEP 4: [process until end of first string] 13
Repeat step 3 until s2 [j] !=NULL
STEP 5: [copy character by character and
increment counter]
s3 [k] s2 [j]
j  j+1
k  k+1
STEP 6: [finish]
s3 [k] NULL
Exit

14
APPENDING
 This function append one string at the end of
another string.
 Example:
char s1[5]={“vpmp”};
char s2[15]={“polytechnic”};
strcat(s1,s2);
printf(“s1 = %s”,s1);
 Output:
s1=vpmppolytechnic
 Algorithm
STEP 1: [initialize]
i0
15
j0
STEP 2: [process until end of first string]
Repeat step 3 until s2 [i] !=NULL
STEP 3: [copy character by character and increment
counter]
s1 [j] s2 [i]
i  i+1
j  j+1
STEP 4: [finish]
s1 [j] NULL
Exit
16
REVERSE A STRING
 This function is used to reverse the given string.
 Example:

char str[10]={“vpmp”};
printf(“Reverse string = %s”,strrev(str));
 Output:

Reverse string = pmpv


 Algorithm:

STEP 1:[find the length of string]


l  strlen(STR1)
STEP 2: [Initialization]
il-1
17
j 0
STEP 3: [Copy the string]
Repeat while <i>=0)
STR2[j] STR1[i]
jj+1
ii-1
STEP 4: [Display string]
STR2[j]  null
Write (STR2)
STEP 5: [Finished]
Exit 18
COPYING A STRING [STRCPY(STR1,STR2)]
 This function copies the string source in to the string
destination.
 Example:

char str1[10];
char str2[10]={“vpmp”};
strcpy(str1,str2);
printf(“Str1= %s”,str1);
 Output:

Str1=vpmp

19
ALGORITHM
STEP 1: [initialize]
i0
STEP 2: [Find the length of string]
l strlen(STR2)
STEP 3: [copy character by character]
repeat while (i<>l)
STR1[i] STr2[i]
i i+1
STEP 4: [Display string]
STR1 [i]NULL
write STR1
STEP 5: [Finished]
Exit 20
COMPARING STRINGS
 This function compare two strings s1 and s2 and find
weather they are equal or not.
 Example:
char s1[10]={“VPMP”};
char s2[10]={“VPmP”};
if(strcmp(s1,s2)==0)
{
printf(“Both are same”);
}
else
{
printf(“Both are different”);
}
 Output: 21
Both are different
 Algorithm:
STEP 1: [initialize]
i0
flag0
STEP 2: [Find Length of two strings]
L1strlen (s1)
L2strlen (s2)
STEP 3: [Compare Length of two string]
if (L1 != L2) then
Write “Strings are not equal”
22
STEP 4: [Process string]
Repeat step 5 while i < L1
STEP 5: [compare character by character]
If s1 [i] !=s2 [i] then
flag 1
Else
ii + 1
STEP 6: [Are string equal?]
If flag == 0 then
Write “Strings are equal”
Else
Write “Strings are not equal”
STEP 7: [Finished] 23

Exit
INSERTION

 This function is used to insert a string into


existing string.
 Example:

char str[20]={“Gujarat University”};


char new[15]={“Technological”};
char temp[35];
INSERT(str,9,13);
printf(“Answer = %s”,temp);
 Output:

Gujarat Technological University


24
 Algorithm: INSERT(str, position,new)
STEP 1: [Initialization]
i0
j0
STEP 2: [Find the length of strings]
l1  strlen(str)
l2  strlen(new)
STEP 3: [copy the characters upto position]
repeat while (i<position)
temp[i]  str[i]
i i+1
25
STEP 4: [copy the new string]
repeat while (j<l2)
temp[i] new[j]
i i+1
j j+1
STEP 5: [copy the remaining characters]
repeat while(position<l1)
temp[i] str[position]
i i+1
position position+1
STEP 6: [Display string]
write temp
26
STEP 7: [Finished]
Exit
SUBSTRING [SUBSTR(STR,START,LEN)]
 This function finds the string sub string from the given
string.
 Here START indicates the starting position in given
string.
 LEN indicates number of character after starting
position.
 Example:

char str[30]={“Gujarat Technological University”};


printf(“Answer = %s”, substr(str,8,13));
 Output:

Technological 27
Algorithm: SUBSTR(S1,START,LEN)
STEP 1: [initialize]
i0
STEP 2: [Process the string]
Repeat step 3 until LEN < 0
STEP 3: [Compare Length of two string]
SUB[i] S1 [START]
ii+1
STARTSTART+1
LENLEN-1
STEP 4: [Finished] 28
SUB[i] NULL
DELETION
 This function is used to delete a string from
existing string.
 Example:

char str[30]={“Gujarat Technological University”};


char new[20];
printf(“Answer = %s”, delete(str,9,13);
 Output:

Gujarat University

29
 Algorithm: DELETE(str, position,n)
STEP 1: [Initialization]
i0
STEP 2: [Find the length of strings]
l  strlen(str)
STEP 3: [copy the characters upto position]
repeat while (i<position)
temp[i]  str[i]
i i+1
STEP 4:
j position+n
30
STEP 5: [copy the remaining characters]
repeat while(j<l)
temp[i] str[j]
i i+1
j j+1
STEP 6: [Display string]
write temp
STEP 7: [Finished]
Exit
31
END

32

You might also like