Strings in 'C' :

Strings in 'C' :
  • String is a sequence of characters enclosed in double quotes and terminated by null character ("\0").
  • Since 'C' language does not provides string data type array of character data type is used to store strings in 'C'. 
  • While scanning string through scanf statement %s format specifier is used along with array name in which string to be stored. 
  • Declaration syntax for string is char str_name[size];
  • For example char str[5]; will store string of five characters in it. 
  • Initialization : String can be initialized at the time of declaration itself or afterwards in a program.
  • Example char str[5]="Hello";  Or char str[5]; str[]="Hello";
  • String can be literal or variable.
  • Literal means a constant.
  • String which has assigned a value in a program itself can be called as string literal since it has predefined value. 
  • But if we are accepting a string value from user then string can have any value thus it can be said as string variable.
  • Consider following programs to illustrate strings :
1) Program using String as a literal


#include<stdio.h>
int main() 
{
 char str[50]="\nHello\n Welcome to strings in C\t bye...";
 printf("%s",str);
 return 0; 
}

Output :
Hello
Welcome to strings in C bye...

2) Program using string as a variable 
#include<stdio.h>
int main() 
{
 //declaring string variable 
 char str[10];
 printf("\n Enter String : ");
 scanf("%s",str);
 printf("\n You entered : %s",str);
 return 0; 
}

Output :
 Enter String : Hello
 You entered : Hello

3) Program to illustrate access of string character by character using array 

#include<stdio.h>
#include<string.h>
int main() 
{
 //declaring string variable 
 char str[30];
 int i;
 printf("\n Enter String : ");
 scanf("%s",str);
 printf("\n your string is :\n");
 //displaying string using array char by char
 for(i=0;i<strlen(str);i++)
 printf("%c",str[i]);
 return 0; 
}

Output :
 Enter String :Welcome
 your string is :
 Welcome

String Functions :
  • 'C' language provides various built-in methods to work with strings. 
  • These methods are called as string functions. 
  • All these methods resides in a header file string.h. 
  • Thus in order to use them we must include string.h in our program. 
  • Some widely used string functions are as follow :

1) strcmp()
  • As the name indicates these function is used to compare two strings. Syntax : strcmp(s1,s2); this function returns value 0 if both strings are same or equal. 
  • It returns positive value if s1 is greater than s2 and negative value if s1 is less than s2. It uses ASCII value of each character in a string for comparison. 
  • It compares string until they are same if not then returns from first non-matched character.
2) strncmp() :
  • This function is similar to strcmp() but it only compares first n characters of two strings. 
  • Syntax : strncmp(s1,s2,n);

3) strcat() : 
  • This function is used for string concatenation or appending. 
  • Syntax : strcat(s1,s2); It appends string2 to the string1. (Thus string1 will be changed.)
4) strncat() : 
  • It is similar to strcat except that it appends only first 'n' characters of string2 to string1.
  • Syntax : strncat(s1,s2,n);

5) strlen() : 
  • This function returns length of string passed to it as an argument. That means it returns count of characters including spaces in a string. 
  • Syntax : strlen(s);

6) strcpy() :  
  • This function copies string2 to string1. If string 1 already holds some value then it replaces first n (n is length of string2) characters by string1 other characters remains same. 
  • Syntax : strcpy(s1,s2);

7) strncpy() : 
  • It similar to strcpy function except that it copies only first n characters of string2 to string1. 
  • Syntax : strncpy(s1,s2,n);

8) strchr() : 
  • This function shift string pointer to the first occurence of character c in a string.
  •  Syntax : strchr(string,c);

9) strrchr() : 
  • This function is similar to strchr except that pointer will be shifted to last occurence of character c in string. 
  • Syntax : strrchr(string,c);

  • Program to illustrate string functions :
#include<stdio.h>
#include<string.h>
int main() {
char s1[50],s2[50],str[50],r[50];
printf("\n Enter String1: ");
printf("%s",s1);
printf("\n Enter String2: ");
printf("%s",s2);
printf("\n String1: %s String2 : %s ",s1,s2);
//using string functions
strcmp(s1,s2)==0)
printf("\n Both strings are same");
else
printf("\n Both strings are not same");
printf("\n Lenght of string1 =%d",strlen(s1));
printf("\n concatening string1 and string 2 we get : %s",strcat(s1,s2));
printf("\n Appending 3 characters of string1 to string 2 we get : %s",strncat(s2,s1,3));
strcpy(str,s1);
printf("\n copying string1 to str : %s",str);
strncpy(r,s1,3);
printf("\n Copying first 3 characters of string1 to r : %s",r);
//finding first occurence 
printf("\n String after l in s2 is : %s",strchr(s2,'l'));
ntf("\n String after last 'e'in s2 is : %s",strrchr(s2,'e'));
if(strstr(s1,"Hello"))
printf("\n Hello is found in string1 ");
else
printf("\n Hello is not found in string1");
return 0;
}

Output : 
 Enter String1: HelloWorld 
 Enter String2: Welcomefriend
String1:HelloWorldString2: Welcomefriend 
 Both strings are not same
 Lenght of string1 =10
concatening string1 and string 2 we get : HelloWorldWelcomefriend
Appending 3 characters of string1 to string 2 we get : WelcomefriendHel
copyings tring1 to str: HelloWorldWelcomefriend
Copying first 3 characters of string1 to r : Hel
String after l in s2 is : lcomefriendHel
String after last 'e'in s2 is : el
Hello is found in string1


Array of Strings in 'C' language  :

  • Basically a single string is itself an array of characters in 'C'. 
  • Thus array of strings is a simply a two dimensional array of characters. 
  • Syntax : char str_arr[rows][cols]; 
  • For example  char str_arr[20][50];                  is a two dimensional array with 20 rows and 50 columns. 
  • Thus we can store maximum 20 strings it in by using one string per row. 
  • Let's see following program  :


#include<stdio.h>
int main()
{
 char str_arr[20][50];
 int n,i;
 printf("\n Enter how many strings: ");
 scanf("%d",&n);
 for(i=0;i<n;i++)
 {
 printf("\n Enter String%d : ",i);
 //storing string at row
 scanf("%s",str_arr[i]);
 }
 printf("\n Your strings are");
 for(i=0;i<n;i++)
 {
 printf("\n %s",str_arr[i]);
 }
return 0;
}

Output :
 Enter how many strings:
 Enter String0 : Hello
 Enter String1 : Welcome
 Enter String2 : Computer
 Enter String3 : Science
 Your strings are
 Hello
 Welcome
 Computer
 Science


Command line arguments in 'C' :
  • Arguments which are accepted through command line are called command line arguments. 
  • Command line arguments are generally passed to the main function. 
  • In 'C' language when we accept command line arguments syntax for main function becomes
int main(int argc,char * argv)
{
  _____
  _____
}
Where argc stores count of arguments passed from command line and argv is a pointer pointing to array vector argv.

  • Array vector argv stores actual arguments passed to main function. 
  • Note that whatever arguments we pass to main function from command line are accepted as a string values. 
  • Consider following programs

1) Program to accept and display command line arguments

#include<stdio.h>
#include<string.h>
int main(int argc,char * argv[])
{
int i,n=argc;
if(n==1)
printf("\n Arguments not given from command line...");
else
{
for(i=0;i<=n;i++)
printf("\n argument %d is : %s",i,argv[i]);
}
return 0;
}

Run command: ./a.out
Output 1:
Arguments not given from command line...


Run command : ./a.out Hello Welcome to command line
Output 2:
argument 0 is : ./a.out
argument 1 is : Hello
argument 2 is : Welcome
argument 3 is : to
argument 4 is : command
argument 5 is : line


2) Program  to compare two strings using command line arguments

#include<stdio.h>
#include<string.h>
int main(int argc,char * argv[])
{
int result=strcmp(argv[1],argv[2]);
if(result==0)
printf("\n Given strings are equal...");
else
printf("\n Given strings are different...");
return 0;
}

Run command : ./a.out Hello World
Output 1 :
Given strings are different...

Run command : ./a.out Hello Hello
Output 2:
Given strings are equal...


3) Program to calculate addition of two numbers using command line arguments


#include<stdio.h>
#include<stdlib.h>
int main(int argc,char * argv[])
{
int n1,n2;
//string to int conversion
n1=atoi(argv[1]);
n2=atoi(argv[2]);
printf("\n Addition of given numbers = %d",n1+n2);
return 0;
}

Run command: ./a.out 45 78
Output :
Addition of given numbers = 123

Note : atoi() is a function which converts asci value to integer form. Since arguments in argument vector are strings or we must convert them to integer form before operating on them.

Comments

Popular posts from this blog

Write a program to allocate memory dynamically for n integers. Accept the elements and calculate their sum and average

C program to accept names of n cities and search for city named “Pune”.