Strings in C
By: Prof. Ganesh Ingle
Course Main Topic/Chapters
Introduction
Declaration/Initialization of an Array
String Library function
Introduction
1. An array is a collection of data items, all of the
same type, accessed using a common name.
2. Strings are defined as an array of characters.
3. The difference between a character array and a
string is the string is terminated with a special
character '0'. Declaration of strings: Declaring a
string is as simple as declaring a one
dimensional array.
4. C language does not directly support string as a
data type. Hence, to display a string in 'C', you
need to make use of a character array.
Declaration/Initialization of an Array
Syntax for declaring a variable as a string is as
follows:
char string_variable_name [array_size];
Example:
char first_name[15]; //declaration of a string variable
char last_name[15];
The C compiler automatically adds a NULL character '0' to
the character array created.
Declaration/Initialization of an Array
Following example demonstrates the initialization of a
string variable:
• char first_name[15] = "ANTHONY";
• char first_name[15] = {'A','N','T','H','O','N','Y','0'};
// NULL character '0' is required at end in this declaration
• char string1 [6] = "hello";
/* string size = 'h'+'e'+'l'+'l'+'o'+"NULL" = 6 */
• char string2 [ ] = "world";
/* string size = 'w'+'o'+'r'+'l'+'d'+"NULL" = 6 */
• char string3[6] = {'h', 'e', 'l', 'l', 'o', '0'} ;
/*Declaration as set of characters ,Size 6*/
Declaration/Initialization of an Array
Input String
1. Input single character at a time
for(i=0;i<n;i++)
scanf(“ %c”, &name[i]);
2. Using “ %s”
scanf(“ %s”, name);
3.Using Library function
String Library Function
• gets();
• strlen();
• strcat();
• strncat();
• strcmp();
• strncmp();
• strrev();
• strstr();
• strnstr();
• strcpy();
• strncpy();
THANK YOU
Image Source
searchenterpriseai.techtarget.com
wikipedia

Strings in c language

  • 1.
    Strings in C By:Prof. Ganesh Ingle
  • 2.
  • 3.
    Introduction 1. An arrayis a collection of data items, all of the same type, accessed using a common name. 2. Strings are defined as an array of characters. 3. The difference between a character array and a string is the string is terminated with a special character '0'. Declaration of strings: Declaring a string is as simple as declaring a one dimensional array. 4. C language does not directly support string as a data type. Hence, to display a string in 'C', you need to make use of a character array.
  • 4.
    Declaration/Initialization of anArray Syntax for declaring a variable as a string is as follows: char string_variable_name [array_size]; Example: char first_name[15]; //declaration of a string variable char last_name[15]; The C compiler automatically adds a NULL character '0' to the character array created.
  • 5.
    Declaration/Initialization of anArray Following example demonstrates the initialization of a string variable: • char first_name[15] = "ANTHONY"; • char first_name[15] = {'A','N','T','H','O','N','Y','0'}; // NULL character '0' is required at end in this declaration • char string1 [6] = "hello"; /* string size = 'h'+'e'+'l'+'l'+'o'+"NULL" = 6 */ • char string2 [ ] = "world"; /* string size = 'w'+'o'+'r'+'l'+'d'+"NULL" = 6 */ • char string3[6] = {'h', 'e', 'l', 'l', 'o', '0'} ; /*Declaration as set of characters ,Size 6*/
  • 6.
  • 7.
    Input String 1. Inputsingle character at a time for(i=0;i<n;i++) scanf(“ %c”, &name[i]); 2. Using “ %s” scanf(“ %s”, name); 3.Using Library function
  • 8.
    String Library Function •gets(); • strlen(); • strcat(); • strncat(); • strcmp(); • strncmp(); • strrev(); • strstr(); • strnstr(); • strcpy(); • strncpy();
  • 9.