C string

University of Potsdam
University of PotsdamIT en University of Potsdam
C
STRING
C - STRING
 strings are arrays of chars. String literals are words surrounded
by double quotation marks.
 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."
"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 WITHOUT USING 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 = is fun
Target string = C tutorial
Target string after strcat( ) = C tutorial is fun
1 de 22

Recomendados

Strings Functions in C Programming por
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C ProgrammingDevoAjit Gupta
3.2K vistas10 diapositivas
File handling in c por
File handling in cFile handling in c
File handling in cDavid Livingston J
28.3K vistas34 diapositivas
Functions in c por
Functions in cFunctions in c
Functions in csunila tharagaturi
1.5K vistas42 diapositivas
Strings por
StringsStrings
StringsMitali Chugh
5.6K vistas24 diapositivas
Function in C program por
Function in C programFunction in C program
Function in C programNurul Zakiah Zamri Tan
70.4K vistas23 diapositivas
Character Array and String por
Character Array and StringCharacter Array and String
Character Array and StringTasnima Hamid
933 vistas27 diapositivas

Más contenido relacionado

La actualidad más candente

structure and union por
structure and unionstructure and union
structure and unionstudent
15.3K vistas41 diapositivas
Function in c por
Function in cFunction in c
Function in cRaj Tandukar
6.8K vistas22 diapositivas
Strings in C por
Strings in CStrings in C
Strings in CKamal Acharya
24K vistas22 diapositivas
String in c por
String in cString in c
String in cSuneel Dogra
8.4K vistas4 diapositivas
C programming - String por
C programming - StringC programming - String
C programming - StringAchyut Devkota
6.1K vistas31 diapositivas
Strings in c por
Strings in cStrings in c
Strings in cvampugani
2.4K vistas29 diapositivas

La actualidad más candente(20)

structure and union por student
structure and unionstructure and union
structure and union
student15.3K vistas
Strings in c por vampugani
Strings in cStrings in c
Strings in c
vampugani2.4K vistas
Strings IN C por yndaravind
Strings IN CStrings IN C
Strings IN C
yndaravind254 vistas
User defined functions in C por Harendra Singh
User defined functions in CUser defined functions in C
User defined functions in C
Harendra Singh17.1K vistas
String in c programming por Devan Thakur
String in c programmingString in c programming
String in c programming
Devan Thakur5.9K vistas
Arrays in c por vampugani
Arrays in cArrays in c
Arrays in c
vampugani1K vistas
RECURSION IN C por v_jk
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk14K vistas

Destacado

C programming string por
C  programming stringC  programming string
C programming stringargusacademy
1.7K vistas11 diapositivas
Strings por
StringsStrings
StringsMichael Gordon
1.3K vistas7 diapositivas
Strings por
StringsStrings
StringsNilesh Dalvi
6.6K vistas27 diapositivas
String functions in C por
String functions in CString functions in C
String functions in Cbaabtra.com - No. 1 supplier of quality freshers
13.7K vistas15 diapositivas
Implementation of c string functions por
Implementation of c string functionsImplementation of c string functions
Implementation of c string functionsmohamed sikander
3.9K vistas13 diapositivas
Computer Programming- Lecture 5 por
Computer Programming- Lecture 5 Computer Programming- Lecture 5
Computer Programming- Lecture 5 Dr. Md. Shohel Sayeed
32.2K vistas61 diapositivas

Destacado(20)

C programming string por argusacademy
C  programming stringC  programming string
C programming string
argusacademy1.7K vistas
Implementation of c string functions por mohamed sikander
Implementation of c string functionsImplementation of c string functions
Implementation of c string functions
mohamed sikander3.9K vistas
Array in c language por home
Array in c languageArray in c language
Array in c language
home42K vistas
String in programming language in c or c++ por Samsil Arefin
 String in programming language  in c or c++  String in programming language  in c or c++
String in programming language in c or c++
Samsil Arefin2.3K vistas
String functions and operations por Mudasir Syed
String functions and operations String functions and operations
String functions and operations
Mudasir Syed1K vistas
Web Project Presentation - JoinPakForces por Wasif Altaf
Web Project Presentation - JoinPakForcesWeb Project Presentation - JoinPakForces
Web Project Presentation - JoinPakForces
Wasif Altaf782 vistas
C++ Preprocessor Directives por Wasif Altaf
C++ Preprocessor DirectivesC++ Preprocessor Directives
C++ Preprocessor Directives
Wasif Altaf1.5K vistas
Union in C programming por Kamal Acharya
Union in C programmingUnion in C programming
Union in C programming
Kamal Acharya6.3K vistas
C programming session 01 por Dushmanta Nath
C programming session 01C programming session 01
C programming session 01
Dushmanta Nath3.5K vistas

Similar a C string

string in C por
string in Cstring in C
string in CCGC Technical campus,Mohali
614 vistas23 diapositivas
STRINGS IN C MRS.SOWMYA JYOTHI.pdf por
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSowmyaJyothi3
314 vistas24 diapositivas
Strings in c mrs.sowmya jyothi por
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
151 vistas24 diapositivas
String in programming language in c or c++ por
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++Azeemaj101
91 vistas37 diapositivas
String notes por
String notesString notes
String notesPrasadu Peddi
116 vistas8 diapositivas
Lecture 05 2017 por
Lecture 05 2017Lecture 05 2017
Lecture 05 2017Jesmin Akhter
114 vistas27 diapositivas

Similar a C string(20)

STRINGS IN C MRS.SOWMYA JYOTHI.pdf por SowmyaJyothi3
STRINGS IN C MRS.SOWMYA JYOTHI.pdfSTRINGS IN C MRS.SOWMYA JYOTHI.pdf
STRINGS IN C MRS.SOWMYA JYOTHI.pdf
SowmyaJyothi3314 vistas
Strings in c mrs.sowmya jyothi por Sowmya Jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
Sowmya Jyothi151 vistas
String in programming language in c or c++ por Azeemaj101
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++
Azeemaj10191 vistas
C - String ppt por Md Razib
C - String ppt C - String ppt
C - String ppt
Md Razib86 vistas
String & its application por Tech_MX
String & its applicationString & its application
String & its application
Tech_MX5.5K vistas
M C6java7 por mbruggen
M C6java7M C6java7
M C6java7
mbruggen487 vistas

Más de University of Potsdam

Computer fundamentals 01 por
Computer fundamentals 01Computer fundamentals 01
Computer fundamentals 01University of Potsdam
1.2K vistas20 diapositivas
Workshop on android apps development por
Workshop on android apps developmentWorkshop on android apps development
Workshop on android apps developmentUniversity of Potsdam
436 vistas32 diapositivas
Transparency and concurrency por
Transparency and concurrencyTransparency and concurrency
Transparency and concurrencyUniversity of Potsdam
2.5K vistas55 diapositivas
Database System Architecture por
Database System ArchitectureDatabase System Architecture
Database System ArchitectureUniversity of Potsdam
1.1K vistas85 diapositivas
Functional dependency and normalization por
Functional dependency and normalizationFunctional dependency and normalization
Functional dependency and normalizationUniversity of Potsdam
1.9K vistas55 diapositivas
indexing and hashing por
indexing and hashingindexing and hashing
indexing and hashingUniversity of Potsdam
2.5K vistas53 diapositivas

Más de University of Potsdam(20)

Último

AUDIENCE - BANDURA.pptx por
AUDIENCE - BANDURA.pptxAUDIENCE - BANDURA.pptx
AUDIENCE - BANDURA.pptxiammrhaywood
84 vistas44 diapositivas
AI Tools for Business and Startups por
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and StartupsSvetlin Nakov
107 vistas39 diapositivas
11.28.23 Social Capital and Social Exclusion.pptx por
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptxmary850239
298 vistas25 diapositivas
Create a Structure in VBNet.pptx por
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptxBreach_P
75 vistas8 diapositivas
GSoC 2024 por
GSoC 2024GSoC 2024
GSoC 2024DeveloperStudentClub10
79 vistas15 diapositivas
UWP OA Week Presentation (1).pptx por
UWP OA Week Presentation (1).pptxUWP OA Week Presentation (1).pptx
UWP OA Week Presentation (1).pptxJisc
88 vistas11 diapositivas

Último(20)

AUDIENCE - BANDURA.pptx por iammrhaywood
AUDIENCE - BANDURA.pptxAUDIENCE - BANDURA.pptx
AUDIENCE - BANDURA.pptx
iammrhaywood84 vistas
AI Tools for Business and Startups por Svetlin Nakov
AI Tools for Business and StartupsAI Tools for Business and Startups
AI Tools for Business and Startups
Svetlin Nakov107 vistas
11.28.23 Social Capital and Social Exclusion.pptx por mary850239
11.28.23 Social Capital and Social Exclusion.pptx11.28.23 Social Capital and Social Exclusion.pptx
11.28.23 Social Capital and Social Exclusion.pptx
mary850239298 vistas
Create a Structure in VBNet.pptx por Breach_P
Create a Structure in VBNet.pptxCreate a Structure in VBNet.pptx
Create a Structure in VBNet.pptx
Breach_P75 vistas
UWP OA Week Presentation (1).pptx por Jisc
UWP OA Week Presentation (1).pptxUWP OA Week Presentation (1).pptx
UWP OA Week Presentation (1).pptx
Jisc88 vistas
Sociology KS5 por WestHatch
Sociology KS5Sociology KS5
Sociology KS5
WestHatch70 vistas
Drama KS5 Breakdown por WestHatch
Drama KS5 BreakdownDrama KS5 Breakdown
Drama KS5 Breakdown
WestHatch79 vistas
Psychology KS5 por WestHatch
Psychology KS5Psychology KS5
Psychology KS5
WestHatch93 vistas
Classification of crude drugs.pptx por GayatriPatra14
Classification of crude drugs.pptxClassification of crude drugs.pptx
Classification of crude drugs.pptx
GayatriPatra1486 vistas
Structure and Functions of Cell.pdf por Nithya Murugan
Structure and Functions of Cell.pdfStructure and Functions of Cell.pdf
Structure and Functions of Cell.pdf
Nithya Murugan545 vistas
Scope of Biochemistry.pptx por shoba shoba
Scope of Biochemistry.pptxScope of Biochemistry.pptx
Scope of Biochemistry.pptx
shoba shoba133 vistas
Solar System and Galaxies.pptx por DrHafizKosar
Solar System and Galaxies.pptxSolar System and Galaxies.pptx
Solar System and Galaxies.pptx
DrHafizKosar91 vistas
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx por ISSIP
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptxEIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
EIT-Digital_Spohrer_AI_Intro 20231128 v1.pptx
ISSIP369 vistas
The Open Access Community Framework (OACF) 2023 (1).pptx por Jisc
The Open Access Community Framework (OACF) 2023 (1).pptxThe Open Access Community Framework (OACF) 2023 (1).pptx
The Open Access Community Framework (OACF) 2023 (1).pptx
Jisc110 vistas
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively por PECB
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks EffectivelyISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
ISO/IEC 27001 and ISO/IEC 27005: Managing AI Risks Effectively
PECB 585 vistas
When Sex Gets Complicated: Porn, Affairs, & Cybersex por Marlene Maheu
When Sex Gets Complicated: Porn, Affairs, & CybersexWhen Sex Gets Complicated: Porn, Affairs, & Cybersex
When Sex Gets Complicated: Porn, Affairs, & Cybersex
Marlene Maheu67 vistas

C string

  • 2. C - STRING  strings are arrays of chars. String literals are words surrounded by double quotation marks.  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." "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 WITHOUT USING 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 = is fun Target string = C tutorial Target string after strcat( ) = C tutorial is fun