UNIT 2 STRINGS 1 Marks 7 SYLLABUS 2
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, � Substring, Deletion 2
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 ‘ ’ or NULL. The string terminated with NULL or ‘ ’ 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
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’, ’ ’}; For Dynamic allocation we use, gets(string name); OR scanf(“%s”, string name); Example: gets(name); OR scanf(“%s”, name); puts(name); OR printf(“%s”, name); 4
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+1 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 repeat upto step 5 while (i<len) 8
STEP 3 STEP 4 STEP 5 STEP 6 [ convert the case of string] if (STR[i]>=65 and STR[i]<=90) then STR[i] + 32 else STR[i] [Increment the counter] i i+1 [Display string] Write STR [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 repeat upto step 5 while (i<len) 10
STEP 3 STEP 4 STEP 5 STEP 6 [ convert the case of string] if (STR[i]>=97 and STR[i]<=122) then STR[i] - 32 else STR[i] [Increment the counter] i i+1 [Display string] Write STR [Finished] Exit 11
CONCATENATION OF TWO STRINGS TO FORM A NEW STRING : STRCAT(S 1, S 2, S 3) This function concatenate two strings s 1 and s 2 in to the string s 3. Example: char s 1[5]={“vpmp”}; char s 2[15]={“polytechnic”}; char s 2[20]; strcat(s 1, s 2, s 3); printf(“s 3 = %s”, s 3); Output: s 3= vpmppolytechnic 12
Algorithm STEP 1: [initialize] i 0 j 0 k 0 STEP 2: [process until end of first string] Repeat step 3 until s 1 [i] !=NULL STEP 3: [copy character by character and increment counter] s 3 [k] s 1 [i] i i+1 k k+1 STEP 4: [process until end of first string] 13 Repeat step 3 until s 2 [j] !=NULL
STEP 5: [copy character by character and increment counter] s 3 [k] s 2 [j] j j+1 k k+1 STEP 6: [finish] s 3 [k] NULL Exit 14
APPENDING This function append one string at the end of another string. Example: char s 1[5]={“vpmp”}; char s 2[15]={“polytechnic”}; strcat(s 1, s 2); printf(“s 1 = %s”, s 1); Output: s 1=vpmppolytechnic Algorithm STEP 1: [initialize] i 0 j 0 15
STEP 2: [process until end of first string] Repeat step 3 until s 2 [i] !=NULL STEP 3: [copy character by character and increment counter] s 1 [j] s 2 [i] i i+1 j j+1 STEP 4: [finish] s 1 [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(STR 1) STEP 2: [Initialization] i l-1 j 0 17
STEP 3: [Copy the string] Repeat while <i>=0) STR 2[j] STR 1[i] j j+1 i i-1 STEP 4: [Display string] STR 2[j] null Write (STR 2) STEP 5: [Finished] Exit 18
COPYING A STRING [STRCPY(STR 1, STR 2)] This function copies the string source in to the string destination. Example: char str 1[10]; char str 2[10]={“vpmp”}; strcpy(str 1, str 2); printf(“Str 1= %s”, str 1); Output: Str 1=vpmp 19
ALGORITHM STEP 1: [initialize] i 0 STEP 2: [Find the length of string] l strlen(STR 2) STEP 3: [copy character by character] repeat while (i<>l) STR 1[i] STr 2[i] i i+1 STEP 4: [Display string] STR 1 [i] NULL write STR 1 STEP 5: [Finished] Exit 20
COMPARING STRINGS This function compare two strings s 1 and s 2 and find weather they are equal or not. Example: char s 1[10]={“VPMP”}; char s 2[10]={“VPm. P”}; if(strcmp(s 1, s 2)==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] L 1 strlen (s 1) L 2 strlen (s 2) STEP 3: [Compare Length of two string] if (L 1 != L 2) then Write “Strings are not equal” 22
STEP 4: [Process string] Repeat step 5 while i < L 1 STEP 5: [compare character by character] If s 1 [i] !=s 2 [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] Exit 23
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] l 1 strlen(str) l 2 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<l 2) temp[i] new[j] i i+1 j j+1 STEP 5: [copy the remaining characters] repeat while(position<l 1) temp[i] str[position] i i+1 position+1 STEP 6: [Display string] write temp STEP 7: [Finished] Exit 26
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(S 1, 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] S 1 [START] i i+1 START+1 LEN-1 STEP 4: [Finished] SUB[i] NULL 28
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
- Slides: 31