Strings

What are Strings
As we know a group of integers can be stored in an integer array,the same way a group of characters can also be stored in a character array. Character arrays are also called strings. Many languages internally treat strings as character arrays,

A string constant is a one-dimensional array of characters terminated by a null(‘\0’).

For example,
char name[ ] = { ‘S’, ‘E’, ‘V’, ‘A’, ‘R’, ‘A’, ‘L’, ‘\0’ } ;

String example

void main( )
{
char name[ ] = "sevaral" ;
int i = 0 ;
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
}

We can scan the string using %s see the example below.

void main( )
{
char name[20];
printf("Enter string : ");
scanf("%s",name);
while ( name[i] != `\0' )
{
printf ( "%c", name[i] ) ;
i++ ;
}
}

we use scanf to read input but for strings scanf( ) is not capable of receiving multi-word strings. hence names such as ‘Amol prakash Ujagare’ would scan ‘Amol’ only. we should use the function gets( ). The usage of functions gets( ) and puts( ) is shown below.

void main( )
{
char name[25] ;
printf("Enter your full name ");
gets(name);
puts("Welcome");
puts(name);
}

String functions

strlen – Finds length of a string
strlwr – Converts a string to lowercase
strupr – Converts a string to uppercase
strcat – Appends one string at the end of another
strncat – Appends first n characters of a string at the end of another
strcpy – Copies a string into another
strncpy – Copies first n characters of one string into another
strcmp – Compares two strings
strncmp – Compares first n characters of two strings
strcmpi – Compares two strings without regard to case (“i” denotes that this function ignores case)
stricmp – Compares two strings without regard to case (identical to strcmpi)
strnicmp – Compares first n characters of two strings without regard to case
strdup – Duplicates a string
strchr – Finds first occurrence of a given character in a string
strrchr – Finds last occurrence of a given character in a string
strstr – Finds first occurrence of a given string in another string
strset – Sets all characters of string to a given character
strnset – Sets first n characters of a string to a given character
strrev – Reverses string

Examples on String functions

1. strlen :-

void main( )
{
char arr[ ] = "amol" ;
int len1, len2 ;
len1 = strlen (arr) ;
len2 = strlen ("amol ujagare") ;
printf("\nstring = %s length = %d",arr, len1) ;
printf("\nstring = %s length = %d","Amol Ujagare",len2) ;
}

out put :

string = amol length = 4
string = amol ujagare length = 12

2.strcpy

void main( )
{
char source[] = "technology";
char target[20];
strcpy(target,source) ;
printf("\nsource string = %s",source);
printf("\ntarget string = %s",target);
}

out put :

source string = technology
target string = technology

3. strcat:-

void main( )
{
char source[ ] = "";
char target[30] = "Hello";
strcat(target,source) ;
printf("\nsource string = %s",source);
printf("\ntarget string = %s",target);
}

out put :

source string = World
target string = HelloWorld

4.strcmp

void main( )
{
char str1[10],str2[10];
printf("\nEnter first string :");
scanf("%s",&str1);
printf("\nEnter Second string :");
scanf("%s",&str2);
if(strcmp(string1,string2))
printf("\nStrings are not equal");
else
printf("\nStrings are not equal");
}

out put :

Enter first string : abc
Enter Second string :pqr
Strings are not equal


Search

Subscribe to YouTube

Training on Selenium

Like us on facebook

Jobs

Jobs

Cooking with Amol