Practical No 5
Practical No 5
Practical No 5
5
To study and perform program based on handling of Strings.
.
Theory:
In this practical, we learn how to declare strings, how to work with strings in C
programming and how to use the pre-defined string handling functions.
String Declaration
Method 1:
char address[]={'T', 'E', 'X', 'A', 'S', '\0'};
In the above declaration NULL character (\0) will automatically be inserted at the end of the
string.
/*Displaying String*/
printf("%s",nickname);
return 0;
}
Output:
Enter your Nick name:Negan
Negan
Note: %s format specifier is used for strings input/output
puts(nickname);
return 0;
}
int main()
{
char string1[20];
int i, length;
int flag = 0;
printf("Enter a string:");
scanf("%s", string1);
length = strlen(string1);
if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}
Output:
int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
printf("\nDigits: %d",digits);
printf("\nWhite spaces: %d", spaces);
return 0;
}
OUTPUT:
C – String functions
#include<string.h>
include this library to your program to use some of the inbuilt string manipulation functions
present in the library.
There are about 20-22 inbuilt string manipulating functions present in this library.
The most common functions used from the library are:
1. strlen("name of string")
2. strcpy( dest, source)
3. strcmp( string1, string2 )
4. strstr( str1, str2 )
strlen( string )
Declaration and usage of strlen() function
It is one of the most useful functions used from this library, whenever we need the length of the
string say "str1"
we write it as
int len;
len = strlen(str1);
len will be the length of the string "str1".(it will include all the spaces and characters in the string
except the NULL('\0') character.
Return value
This returns a pointer to the destination string dest.
Example
Below code shows the usage of strcpy() function.
#include<stdio.h>
#include<string.h>
int main( )
{
char str1[ ] = "I love programming" ;
char str2[20]= "" ;
printf ( "source string(i.e str1) = %s\n", str1 ) ;
printf ( "dest string(i.e str2) = %s\n", str2) ;
strcpy ( str2, str1 ) ;
printf ( "target( i.e str2) string after strcpy( ) = %s\n", str2 ) ;
return 0;
}
Output
Return value
- if Return value if < 0 then it indicates str1 is less than str2
- if Return value if > 0 then it indicates str2 is less than str1
- if Return value if = 0 then it indicates str1 is equal to str2
Normal approach for comparing two strings is
void compare( char str1[], char str2[]){
int l1,l2,i;
l1=strlen(str1);
l2=strlen(str2);
if(l1!=l2){
printf("string %s and string %s are not equal\n",str1,str);
}
else{
for( i = 0; i < l1 ; i++){ // l1 or l2 anyone as both are equal.
if(str1[i]!=str2[i]){
printf("string %s and string %s are not equal\n",str1,str);
break;
}
}
if(i==l1){ //if the for loop has ran for the whole l1 lenth.
printf("string %s and string %s are equal\n",str1,str);
}
}
}
you can yourself see that the first comparing approach is always better and time saving too.
strstr(str1, str2)
This library function finds the first occurrence of the substring str2 in the string str1. The
terminating '\0' character is not compared.
Declaration
strstr( str1, str2);
- str1: the main string to be scanned.
- str2: the small string to be searched in str1.
This function is very useful in checking whether str2 is a substring of str1 or not.
Return value
This function returns a pointer to the first occurrence in str1 of any of the entire sequence of
characters specified in str2, or a NULL pointer if the sequence is not present in str1.
Example
Below code shows the usage of strstr() function.
#include<stdio.h>
#include<string.h>
int main ()
{
char str1[55] ="This is a test string for testing";
char str2[20]="test";
char *p;
p = strstr (str1, str2);
if(p)
{
printf("string found\n" ); //i.e str2 is a substring of str1.
printf ("First occurrence of string \"test\" in \"%s\" is"\
" \"%s\"",str1, p);
}
else printf("string not found\n" ); // str2 is not a substring of str1.
return 0;
}
Output
string found
First occurrence of string “test” in “This is a test string for testing” is “test string for testing”
Conclusion:
In this way we had studied and perform program based on handling of strings.