Practical No 5

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Practical no.

5
To study and perform program based on handling of Strings.
.
Theory:

String is an array of characters.

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'};

Method 2: The above string can also be defined as –


char address[]="TEXAS";

In the above declaration NULL character (\0) will automatically be inserted at the end of the
string.

What is NULL Char “\0”?


'\0' represents the end of the string. It is also referred as String terminator & Null Character.
String I/O in C programming

Read & write Strings in C using Printf() and Scanf() functions


#include <stdio.h>
#include <string.h>
int main()
{
/* String Declaration*/
char nickname[20];

printf("Enter your Nick name:");

/* I am reading the input string and storing it in nickname


* Array name alone works as a base address of array so
* we can use nickname instead of &nickname here
*/
scanf("%s", nickname);

/*Displaying String*/
printf("%s",nickname);

return 0;
}
Output:
Enter your Nick name:Negan
Negan
Note: %s format specifier is used for strings input/output

Read & Write Strings in C using gets() and puts() functions


#include <stdio.h>
#include <string.h>
int main()
{
/* String Declaration*/
char nickname[20];

/* Console display using puts */


puts("Enter your Nick name:");

/*Input using gets*/


gets(nickname);

puts(nickname);

return 0;
}

C Program to Check if a given String is Palindrome


#include <stdio.h>
#include <string.h>

int main()
{
char string1[20];
int i, length;
int flag = 0;

printf("Enter a string:");
scanf("%s", string1);

length = strlen(string1);

for(i=0;i < length ;i++)


{
if(string1[i] != string1[length-i-1])
{
flag = 1;
break;
}
}

if (flag) {
printf("%s is not a palindrome", string1);
}
else {
printf("%s is a palindrome", string1);
}
return 0;
}

Output:

Program to count vowels, consonants etc.


#include <stdio.h>

int main()
{
char line[150];
int i, vowels, consonants, digits, spaces;

vowels = consonants = digits = spaces = 0;

printf("Enter a line of string: ");


scanf("%[^\n]", line);

for(i=0; line[i]!='\0'; ++i)


{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&&
line[i]<='Z'))
{
++consonants;
}
else if(line[i]>='0' && line[i]<='9')
{
++digits;
}
else if (line[i]==' ')
{
++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.

strcpy( dest, source)


This function copies the string "source" to string "dest".
Declaration
strcpy( str2, str1 );

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

source string(i.e str1) = I love programming


dest string(i.e str2) =
target( i.e str2) string after strcpy( ) =I love programming
it is also easy to copy one string to another via a for loop, as
int i,len;
len=strlen(str1);
for( i=0 ; i < len ; i++ ){
str2[i]=str1[i];
}
//last index in the string was len-1
//now outside for loop value of i=len.
str2[i]='\0'; // don't ever forget to terminate the string with '\0'.
either way is good to copy but using strcpy is a good habit to have.

strcmp( str1, str2 )


This function is used to compare two strings "str1" and "str2". this function returns zero("0") if
the two compared strings are equal, else some none zero integer.
Declaration and usage of strcmp() function
strcmp( str1 , str2 ); //declaration
//using this function to check given two strings are equal or not.
if( strcmp( str1, str2 )==0){
printf("string %s and string %s are equal\n",str1,str2);
}
else
printf("string %s and string %s are not equal\n",str1,str2);

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.

// let say str2="speed";


//function can also be written as
strstr(str1, "speed" );

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.

You might also like