SlideShare a Scribd company logo
C STRING
By:Er. Anupam Sharma
Assistant Professor
anupamcgctc@gmail.com
C - STRING
 The string in C programming language is actually a one- dimensional array of
characters which is terminated by a null character '0'. Thus a null-
terminated string contains the characters that comprise the string followed
by a null.
 A string can be declared as a character array or with a string pointer.
 The following declaration and initialization create a string consisting of the
word "Hello". To hold the null character at the end of the array, the size of
the character array containing the string is one more than the number of
characters in the word "Hello."
 strings are arrays of chars. String literals are words surrounded
by double quotation marks.
"This is a static string"
Or
Or
char *greeting = “Hello” ;
 Following is the memory presentation of above defined string in
C/C++:
C - STRING
 It's important to remember that there will be an extra character on
the end on a string, literally a '0' character, just like there is
always a period at the end of a sentence. Since this string
terminator is unprintable, it is not counted as a letter, but it still
takes up a space. Technically, in a fifty char array you could only
hold 49 letters and one null character at the end to terminate the
string.
 Actually, you do not place the null character at the end of a string
constant. The C compiler automatically places the '0' at the end
of the string when it initializes the array.
 Let us try to print above mentioned string:
C - STRING
 Note: %s is used to print a string.
String Pointer
 String pointers are declared as a pointer to a char.
 When there is a value assigned to the string pointer the
NULL is put at the end automatically.
 Take a look at this example:
 It is not possible to read, with scanf(), a string with a string
pointer. You have to use a character array and a pointer.
See this example:
STRING POINTER
READING A LINE OF TEXT
gets() and puts() are two string functions to take string input from
user and display string respectively
STRING RELATED OPERATIONS
 Find the Frequency of Characters in a String
 Find the Number of Vowels, Consonants, Digits and White
space in a String
 Reverse a String by Passing it to Function
 Find the Length of a String
 Concatenate Two Strings
 Copy a String
 Remove all Characters in a String except alphabet
 Sort a string in alphabetic order
 Sort Elements in Lexicographical Order (Dictionary Order)
 Change Decimal to Hexadecimal Number
 Convert Binary Number to Decimal
FIND THE FREQUENCY OF CHARACTERS
C PROGRAM TO FIND FREQUENCY OF CHARACTERS IN A STRING
This program computes
frequency of characters in a
string i.e. which character is
present how many times in a
string.
For example in the string
"code" each of the character
'c', 'o', 'd', and 'e' has
occurred one time.
Only lower case alphabets
are considered, other
characters (uppercase and
special characters) are
ignored. You can easily
modify this program to
handle uppercase and
special symbols.
FIND NUMBER OF VOWELS, CONSONANTS, DIGITS AND WHITE SPACE
CHARACTER
Output
REVERSE STRING
To solve this problem,
two standard library
functions strlen() and st
rcpy() are used to
calculate length and to
copy string
respectively.
CALCULATED LENGTH OF A STRING WITHOUTUSING STRLEN()
FUNCTION
You can use standard library function strlen( ) to find the length of a string but,
this program computes the length of a string manually without using strlen( )
funtion.
CONCATENATE TWO STRINGS MANUALLY
You can concatenate two strings using standard library function strcat( ) , this
program concatenates two strings manually without using strcat( ) function.
COPY STRING MANUALLY
You can use the strcpy( )
function to copy the content
of one string to another but,
this program copies the
content of one string to
another manually without
using strcpy( ) function.
REMOVE CHARACTERS IN STRING EXCEPT ALPHABETS
This program
takes a string
from user and for
loop executed
until all characters
of string is
checked. If any
character inside a
string is not a
alphabet, all
characters after it
including null
character is
shifted by 1
position
backwards.
SORT A STRING IN ALPHABETIC
ORDER
C program to sort a string in alphabetic order:
For example if user will enter a string
"programming" then output will be
"aggimmnoprr" or output string will contain
characters in alphabetical order.
SORT ELEMENTS IN LEXICOGRAPHICAL ORDER (DICTIONARY ORDER)
This program takes 10 words
from user and sorts elements in
lexicographical order. To perform
this task, two dimensional string
is used.
C LIBRARY FUNCTIONS
 C supports a wide range of functions that manipulate null-
terminated strings:
Following example makes use of few of the above-mentioned functions:
STRCAT( ) FUNCTION
 strcat( ) function concatenates two given strings. It
concatenates source string at the end of destination string.
 Syntax for strcat( ) function is given below.
char * strcat ( char * destination, const char * source );
 Example :
 strcat ( str2, str1 ); - str1 is concatenated at the end of str2.
strcat ( str1, str2 ); - str2 is concatenated at the end of str1.
 As you know, each string in C is ended up with null character
(‘0′).
 In strcat( ) 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 strcat( ) operation.
EXAMPLE PROGRAM FOR STRCAT( )
 In this program, two strings “is fun” and “C tutorial”
are concatenated using strcat( ) function and result is displayed as
“C tutorial is fun”.
Output:
Source string
Target string
= is fun
= C tutorial
Target string after strcat( ) = C tutorial is fun
Thanks for your time and attention!

More Related Content

PPTX
Pointer in c
lavanya marichamy
 
PPTX
C Programming: Control Structure
Sokngim Sa
 
PPTX
C programming - String
Achyut Devkota
 
PPT
Strings
Nilesh Dalvi
 
PPTX
Types of loops in c language
sneha2494
 
PPTX
Function C programming
Appili Vamsi Krishna
 
PDF
Character Array and String
Tasnima Hamid
 
DOCX
C Programming
Sumant Diwakar
 
Pointer in c
lavanya marichamy
 
C Programming: Control Structure
Sokngim Sa
 
C programming - String
Achyut Devkota
 
Strings
Nilesh Dalvi
 
Types of loops in c language
sneha2494
 
Function C programming
Appili Vamsi Krishna
 
Character Array and String
Tasnima Hamid
 
C Programming
Sumant Diwakar
 

What's hot (20)

PDF
Function in C
Dr. Abhineet Anand
 
PPTX
String In C Language
Simplilearn
 
PPT
Memory allocation in c
Prabhu Govind
 
PPT
358 33 powerpoint-slides_3-pointers_chapter-3
sumitbardhan
 
PPTX
String functions
Kumar Krishnan
 
PPTX
Constructor and destructor
Shubham Vishwambhar
 
PPSX
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
PPTX
Control statements in c
Sathish Narayanan
 
PPTX
Functions in c
sunila tharagaturi
 
PPTX
Data types in c++
Venkata.Manish Reddy
 
PPTX
C if else
Ritwik Das
 
PPT
File handling-c
CGC Technical campus,Mohali
 
PPTX
This keyword in java
Hitesh Kumar
 
PDF
Strings IN C
yndaravind
 
PPTX
User defined function in c
JeevanandhamSubraman
 
PPTX
Presentation on Function in C Programming
Shuvongkor Barman
 
PPTX
Dynamic memory allocation in c
lavanya marichamy
 
DOC
String in c
Suneel Dogra
 
PPTX
Dynamic memory allocation
Viji B
 
PPTX
Unit 5. Control Statement
Ashim Lamichhane
 
Function in C
Dr. Abhineet Anand
 
String In C Language
Simplilearn
 
Memory allocation in c
Prabhu Govind
 
358 33 powerpoint-slides_3-pointers_chapter-3
sumitbardhan
 
String functions
Kumar Krishnan
 
Constructor and destructor
Shubham Vishwambhar
 
C lecture 4 nested loops and jumping statements slideshare
Gagan Deep
 
Control statements in c
Sathish Narayanan
 
Functions in c
sunila tharagaturi
 
Data types in c++
Venkata.Manish Reddy
 
C if else
Ritwik Das
 
This keyword in java
Hitesh Kumar
 
Strings IN C
yndaravind
 
User defined function in c
JeevanandhamSubraman
 
Presentation on Function in C Programming
Shuvongkor Barman
 
Dynamic memory allocation in c
lavanya marichamy
 
String in c
Suneel Dogra
 
Dynamic memory allocation
Viji B
 
Unit 5. Control Statement
Ashim Lamichhane
 
Ad

Similar to string in C (20)

PPTX
C string
University of Potsdam
 
PPTX
String in programming language in c or c++
Samsil Arefin
 
PPT
14 strings
Rohit Shrivastava
 
PPTX
Presentation more c_programmingcharacter_and_string_handling_
KarthicaMarasamy
 
PPT
CPSTRINGSARGAVISTRINGS.PPT
Sasideepa
 
PPT
BHARGAVISTRINGS.PPT
Sasideepa
 
PPT
CP-STRING.ppt
TAPANDDRAW
 
PPT
CP-STRING.ppt
arunatluri
 
PPT
CP-STRING (1).ppt
mounikanarra3
 
PDF
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
PPTX
String in c programming
Devan Thakur
 
PPTX
C - String ppt
Md Razib
 
PPT
String manipulation techniques like string compare copy
Dr. T. Kalaikumaran
 
PPTX
Strings cprogramminglanguagedsasheet.pptx
hyundaitvhamari
 
PPT
Strings
Mitali Chugh
 
PPTX
Module-2_Strings concepts in c programming
CHAITRAB29
 
PPTX
String in programming language in c or c++
Azeemaj101
 
PDF
c programming
Arun Umrao
 
PDF
0-Slot21-22-Strings.pdf
ssusere19c741
 
PPT
Strings(2007)
svit vasad
 
String in programming language in c or c++
Samsil Arefin
 
14 strings
Rohit Shrivastava
 
Presentation more c_programmingcharacter_and_string_handling_
KarthicaMarasamy
 
CPSTRINGSARGAVISTRINGS.PPT
Sasideepa
 
BHARGAVISTRINGS.PPT
Sasideepa
 
CP-STRING.ppt
TAPANDDRAW
 
CP-STRING.ppt
arunatluri
 
CP-STRING (1).ppt
mounikanarra3
 
[ITP - Lecture 17] Strings in C/C++
Muhammad Hammad Waseem
 
String in c programming
Devan Thakur
 
C - String ppt
Md Razib
 
String manipulation techniques like string compare copy
Dr. T. Kalaikumaran
 
Strings cprogramminglanguagedsasheet.pptx
hyundaitvhamari
 
Strings
Mitali Chugh
 
Module-2_Strings concepts in c programming
CHAITRAB29
 
String in programming language in c or c++
Azeemaj101
 
c programming
Arun Umrao
 
0-Slot21-22-Strings.pdf
ssusere19c741
 
Strings(2007)
svit vasad
 
Ad

More from CGC Technical campus,Mohali (20)

PPTX
Gender Issues CS.pptx
CGC Technical campus,Mohali
 
PPTX
Intellectual Property Rights.pptx
CGC Technical campus,Mohali
 
PPTX
Cyber Safety ppt.pptx
CGC Technical campus,Mohali
 
PPTX
Python Modules .pptx
CGC Technical campus,Mohali
 
PPT
Dynamic allocation
CGC Technical campus,Mohali
 
PPT
Control statments in c
CGC Technical campus,Mohali
 
PPTX
Operators in c by anupam
CGC Technical campus,Mohali
 
PPTX
Datatypes in c
CGC Technical campus,Mohali
 
PPT
Fundamentals of-computer
CGC Technical campus,Mohali
 
PPTX
Structure in C language
CGC Technical campus,Mohali
 
PPTX
Function in c program
CGC Technical campus,Mohali
 
PPTX
Function in c
CGC Technical campus,Mohali
 
PPTX
Data processing and Report writing in Research(Section E)
CGC Technical campus,Mohali
 
PPTX
data analysis and report wring in research (Section d)
CGC Technical campus,Mohali
 
PPTX
Section C(Analytical and descriptive surveys... )
CGC Technical campus,Mohali
 
PPTX
Researchmethodology
CGC Technical campus,Mohali
 
Gender Issues CS.pptx
CGC Technical campus,Mohali
 
Intellectual Property Rights.pptx
CGC Technical campus,Mohali
 
Cyber Safety ppt.pptx
CGC Technical campus,Mohali
 
Python Modules .pptx
CGC Technical campus,Mohali
 
Dynamic allocation
CGC Technical campus,Mohali
 
Control statments in c
CGC Technical campus,Mohali
 
Operators in c by anupam
CGC Technical campus,Mohali
 
Fundamentals of-computer
CGC Technical campus,Mohali
 
Structure in C language
CGC Technical campus,Mohali
 
Function in c program
CGC Technical campus,Mohali
 
Data processing and Report writing in Research(Section E)
CGC Technical campus,Mohali
 
data analysis and report wring in research (Section d)
CGC Technical campus,Mohali
 
Section C(Analytical and descriptive surveys... )
CGC Technical campus,Mohali
 
Researchmethodology
CGC Technical campus,Mohali
 

Recently uploaded (20)

PDF
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
PDF
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PDF
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PDF
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PPTX
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
PDF
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
PDF
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
PPT
SCOPE_~1- technology of green house and poyhouse
bala464780
 
PDF
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PPTX
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 
Cryptography and Information :Security Fundamentals
Dr. Madhuri Jawale
 
LEAP-1B presedntation xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
hatem173148
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
top-5-use-cases-for-splunk-security-analytics.pdf
yaghutialireza
 
Software Testing Tools - names and explanation
shruti533256
 
Biodegradable Plastics: Innovations and Market Potential (www.kiu.ac.ug)
publication11
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
MSME 4.0 Template idea hackathon pdf to understand
alaudeenaarish
 
July 2025: Top 10 Read Articles Advanced Information Technology
ijait
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
67243-Cooling and Heating & Calculation.pdf
DHAKA POLYTECHNIC
 
Introduction to Ship Engine Room Systems.pdf
Mahmoud Moghtaderi
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
EVS+PRESENTATIONS EVS+PRESENTATIONS like
saiyedaqib429
 
SCOPE_~1- technology of green house and poyhouse
bala464780
 
Traditional Exams vs Continuous Assessment in Boarding Schools.pdf
The Asian School
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
Victory Precisions_Supplier Profile.pptx
victoryprecisions199
 

string in C

  • 1. C STRING By:Er. Anupam Sharma Assistant Professor [email protected]
  • 2. C - STRING  The string in C programming language is actually a one- dimensional array of characters which is terminated by a null character '0'. Thus a null- terminated string contains the characters that comprise the string followed by a null.  A string can be declared as a character array or with a string pointer.  The following declaration and initialization create a string consisting of the word "Hello". To hold the null character at the end of the array, the size of the character array containing the string is one more than the number of characters in the word "Hello."  strings are arrays of chars. String literals are words surrounded by double quotation marks. "This is a static string" Or Or char *greeting = “Hello” ;
  • 3.  Following is the memory presentation of above defined string in C/C++: C - STRING  It's important to remember that there will be an extra character on the end on a string, literally a '0' character, just like there is always a period at the end of a sentence. Since this string terminator is unprintable, it is not counted as a letter, but it still takes up a space. Technically, in a fifty char array you could only hold 49 letters and one null character at the end to terminate the string.  Actually, you do not place the null character at the end of a string constant. The C compiler automatically places the '0' at the end of the string when it initializes the array.
  • 4.  Let us try to print above mentioned string: C - STRING  Note: %s is used to print a string.
  • 5. String Pointer  String pointers are declared as a pointer to a char.  When there is a value assigned to the string pointer the NULL is put at the end automatically.  Take a look at this example:
  • 6.  It is not possible to read, with scanf(), a string with a string pointer. You have to use a character array and a pointer. See this example: STRING POINTER
  • 7. READING A LINE OF TEXT gets() and puts() are two string functions to take string input from user and display string respectively
  • 8. STRING RELATED OPERATIONS  Find the Frequency of Characters in a String  Find the Number of Vowels, Consonants, Digits and White space in a String  Reverse a String by Passing it to Function  Find the Length of a String  Concatenate Two Strings  Copy a String  Remove all Characters in a String except alphabet  Sort a string in alphabetic order  Sort Elements in Lexicographical Order (Dictionary Order)  Change Decimal to Hexadecimal Number  Convert Binary Number to Decimal
  • 9. FIND THE FREQUENCY OF CHARACTERS
  • 10. C PROGRAM TO FIND FREQUENCY OF CHARACTERS IN A STRING This program computes frequency of characters in a string i.e. which character is present how many times in a string. For example in the string "code" each of the character 'c', 'o', 'd', and 'e' has occurred one time. Only lower case alphabets are considered, other characters (uppercase and special characters) are ignored. You can easily modify this program to handle uppercase and special symbols.
  • 11. FIND NUMBER OF VOWELS, CONSONANTS, DIGITS AND WHITE SPACE CHARACTER Output
  • 12. REVERSE STRING To solve this problem, two standard library functions strlen() and st rcpy() are used to calculate length and to copy string respectively.
  • 13. CALCULATED LENGTH OF A STRING WITHOUTUSING STRLEN() FUNCTION You can use standard library function strlen( ) to find the length of a string but, this program computes the length of a string manually without using strlen( ) funtion.
  • 14. CONCATENATE TWO STRINGS MANUALLY You can concatenate two strings using standard library function strcat( ) , this program concatenates two strings manually without using strcat( ) function.
  • 15. COPY STRING MANUALLY You can use the strcpy( ) function to copy the content of one string to another but, this program copies the content of one string to another manually without using strcpy( ) function.
  • 16. REMOVE CHARACTERS IN STRING EXCEPT ALPHABETS This program takes a string from user and for loop executed until all characters of string is checked. If any character inside a string is not a alphabet, all characters after it including null character is shifted by 1 position backwards.
  • 17. SORT A STRING IN ALPHABETIC ORDER C program to sort a string in alphabetic order: For example if user will enter a string "programming" then output will be "aggimmnoprr" or output string will contain characters in alphabetical order.
  • 18. SORT ELEMENTS IN LEXICOGRAPHICAL ORDER (DICTIONARY ORDER) This program takes 10 words from user and sorts elements in lexicographical order. To perform this task, two dimensional string is used.
  • 19. C LIBRARY FUNCTIONS  C supports a wide range of functions that manipulate null- terminated strings:
  • 20. Following example makes use of few of the above-mentioned functions:
  • 21. STRCAT( ) FUNCTION  strcat( ) function concatenates two given strings. It concatenates source string at the end of destination string.  Syntax for strcat( ) function is given below. char * strcat ( char * destination, const char * source );  Example :  strcat ( str2, str1 ); - str1 is concatenated at the end of str2. strcat ( str1, str2 ); - str2 is concatenated at the end of str1.  As you know, each string in C is ended up with null character (‘0′).  In strcat( ) 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 strcat( ) operation.
  • 22. EXAMPLE PROGRAM FOR STRCAT( )  In this program, two strings “is fun” and “C tutorial” are concatenated using strcat( ) function and result is displayed as “C tutorial is fun”. Output: Source string Target string = is fun = C tutorial Target string after strcat( ) = C tutorial is fun
  • 23. Thanks for your time and attention!