(XI) C Lab Work 5 - String

Theory of Strings

Definition of String:
String is the set of characters,digits and symbols. We can also define string as the array of characters. The end of the string is marked with a special character , the ‘\0’ that means Null character. The size in a character string represents the maximum number of characters that the string can hold.
Syntax:
char string_name[string_size];
As in above syntax, char refers to the character data type that will be stored in the array, string_name is the name of the string variable and string_size indicates the number of characters that can be stored inside the array.
Example
char name[10];
char address[10];
This declares the name as a character array (string) variable that can hold a maximum of 10 characters.
String is simply a sequence of characters, so to declare a string, we have to put data type char with string name with square brackets.

Initialization of Strings

Let us take a string:
char name[5]={‘a’, ‘l ’, ‘e’, ‘x’,‘\0’};
or second method
char name[5]= “alex” ;
Each character is treated as an element of the array name and is stored in the memory as follows:
name[0] name[1] name[2] name[3] name[4]
'a' 'l' 'e' 'x' '\0'
Index 0 Index 1 Index 2 Index 3 Index 4

When the compiler sees a character string, it terminates it with an additional null character means empty. Thus, the element name[4] holds the null character ‘\0’ at the end. When declaring a character array, we must always allow one extra element space for the null terminator.

Array of String

A two dimensional array of characters is called an array of strings. It is also known as a table of strings. It is very useful for solving string sorting problems. In other words, an array of strings can be declared and handled like a two dimensional array.
Syntax:
char string_name[MAX][SIZE];
As in above syntax, char refers to character data type, string_name is the name of the string variable and in two dimensional array, [MAX] indicates the number of strings and [SIZE] indicates the maximum number characters associated with string.
Example
char name[5][10];
In this example, here is a two dimensional character array. The array is initialized with five character strings with maximum size ten.

String Handling Functions

The C library supports a large number of string handling functions that can be used to carry out many of the string manipulation. The header file #include<string.h> is used for string manipulation functions. Some of the common string manipulation functions are:
1. strlen() function
The strlen() function returns the length of a string which takes the string name as an argument.
Syntax:
l=strlen(str);
where, str is the name of string and l is the length of the string, returned by function.
2. strrev() function
The strrev() function is used to reverse the given string.
Syntax:
strrev(str);
where str is string to be reversed.
3. strcpy() function
The strcpy() function is used to copy one string into another string.
Syntax:
strcpy(destination,source);
OR
strcpy(strl,str2);
where strl,str2 are two strings.
The content of string str2 is copied on string str1.
4. strcat() function
The strcat() function is used to join or concatenate one string into another string.
Syntax:
strcat(strl,str2);
where str1,str2 are two strings. The content of string str2 is concatenated with string str1.
5. strcmp( ) function
The strcmp() function is used to compare two strings, character by character and stops comparison when there is a difference in the ASCII value or the end of any one string and returns ASCII difference of the character that is integer.
Syntax:
v=strcmp(strl,str2);
Where, str1 and str2 are two strings to be compared and v is a value returned by the function which is either zero(equal), positive value(descending) or negative value(ascending).
6. strlwr() function
The strlwr() function is used to convert upper case characters of string into lower case characters.
Syntax:
strlwr (str);
where str is string to be converted into lower case characters.
7. strupr() function
The strupr() function is used to convert lower case characters of string into upper case characters.
Syntax:
strupr (str);
where str is string to be converted into upper case characters.
1. Write C Program to input any five strings and print them. #include<stdio.h> #include<string.h> int main() { char str[5][100]; int i; for(i=0;i<5;i++) { printf("Enter %d string : ",i+1); scanf("%s",str[i]); } printf("-----Output-----\n"); for(i=0;i<5;i++) { printf("%s\n",str[i]); } return 0; } Output Enter 1 string : KOSHI Enter 2 string : JANAKPUR Enter 3 string : POKHARA Enter 4 string : KARNALI Enter 5 string : BAGMATI -----Output----- KOSHI JANAKPUR POKHARA KARNALI BAGMATI
2. Write C Program to find length of entered string using strlen(). #include<stdio.h> #include<string.h> int main() { char str[100]; int i; printf("Enter a string : "); scanf("%s",str); i=strlen(str); printf("Length of string is %d",i); return 0; } Output Enter a string : computer Length of string is 8
3. Write C Program to read a string with space and reverse that using strrev(). #include<stdio.h> #include<string.h> int main() { char str[50]; printf("Enter string : "); gets(str); strrev(str); printf(" Reverse of given string : %s\n",str); return 0; } Output Enter string : computer science Reverse of given string : ecneics retupmoc
4. Write C Program to read a string and convert that into lower case and upper case. [use strlwr() and strupr()] #include<stdio.h> #include<string.h> int main() { char str[50]; printf(" Enter string : "); gets(str); printf(" String in Upper Case : %s\n",strupr(str)); printf(" String in Lower Case : %s\n",strlwr(str)); return 0; } Output Enter string : Computer Science String in Upper Case : COMPUTER SCIENCE String in Lower Case : computer science
5. Write C Program to copy a string into another variable using strcpy(). #include<stdio.h> #include<string.h> int main() { char str1[50],str2[50]; printf(" Enter a string : "); gets(str1); strcpy(str2,str1); printf(" String after copy : %s",str2); return 0; } Output Enter a string : computer science String after copy : computer science
6. Input any two strings and know which has greater ASCII value.[Note: use strcmp()] #include<stdio.h> #include<string.h> int main() { char str1[50],str2[50]; int a; printf(" Enter first string : "); gets(str1); printf(" Enter second string : "); gets(str2); a=strcmp(str1,str2); if(a>0) printf(" %s has greater ASCII value than %s ",str1,str2); else printf(" %s has greater ASCII value than %s ",str2,str1); return 0; } Output Enter first string : computer Enter second string : science science has greater ASCII value than computer
7. Read a string with space and concatenate with another string using strcat(). #include<stdio.h> #include<string.h> int main() { char str1[50],str2[50]; int a; printf(" Enter first str : "); gets(str1); printf(" Enter second str : "); gets(str2); strcat(str1,str2); printf(" String after concatenation : %s ",str1); return 0; } Output Enter first str : PLK Enter second str : compute SIR String after concatenation : PLK compute SIR
8. Input a string and find total length without using strlen().[Note: use loop with null concept] #include<stdio.h> #include<string.h> int main() { char str[100]; int i; printf("Enter any string : "); gets(str); for(i=0;str[i]!='\0';i++); // no body so use semicolon printf("The length of string is %d",i); return 0; } Output Enter any string : apple The length of string is 5
9. Input a string and find total number of digits, vowels and consonants.[use null and ASCII value with loop] #include<stdio.h> #include<string.h> int main() { char s[1000]; int i,vowels=0,consonants=0,digits=0; printf("Enter the string : "); gets(s); strupr(s); for(i=0;s[i];i++) { if(s[i]>=48 && s[i]<=57) digits++; else if(s[i]>=65 && s[i]<=90) { if(s[i]=='A'||s[i]=='E'||s[i]=='I'||s[i]=='O' ||s[i]=='U') vowels++; else consonants++; } } printf("Digits =%d\n",digits); printf("vowels = %d\n",vowels); printf("consonants = %d\n",consonants); return 0; } Output Enter the string : PLK computer SIR viber number is 9861492492 Digits =10 vowels = 9 consonants = 18
10. Write C Program to know whether a string is palindrome or not. #include<stdio.h> #include<string.h> int main() { char str1[100],str2[100]; printf("Enter string : "); gets(str1); strcpy(str2,str1); strrev(str1); if((strcmp(str1,str2)==0)) printf("%s is palindrome",str2); else printf("%s is not palindrome",str2); return 0; } Output Enter string : racecar racecar is palindrome
11. Write C Program to convert a string into lowercase without using strlwr(). #include<stdio.h> #include<string.h> int main() { char Str[100]; int i; printf(" Enter a String : "); gets(Str); for (i = 0; Str[i]!='\0'; i++) { if(Str[i] >= 'A' && Str[i] <= 'Z') { Str[i] = Str[i] + 32; } } printf(" String in Lower Case = %s", Str); return 0; } Output Enter a String : COMPUTER SCIENCE String in Lower Case = computer science
12. Write C Program to input a string with space and print that in uppercase and lowercase alternatively. E.g. For Kathmandu, the output would be KaThMaNdU. #include<stdio.h> #include<ctype.h> int main() { char str[30]; int i; printf(" Enter String : "); gets(str); for(i = 0; str[i] != '\0'; i++) { if( (i % 2) == 0) str[i] = toupper(str[i]); else str[i] = tolower(str[i]); } printf(" %s ", str); return 0; } Output Enter String : kathmandu KaThMaNdU
13. Write C Program to input any ten names of students and arrange them in descending/ascending order. #include<stdio.h> #include<string.h> int main() { char str[50][30],temp[30]; int i,j; for(i=0;i<10;i++) { printf("Enter [%d] Name : ",i+1); gets(str[i]); } for(i=0;i<9;i++) { for(j=i+1;j<10;j++) { if(strcmp(str[i],str[j])>0) // for descending less than zero { strcpy(temp,str[i]); strcpy(str[i],str[j]); strcpy(str[j],temp); } } } printf("\n The sorted names in ascending are : "); for(i=0;i<10;i++) { printf("\n %s",str[i]); } return 0; } Output Enter [1] Name : kripa thapa Enter [2] Name : pawan poudel Enter [3] Name : sara bista Enter [4] Name : diva ranjitkar Enter [5] Name : gracy kunwar Enter [6] Name : amisha roy Enter [7] Name : bibhuti shrestha Enter [8] Name : jenisha shrestha Enter [9] Name : samrat gurung Enter [10] Name : alex lal karn The sorted names are : alex lal karn amisha roy bibhuti shrestha diva ranjitkar gracy kunwar jenisha shrestha kripa thapa pawan poudel samrat gurung sara bista
14. Write C Program to input any ten strings and search a string in that list. #include<stdio.h> #include<string.h> int main() { char str[10][10]={"alex","jenisha","kripa","computer","ktm","janakpur","trinity","neb","apple","football"}; char str1[10]; int i,flag=0; printf("Enter string to be searched : "); scanf("%s",str1); for(i=0;i<10;i++) { if(strcmp(str[i],str1)==0) { flag=1; break; } } if(flag==1) printf("String found and location is %d",i); else printf("Sorry , String not found"); return 0; } Output Enter string to be searched : neb String found and location is 7