Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
Strings(character Array) 
Abhineet Anand 
Computer Science and Engg. Department 
University of Petrolium and Energy Studies, Dehradun 
November 20, 2014 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
Introduction 
A string is a one-dimensional array of Characters. 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
Reading and Writing String 
Arrays 
A string can be read in or written out using the input/output 
statements. 
Reading string 
scanf() with the %s format specfication. 
gets() function 
getchar() function several times. 
Writing string 
printf() with the %s format specfication. 
puts() function 
putchar() function several times. 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
Examples 
Write a program to accept a string and count the number of 
characters in it and print the same. 
Write a program to accept a string and reverse it, with 
using any predefined string function. 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
String Compare 
String Copy and Concatenation 
String Length and Comparing of n characters 
Upper to Lower and appending of n characters 
String Handling Functions 
C Support a number of string handling functions. 
Header File - string.h 
Some of the string handling functions are 
Strcmp()- function 
The function compare two strings character by character 
and return one of the three values-1,0,1 
Syntax - strcmp(string1,string2); 
-1 - if ASCII value of the charecter of first string is less than 
of the second. 
0 - if both strings are same 
1 - if both strings are different 
Example: p=strcmp("ABC","abc") 
Here ASCII value of A(65)is less than a(97), so the result 
will be -32. 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
String Compare 
String Copy and Concatenation 
String Length and Comparing of n characters 
Upper to Lower and appending of n characters 
String Copy and Concatenation 
Strcpy()- function 
The function used to copy one string to the other. 
Syntax - strcpy(string1,string2) 
The Content on string1 will be overwritten by content of 
string2. 
Strcat()- function 
The function used to concatenate. 
Syntax - strcat(string1,string2) 
String string2 is appended to the end of string1. 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
String Compare 
String Copy and Concatenation 
String Length and Comparing of n characters 
Upper to Lower and appending of n characters 
String Length and Comparing of n characters 
Strlen()- function 
The function returns the numbers of characters in the string. 
The string length does not include the’ 
0’(null) character. 
Syntax - strcpy(string1) 
string1 is one dimension array of characters. 
Strncmp()- function 
The function used to compare the first n character of string. 
Syntax - strncmp(string1,string2,n) 
-1 - if ASCII value of the charecter of first string is less than 
of the second. 
0 - if both strings are same 
1 - if both strings are different 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
String Compare 
String Copy and Concatenation 
String Length and Comparing of n characters 
Upper to Lower and appending of n characters 
Upper to Lower and appending of n characters 
Strlwr()- function 
The function is used to convert any uppercase letters in a 
string to its equivalent lowercase. 
Syntax - strlwr(string1) 
string1 is one dimension array of characters. 
Strncat()- function 
The function used to append first n characters of the 
second string at the end of the first string. 
Syntax - strncat(string1,string2,n) 
Strchr()- function 
The function seraches for for specified character in a given 
string. 
Returns NULL if the desired character is not found in the 
string. 
Syntax - strncat(string, desired-char) 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
Assignemnt 
Write program to implement all the string function we have 
discussed in the class. 
Write a program to accept a string and count the number of 
vowels and consonant in it. 
Write a program to accept a line of text and display the 
number of times each character appears in the text. 
Abhineet Anand Strings
Introduction to Strings 
Reading and Writing String 
Examples 
String Handling Functions 
Assignement 
THANK YOU 
Abhineet Anand Strings

String

  • 1.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement Strings(character Array) Abhineet Anand Computer Science and Engg. Department University of Petrolium and Energy Studies, Dehradun November 20, 2014 Abhineet Anand Strings
  • 2.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement Introduction A string is a one-dimensional array of Characters. Abhineet Anand Strings
  • 3.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement Reading and Writing String Arrays A string can be read in or written out using the input/output statements. Reading string scanf() with the %s format specfication. gets() function getchar() function several times. Writing string printf() with the %s format specfication. puts() function putchar() function several times. Abhineet Anand Strings
  • 4.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement Examples Write a program to accept a string and count the number of characters in it and print the same. Write a program to accept a string and reverse it, with using any predefined string function. Abhineet Anand Strings
  • 5.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement String Compare String Copy and Concatenation String Length and Comparing of n characters Upper to Lower and appending of n characters String Handling Functions C Support a number of string handling functions. Header File - string.h Some of the string handling functions are Strcmp()- function The function compare two strings character by character and return one of the three values-1,0,1 Syntax - strcmp(string1,string2); -1 - if ASCII value of the charecter of first string is less than of the second. 0 - if both strings are same 1 - if both strings are different Example: p=strcmp("ABC","abc") Here ASCII value of A(65)is less than a(97), so the result will be -32. Abhineet Anand Strings
  • 6.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement String Compare String Copy and Concatenation String Length and Comparing of n characters Upper to Lower and appending of n characters String Copy and Concatenation Strcpy()- function The function used to copy one string to the other. Syntax - strcpy(string1,string2) The Content on string1 will be overwritten by content of string2. Strcat()- function The function used to concatenate. Syntax - strcat(string1,string2) String string2 is appended to the end of string1. Abhineet Anand Strings
  • 7.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement String Compare String Copy and Concatenation String Length and Comparing of n characters Upper to Lower and appending of n characters String Length and Comparing of n characters Strlen()- function The function returns the numbers of characters in the string. The string length does not include the’ 0’(null) character. Syntax - strcpy(string1) string1 is one dimension array of characters. Strncmp()- function The function used to compare the first n character of string. Syntax - strncmp(string1,string2,n) -1 - if ASCII value of the charecter of first string is less than of the second. 0 - if both strings are same 1 - if both strings are different Abhineet Anand Strings
  • 8.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement String Compare String Copy and Concatenation String Length and Comparing of n characters Upper to Lower and appending of n characters Upper to Lower and appending of n characters Strlwr()- function The function is used to convert any uppercase letters in a string to its equivalent lowercase. Syntax - strlwr(string1) string1 is one dimension array of characters. Strncat()- function The function used to append first n characters of the second string at the end of the first string. Syntax - strncat(string1,string2,n) Strchr()- function The function seraches for for specified character in a given string. Returns NULL if the desired character is not found in the string. Syntax - strncat(string, desired-char) Abhineet Anand Strings
  • 9.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement Assignemnt Write program to implement all the string function we have discussed in the class. Write a program to accept a string and count the number of vowels and consonant in it. Write a program to accept a line of text and display the number of times each character appears in the text. Abhineet Anand Strings
  • 10.
    Introduction to Strings Reading and Writing String Examples String Handling Functions Assignement THANK YOU Abhineet Anand Strings