Unit-2 Strings
Unit-2 Strings
1 Marks - 7
SYLLABUS
2.1 String representation : Reading and Writing
Strings
2.2 String operations :
Substring, Deletion
STRINGS
String is defined as a sequence of characters.
In terms of c language string is an array of characters.
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,
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:
String length = 16
6
ALGORITHM [STRLEN(STR)]
STEP 1: [initialize]
len0
STEP 2: [Process until end of the string]
Repeat step 3 until str!=NULL
STEP 3: [increment counter]
str str+1
lenlen + 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:
STEP 1 [Initialization]
i0
len strlen(STR)
STEP 2 8
STEP 1 [Initialization]
i0
len strlen(STR)
STEP 2 10
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]
i0
j0
k0
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]
i0
15
j0
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:
char str1[10];
char str2[10]={“vpmp”};
strcpy(str1,str2);
printf(“Str1= %s”,str1);
Output:
Str1=vpmp
19
ALGORITHM
STEP 1: [initialize]
i0
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]
i0
flag0
STEP 2: [Find Length of two strings]
L1strlen (s1)
L2strlen (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
ii + 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
Technological 27
Algorithm: SUBSTR(S1,START,LEN)
STEP 1: [initialize]
i0
STEP 2: [Process the string]
Repeat step 3 until LEN < 0
STEP 3: [Compare Length of two string]
SUB[i] S1 [START]
ii+1
STARTSTART+1
LENLEN-1
STEP 4: [Finished] 28
SUB[i] NULL
DELETION
This function is used to delete a string from
existing string.
Example:
Gujarat University
29
Algorithm: DELETE(str, position,n)
STEP 1: [Initialization]
i0
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