Declaration
• A string variable is declared as an array of characters.
Syntax:
char string_name[size];
e.g. char s[5];
• When the compiler assigns a character string to a
character array, it automatically supplies a null character
(‘0’) at the end of the string
VIIT, Pune 1
Initialization
Initialization Syntax:
char myString [] = { 'H','A','E','S', 'L', 'E', 'R', '0' } ;
char myString[13] = “Initial value”
char myString[] = “Initial value”;
0 = null character
Note: that ‘0’ and ‘0’ are not same.
• When we initialize a character array by listing its elements, the null
terminator or the size of the array must be provided explicitly.
• When declaring a string don’t forget to leave a space for the null
character which is also known as the string terminator character
because it is only way the functions that work with a string can know
where the string ends.
n i t i a l v a l u e ? ? …
I 0
Compilation time
initialization
VIIT, Pune 2
Differences Between Strings and Character
Arrays
VIIT, Pune 3
• Memory for strings must be allocated before the string can be
used.
• A string literal is enclosed in double quotes
VIIT, Pune 4
Reading String from terminal
• Input function scanf can be used with %s format specification to
read in a string of characters.
• Example: char address[10]; scanf(“%s”, address);
• The scanf function automatically terminates the string that is read
with a null character.
• %ws format specification can be used for reading a specified
number of characters from the input string.
• scanf with %s or %ws can ready only strings without whitespaces.
• C supports a format specification known as the edit set conversion
code %[..] that can be used to read a line containing a variety of
characters, including whitespaces.
• Example:
char line[80];
scanf(“%[^n]”,line);
VIIT, Pune 5
Library functions
• There are various string handling functions define in
string.h some of them are:
VIIT, Pune 6
strlen()
• In C, strlen() function calculates the length of string.
• It takes only one argument, i.e, string name.
Syntax:
temp_variable = strlen(string_name);
• Function strlen() returns the value of type integer.
VIIT, Pune 7
strcpy()
• Function strcpy() copies the content of one string to the
content of another string.
• Syntax of strcpy()
strcpy(destination,source);
VIIT, Pune 8
strcat()
• concatenates(joins) two strings.
• resultant string is stored in the first string
specified in the argument.
Syntax of strcat()
strcat(first_string,second_string);
VIIT, Pune 9
strcmp()
• compares two string and returns value 0, if the two strings are
equal.
Syntax of strcmp()
temp_varaible=strcmp(string1,string2);
VIIT, Pune 10
strlwr()
• function converts all the uppercase characters in that string to
lowercase characters.
• The resultant from strlwr() is stored in the same string.
Syntax of strlwr():-
strlwr(string_name);
VIIT, Pune 12
strupr()
• function converts all the lowercase characters in that string to
uppercase characters.
• The resultant from strupr() is stored in the same string.
Syntax of strupr()
strupr(string_name);
VIIT, Pune 13
Structure in C
– Introduction to structure
• Definition
• declaration of structure, declaration of structure variables
• initialization,
• accessing members of structure
• Array of structures
Vishwakarma Institute of Information
Technology
14
What is structure
• Structure is user defined data type which allows to combine
data items of different kinds
• Structures are used to represent a record
• For example to keep track of books in a library, following
attributes about each book has to maintained−
– Book ID (Data Type – Int)
– Title (Data Type – Char)
– Author (Data Type – Char)
– Publication (Data Type – Char)
– Price (Data Type – Float)
Vishwakarma Institute of Information
Technology
15

Basic Algorithms and Array along with Structure.pptx

  • 1.
    Declaration • A stringvariable is declared as an array of characters. Syntax: char string_name[size]; e.g. char s[5]; • When the compiler assigns a character string to a character array, it automatically supplies a null character (‘0’) at the end of the string VIIT, Pune 1
  • 2.
    Initialization Initialization Syntax: char myString[] = { 'H','A','E','S', 'L', 'E', 'R', '0' } ; char myString[13] = “Initial value” char myString[] = “Initial value”; 0 = null character Note: that ‘0’ and ‘0’ are not same. • When we initialize a character array by listing its elements, the null terminator or the size of the array must be provided explicitly. • When declaring a string don’t forget to leave a space for the null character which is also known as the string terminator character because it is only way the functions that work with a string can know where the string ends. n i t i a l v a l u e ? ? … I 0 Compilation time initialization VIIT, Pune 2
  • 3.
    Differences Between Stringsand Character Arrays VIIT, Pune 3
  • 4.
    • Memory forstrings must be allocated before the string can be used. • A string literal is enclosed in double quotes VIIT, Pune 4
  • 5.
    Reading String fromterminal • Input function scanf can be used with %s format specification to read in a string of characters. • Example: char address[10]; scanf(“%s”, address); • The scanf function automatically terminates the string that is read with a null character. • %ws format specification can be used for reading a specified number of characters from the input string. • scanf with %s or %ws can ready only strings without whitespaces. • C supports a format specification known as the edit set conversion code %[..] that can be used to read a line containing a variety of characters, including whitespaces. • Example: char line[80]; scanf(“%[^n]”,line); VIIT, Pune 5
  • 6.
    Library functions • Thereare various string handling functions define in string.h some of them are: VIIT, Pune 6
  • 7.
    strlen() • In C,strlen() function calculates the length of string. • It takes only one argument, i.e, string name. Syntax: temp_variable = strlen(string_name); • Function strlen() returns the value of type integer. VIIT, Pune 7
  • 8.
    strcpy() • Function strcpy()copies the content of one string to the content of another string. • Syntax of strcpy() strcpy(destination,source); VIIT, Pune 8
  • 9.
    strcat() • concatenates(joins) twostrings. • resultant string is stored in the first string specified in the argument. Syntax of strcat() strcat(first_string,second_string); VIIT, Pune 9
  • 10.
    strcmp() • compares twostring and returns value 0, if the two strings are equal. Syntax of strcmp() temp_varaible=strcmp(string1,string2); VIIT, Pune 10
  • 12.
    strlwr() • function convertsall the uppercase characters in that string to lowercase characters. • The resultant from strlwr() is stored in the same string. Syntax of strlwr():- strlwr(string_name); VIIT, Pune 12
  • 13.
    strupr() • function convertsall the lowercase characters in that string to uppercase characters. • The resultant from strupr() is stored in the same string. Syntax of strupr() strupr(string_name); VIIT, Pune 13
  • 14.
    Structure in C –Introduction to structure • Definition • declaration of structure, declaration of structure variables • initialization, • accessing members of structure • Array of structures Vishwakarma Institute of Information Technology 14
  • 15.
    What is structure •Structure is user defined data type which allows to combine data items of different kinds • Structures are used to represent a record • For example to keep track of books in a library, following attributes about each book has to maintained− – Book ID (Data Type – Int) – Title (Data Type – Char) – Author (Data Type – Char) – Publication (Data Type – Char) – Price (Data Type – Float) Vishwakarma Institute of Information Technology 15