SlideShare a Scribd company logo
3
Most read
7
Most read
11
Most read
String
in c programming
Prepared By
Devan Thakur
BCA 1st
29-10-2017 1
Strings
• A string is nothing but the collection of the individual array elements or
characters.
• String is enclosed within Double quotes.
• “programming" is a example of String.
• Each Character Occupy 1 byte of Memory.
• Size of “programming“ = 11 bytes
• String is always Terminated with NULL Character (‘0′).
char word[20] = “‘p’ , ‘r’ , ‘o’ , ‘g’ , ‘r’ , ‘a’ , ‘m’ , ‘m’ , ‘I’ , ‘n’ , ‘g’ ,
‘0’”
29-10-2017 2
NULL Character
• NULL Character is also known as string terminating character.
• It is represented by “0”.
• NULL Character is having ASCII value 0
• NULL terminates a string, but isn’t part of it
• important for strlen() – length doesn’t include the NULL
29-10-2017 3
Declaration of a string
• Since we cannot declare string using String Data Type, instead of
which we use array of type “char” to create String.
• Syntax :
• char String_Variable_name [ SIZE ] ;
• Examples :
• char city[30];
• char name[20];
• char message[50];
29-10-2017 4
Rules for declaring a string
• String / Character Array Variable name should be legal C Identifier.
• String Variable must have Size specified.
• char city[];
• Above Statement will cause compile time error.
• Do not use String as data type because String data type is included in
later languages such as C++ / Java. C does not support String data type
• When you are using string for other purpose than accepting and
printing data then you must include following header file in your code –
#include<string.h>
29-10-2017 5
Initializing String (Character Array)
• Process of Assigning some legal default data to String is
Called Initialization of String.
• There are different ways of initializing String in C Programming –
• Initializing Unsized Array of Character
• Initializing String Directly
• Initializing String Using Character Pointer
29-10-2017 6
Functions of string.h
29-10-2017 7
Function Purpose Example Output
Strcpy(); Makes a copy of a string strcpy(s1, “Hi”); Copies “Hi” to ‘s1’ variable
Strcat(); Appends a string to the end
of another string
strcat(“Work”, “Hard”); Prints “WorkHard”
Strcmp(); Compare two strings
alphabetically
strcmp(“hi”, “bye”); Returns -1.
Strlen(); Returns the number of
characters in a string
strlen(“Hi”); Returns 2.
Strrev(); reverses a given string Strrev(“Hello”); olleH
Strlwr(); Converts string to lowercase Strlwr(“HELLO”); hello
Strupr(); Converts string to uppercase Strupr(“hello”); HELLO
String Copy (strcpy)
• strcpy( ) function copies contents of one string into another string.
• Syntax : strcpy (destination_string , source_string );
• Example:
strcpy ( str1, str2) – It copies contents of str2 into str1.
strcpy ( str2, str1) – It copies contents of str1 into str2.
• If destination string length is less than source string, entire source string value
won’t be copied into destination string.
• For example, consider destination string length is 20 and source string length
is 30. Then, only 20 characters from source string will be copied into
destination string and remaining 10 characters won’t be copied and will be
truncated.
29-10-2017 8
String Concat (strcat)
• strncat( ) function in C language concatenates (appends) portion of one
string at the end of another string.
• Syntax : strncat ( destination_string , source_string, size);
• Example :
strncat ( str2, str1, 3 ); – First 3 characters of str1 is concatenated at the
end of str2.
strncat ( str1, str2, 3 ); – First 3 characters of str2 is concatenated at the
end of str1.
• As you know, each string in C is ended up with null character (‘0’).
• In strncat( ) operation, null character of destination string is overwritten
by source string’s first character and null character is added at the end
of new destination string which is created after strncat( ) operation.29-10-2017 9
String Compare (strcmp)
• strcmp( ) function in C compares two given strings and
returns zero if they are same.
 If length of string1 < string2, it returns < 0 value that is -1.
 If length of string1 > string2, it returns > 0 value that is 1
 If length of string1 = string2 it returns 0.
• Syntax : strcmp (str1 , str2 );
• strcmp( ) function is case sensitive. i.e, “A” and “a” are
treated as different characters.
29-10-2017 10
String Length (strlen)
• strlen( ) function in C gives the length of the given
string.
• Syntax : strlen(str);
• strlen( ) function counts the number of characters in a given
string and returns the integer value.
• It stops counting the character when null character is found.
Because, null character indicates the end of the string in C.
29-10-2017 11
FAQ’S
• How to declare character array?
• Define string?
• What are various functions of string?
• How to compare to strings?
29-10-2017 12
Bibliography
• https://www.programiz.com/c-programming/c-string-examples
• https://www.tutorialspoint.com/cprogramming/c_strings.htm
• http://www.cprogramming.com/tutorial/c/lesson9.html
• http://fresh2refresh.com/c-programming/c-strings/
29-10-2017 13
THANK YOU
29-10-2017 14

More Related Content

PPTX
Strings in C language
PPTX
Strings in C
PPTX
PPT
Strings in c
PPT
Strings
PDF
Character Array and String
PPTX
C programming - String
Strings in C language
Strings in C
Strings in c
Strings
Character Array and String
C programming - String

What's hot (20)

PPTX
Pointers in C Programming
PPTX
Dynamic memory allocation in c
PPTX
Recursive Function
PPTX
Functions in C
PPTX
Function in C program
PPTX
String C Programming
PPTX
Constructor and Types of Constructors
PPTX
Pointer in C++
PPT
Multidimensional array in C
PPTX
List in Python
PPT
RECURSION IN C
PPT
structure and union
PDF
Python file handling
PPTX
Structure in C
PPT
Array in c
PPTX
Programming in c Arrays
PPTX
Control statements in c
PPTX
Arrays in c
PPTX
Call by value
Pointers in C Programming
Dynamic memory allocation in c
Recursive Function
Functions in C
Function in C program
String C Programming
Constructor and Types of Constructors
Pointer in C++
Multidimensional array in C
List in Python
RECURSION IN C
structure and union
Python file handling
Structure in C
Array in c
Programming in c Arrays
Control statements in c
Arrays in c
Call by value
Ad

Similar to String in c programming (20)

PPT
CP-STRING.ppt
PPT
CP-STRING (1).ppt
PPT
CP-STRING.ppt
PPT
CPSTRINGSARGAVISTRINGS.PPT
PPT
BHARGAVISTRINGS.PPT
DOCX
Unitii string
PPT
14 strings
PPTX
Week6_P_String.pptx
PDF
[ITP - Lecture 17] Strings in C/C++
PDF
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
PDF
Principals of Programming in CModule -5.pdfModule-4.pdf
PPTX
String in programming language in c or c++
PPTX
All_About_Strings_in_C_Programming.pptxn
PDF
Strings in c mrs.sowmya jyothi
PPT
Strings(2007)
PPTX
fundamentals of c programming_String.pptx
PPTX
Strings CPU GTU
DOCX
C Programming Strings.docx
PPT
Lecture 17 - Strings
CP-STRING.ppt
CP-STRING (1).ppt
CP-STRING.ppt
CPSTRINGSARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
Unitii string
14 strings
Week6_P_String.pptx
[ITP - Lecture 17] Strings in C/C++
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
Principals of Programming in CModule -5.pdfModule-4.pdf
String in programming language in c or c++
All_About_Strings_in_C_Programming.pptxn
Strings in c mrs.sowmya jyothi
Strings(2007)
fundamentals of c programming_String.pptx
Strings CPU GTU
C Programming Strings.docx
Lecture 17 - Strings
Ad

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Complications of Minimal Access Surgery at WLH
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PPTX
Cell Structure & Organelles in detailed.
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Insiders guide to clinical Medicine.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Basic Mud Logging Guide for educational purpose
PDF
Business Ethics Teaching Materials for college
PPTX
Institutional Correction lecture only . . .
STATICS OF THE RIGID BODIES Hibbelers.pdf
Classroom Observation Tools for Teachers
Complications of Minimal Access Surgery at WLH
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Cell Structure & Organelles in detailed.
2.FourierTransform-ShortQuestionswithAnswers.pdf
Insiders guide to clinical Medicine.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Week 4 Term 3 Study Techniques revisited.pptx
Renaissance Architecture: A Journey from Faith to Humanism
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Supply Chain Operations Speaking Notes -ICLT Program
O5-L3 Freight Transport Ops (International) V1.pdf
TR - Agricultural Crops Production NC III.pdf
Basic Mud Logging Guide for educational purpose
Business Ethics Teaching Materials for college
Institutional Correction lecture only . . .

String in c programming

  • 1. String in c programming Prepared By Devan Thakur BCA 1st 29-10-2017 1
  • 2. Strings • A string is nothing but the collection of the individual array elements or characters. • String is enclosed within Double quotes. • “programming" is a example of String. • Each Character Occupy 1 byte of Memory. • Size of “programming“ = 11 bytes • String is always Terminated with NULL Character (‘0′). char word[20] = “‘p’ , ‘r’ , ‘o’ , ‘g’ , ‘r’ , ‘a’ , ‘m’ , ‘m’ , ‘I’ , ‘n’ , ‘g’ , ‘0’” 29-10-2017 2
  • 3. NULL Character • NULL Character is also known as string terminating character. • It is represented by “0”. • NULL Character is having ASCII value 0 • NULL terminates a string, but isn’t part of it • important for strlen() – length doesn’t include the NULL 29-10-2017 3
  • 4. Declaration of a string • Since we cannot declare string using String Data Type, instead of which we use array of type “char” to create String. • Syntax : • char String_Variable_name [ SIZE ] ; • Examples : • char city[30]; • char name[20]; • char message[50]; 29-10-2017 4
  • 5. Rules for declaring a string • String / Character Array Variable name should be legal C Identifier. • String Variable must have Size specified. • char city[]; • Above Statement will cause compile time error. • Do not use String as data type because String data type is included in later languages such as C++ / Java. C does not support String data type • When you are using string for other purpose than accepting and printing data then you must include following header file in your code – #include<string.h> 29-10-2017 5
  • 6. Initializing String (Character Array) • Process of Assigning some legal default data to String is Called Initialization of String. • There are different ways of initializing String in C Programming – • Initializing Unsized Array of Character • Initializing String Directly • Initializing String Using Character Pointer 29-10-2017 6
  • 7. Functions of string.h 29-10-2017 7 Function Purpose Example Output Strcpy(); Makes a copy of a string strcpy(s1, “Hi”); Copies “Hi” to ‘s1’ variable Strcat(); Appends a string to the end of another string strcat(“Work”, “Hard”); Prints “WorkHard” Strcmp(); Compare two strings alphabetically strcmp(“hi”, “bye”); Returns -1. Strlen(); Returns the number of characters in a string strlen(“Hi”); Returns 2. Strrev(); reverses a given string Strrev(“Hello”); olleH Strlwr(); Converts string to lowercase Strlwr(“HELLO”); hello Strupr(); Converts string to uppercase Strupr(“hello”); HELLO
  • 8. String Copy (strcpy) • strcpy( ) function copies contents of one string into another string. • Syntax : strcpy (destination_string , source_string ); • Example: strcpy ( str1, str2) – It copies contents of str2 into str1. strcpy ( str2, str1) – It copies contents of str1 into str2. • If destination string length is less than source string, entire source string value won’t be copied into destination string. • For example, consider destination string length is 20 and source string length is 30. Then, only 20 characters from source string will be copied into destination string and remaining 10 characters won’t be copied and will be truncated. 29-10-2017 8
  • 9. String Concat (strcat) • strncat( ) function in C language concatenates (appends) portion of one string at the end of another string. • Syntax : strncat ( destination_string , source_string, size); • Example : strncat ( str2, str1, 3 ); – First 3 characters of str1 is concatenated at the end of str2. strncat ( str1, str2, 3 ); – First 3 characters of str2 is concatenated at the end of str1. • As you know, each string in C is ended up with null character (‘0’). • In strncat( ) operation, null character of destination string is overwritten by source string’s first character and null character is added at the end of new destination string which is created after strncat( ) operation.29-10-2017 9
  • 10. String Compare (strcmp) • strcmp( ) function in C compares two given strings and returns zero if they are same.  If length of string1 < string2, it returns < 0 value that is -1.  If length of string1 > string2, it returns > 0 value that is 1  If length of string1 = string2 it returns 0. • Syntax : strcmp (str1 , str2 ); • strcmp( ) function is case sensitive. i.e, “A” and “a” are treated as different characters. 29-10-2017 10
  • 11. String Length (strlen) • strlen( ) function in C gives the length of the given string. • Syntax : strlen(str); • strlen( ) function counts the number of characters in a given string and returns the integer value. • It stops counting the character when null character is found. Because, null character indicates the end of the string in C. 29-10-2017 11
  • 12. FAQ’S • How to declare character array? • Define string? • What are various functions of string? • How to compare to strings? 29-10-2017 12
  • 13. Bibliography • https://www.programiz.com/c-programming/c-string-examples • https://www.tutorialspoint.com/cprogramming/c_strings.htm • http://www.cprogramming.com/tutorial/c/lesson9.html • http://fresh2refresh.com/c-programming/c-strings/ 29-10-2017 13