SlideShare a Scribd company logo
1 of 15
STRINGS
Subject :Computer Programming and Utilization(
Active Learning Assignment(ALA)
Strings
• String:- A sequence of characters.
• The ending character is always null character ‘0’.
• Strings are important in many programming.
Examples:-
“The cow jump over the moon.”
“123456” etc.
Declaration of Strings
• Syntax: char string_name[length];
• Example: char name[5];
• Since string is an array, the declaration of a string is the same as declaring a char array.
char string_var[30];
char string_var[20] = “Initial value”;
"A String" A 0gnirtS
Memory Storage for a String
• The string is always ended with a null
character ‘0’.
• The characters after the null character are
ignored.
• e.g., char str[20] = “Initial value”;
n i t i a l v a l u e ? ? …I 0
[0] [13]
Input/Output of a String
• The placeholder %s is used to represent string
arguments in printf and scanf.
– printf(“Topic: %sn”, string_var);
• The string can be right-justified by placing a
positive number in the placeholder.
– printf(“%8s”, str);
• The string can be left-justified by placing a
negative number in the placeholder.
– printf(“%-8s”, str);
Various Operations Possible on Strings
1. To find length of string
2. To copy content of one string to another string
3. To change the case of letters in the strings
4. To do encryption and decryption operation with string
5. To find out tokens from the string
6. To do some string manipulation and rearranging the
strings
7. To search match
Some String Functions from string.h
Function Purpose Example
strcpy Makes a copy of a
string
strcpy(s1, “Hi”);
strcat Appends a string to the
end of another string
strcat(s1, “more”);
strcmp Compare two strings
alphabetically
strcmp(s1, “Hu”);
strlen Returns the number of
characters in a string
strlen(“Hi”)
returns 2.
strrev Returns the reverse
string of string.
strrev(“Hello”);
Returns olleH.
Function strcat
• Functions strcat appends a copy of string s2 to the end of s1 and terminate s1 with null
and returns s1.
• Example:-
#include<stdio.h>
#include<string.h>
main()
{
char a[100],b[100];
printf("enter the 1st string");
scanf("%s",a);
printf("enter the 2nd string");
scanf("%s",b);
strcat(a,b);
printf("string obtained is %sn",a);
}
Output:-
enter the 1st string Hello
enter the 2nd string World
string obtained is Hello World
Function strcpy
• Function strcpy copies one string into another string.
• Example:-
#include<stdio.h>
#include<string.h>
main()
{
char s1[15],s2[15];
printf("enter s1");
scanf("[^n]",s1);
strcpy(s2,s1);
printf("n copied s2 is %s",s2);
}
• Output:-
enter s1 Hello
copied s2 is Hello
• Write a program to accept the string and find out its length using string.
#include<stdio.h>
#include<string.h>
void main()
{
char s[80];
int i,tn;
printf(“Enter a string:”);
scanf(“%s”,&s);
tn=strlen(s);
printf(“nLength of %s is %d”,s,tn);
}
Output:-
Enter a string:Computer
Length of Computer is 8
Distinction Between Characters and
Strings
• The representation of a char (e.g., ‘Q’) and a
string (e.g., “Q”) is essentially different.
–A string is an array of characters ended with
the null character.
Q
Character ‘Q’
Q 0
String “Q”
String Comparison (1/2)
• Suppose there are two strings, str1 and str2.
– The condition str1 < str2 compare the initial memory address
of str1 and of str2.
• The comparison between two strings is done by comparing each
corresponding character in them.
– The characters are comapared against the ASCII table.
– “thrill” < “throw” since ‘i’ < ‘o’;
– “joy” < joyous“;
• The standard string comparison uses the strcmp and strncmp
functions.
String Comparison (2/2)
Relationship Returned Value Example
str1 < str2 Negative “Hello”< “Hi”
str1 = str2 0 “Hi” = “Hi”
str1 > str2 Positive “Hi” > “Hello”
• e.g., we can check if two strings are the same by
if(strcmp(str1, str2) != 0)
printf(“The two strings are different!”);
Array of Strings
• Table of strings is called as Array of strings.
Example:
char name[3][10]={“Sharda”,”Neelima”,”Geeta”};
//(Array of strings)Two dimensional array of strings
• Name is an array of 3 strings;each containing 10 characters.The total storage
for name requires 30 bytes as below.
S h a r d a 0
N e e l i m a 0
G e e t a 0
Strings CPU GTU

More Related Content

What's hot

Strings in c
Strings in cStrings in c
Strings in cvampugani
 
String function in my sql
String function in my sqlString function in my sql
String function in my sqlknowledgemart
 
The Ring programming language version 1.5.3 book - Part 24 of 184
The Ring programming language version 1.5.3 book - Part 24 of 184The Ring programming language version 1.5.3 book - Part 24 of 184
The Ring programming language version 1.5.3 book - Part 24 of 184Mahmoud Samir Fayed
 
The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.9 book - Part 31 of 210The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.9 book - Part 31 of 210Mahmoud Samir Fayed
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machinesVlad Patryshev
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and StringTasnima Hamid
 
The Ring programming language version 1.6 book - Part 26 of 189
The Ring programming language version 1.6 book - Part 26 of 189The Ring programming language version 1.6 book - Part 26 of 189
The Ring programming language version 1.6 book - Part 26 of 189Mahmoud Samir Fayed
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentationAliul Kadir Akib
 

What's hot (20)

String in c
String in cString in c
String in c
 
Strings in c
Strings in cStrings in c
Strings in c
 
String function in my sql
String function in my sqlString function in my sql
String function in my sql
 
Strings
StringsStrings
Strings
 
The Ring programming language version 1.5.3 book - Part 24 of 184
The Ring programming language version 1.5.3 book - Part 24 of 184The Ring programming language version 1.5.3 book - Part 24 of 184
The Ring programming language version 1.5.3 book - Part 24 of 184
 
String.ppt
String.pptString.ppt
String.ppt
 
Rubymacros
RubymacrosRubymacros
Rubymacros
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Strings in C
Strings in CStrings in C
Strings in C
 
The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.9 book - Part 31 of 210The Ring programming language version 1.9 book - Part 31 of 210
The Ring programming language version 1.9 book - Part 31 of 210
 
Formal methods 3 - languages and machines
Formal methods   3 - languages and machinesFormal methods   3 - languages and machines
Formal methods 3 - languages and machines
 
strings
stringsstrings
strings
 
Strings
StringsStrings
Strings
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
The Ring programming language version 1.6 book - Part 26 of 189
The Ring programming language version 1.6 book - Part 26 of 189The Ring programming language version 1.6 book - Part 26 of 189
The Ring programming language version 1.6 book - Part 26 of 189
 
Operation on string presentation
Operation on string presentationOperation on string presentation
Operation on string presentation
 
String
StringString
String
 
Team 1
Team 1Team 1
Team 1
 
The string class
The string classThe string class
The string class
 

Similar to Strings CPU GTU

5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTSasideepa
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTSasideepa
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)Digvijaysinh Gohil
 
String & its application
String & its applicationString & its application
String & its applicationTech_MX
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiSowmya Jyothi
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulationShahjahan Samoon
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxAbhimanyuChaure
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation xShahjahan Samoon
 
String in programming language in c or c++
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
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxJStalinAsstProfessor
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In CFazila Sadia
 

Similar to Strings CPU GTU (20)

5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
Strings
StringsStrings
Strings
 
CPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPTCPSTRINGSARGAVISTRINGS.PPT
CPSTRINGSARGAVISTRINGS.PPT
 
BHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPTBHARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
 
String (Computer programming and utilization)
String (Computer programming and utilization)String (Computer programming and utilization)
String (Computer programming and utilization)
 
CP-STRING (1).ppt
CP-STRING (1).pptCP-STRING (1).ppt
CP-STRING (1).ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
CP-STRING.ppt
CP-STRING.pptCP-STRING.ppt
CP-STRING.ppt
 
String & its application
String & its applicationString & its application
String & its application
 
Strings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothiStrings in c mrs.sowmya jyothi
Strings in c mrs.sowmya jyothi
 
String and string manipulation
String and string manipulationString and string manipulation
String and string manipulation
 
Strings
StringsStrings
Strings
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
Strings(2007)
Strings(2007)Strings(2007)
Strings(2007)
 
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptxINDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
INDIAN INSTITUTE OF TECHNOLOGY KANPURESC 111M Lec13.pptx
 
String and string manipulation x
String and string manipulation xString and string manipulation x
String and string manipulation x
 
String in programming language in c or c++
String in programming language in c or c++String in programming language in c or c++
String in programming language in c or c++
 
[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++[ITP - Lecture 17] Strings in C/C++
[ITP - Lecture 17] Strings in C/C++
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
Implementation Of String Functions In C
Implementation Of String Functions In CImplementation Of String Functions In C
Implementation Of String Functions In C
 

More from Maharshi Dave

Geographical information system
Geographical information system Geographical information system
Geographical information system Maharshi Dave
 
TYPES OF PILE FOUNDATION & APPLICATIONS
TYPES OF PILE FOUNDATION & APPLICATIONSTYPES OF PILE FOUNDATION & APPLICATIONS
TYPES OF PILE FOUNDATION & APPLICATIONSMaharshi Dave
 
Application of Laplace Transforme
Application of Laplace TransformeApplication of Laplace Transforme
Application of Laplace TransformeMaharshi Dave
 
Introduction to environment,ecology and ecosystem
Introduction to environment,ecology and ecosystemIntroduction to environment,ecology and ecosystem
Introduction to environment,ecology and ecosystemMaharshi Dave
 
Application of partial derivatives
Application of partial derivativesApplication of partial derivatives
Application of partial derivativesMaharshi Dave
 
Ece impacts of infrastructural development on economy of india
Ece impacts  of infrastructural development on economy of india Ece impacts  of infrastructural development on economy of india
Ece impacts of infrastructural development on economy of india Maharshi Dave
 

More from Maharshi Dave (9)

Geographical information system
Geographical information system Geographical information system
Geographical information system
 
Types of loads
Types of loadsTypes of loads
Types of loads
 
TYPES OF PILE FOUNDATION & APPLICATIONS
TYPES OF PILE FOUNDATION & APPLICATIONSTYPES OF PILE FOUNDATION & APPLICATIONS
TYPES OF PILE FOUNDATION & APPLICATIONS
 
Application of Laplace Transforme
Application of Laplace TransformeApplication of Laplace Transforme
Application of Laplace Transforme
 
Reality show
Reality showReality show
Reality show
 
Introduction to environment,ecology and ecosystem
Introduction to environment,ecology and ecosystemIntroduction to environment,ecology and ecosystem
Introduction to environment,ecology and ecosystem
 
Air compressor
Air compressorAir compressor
Air compressor
 
Application of partial derivatives
Application of partial derivativesApplication of partial derivatives
Application of partial derivatives
 
Ece impacts of infrastructural development on economy of india
Ece impacts  of infrastructural development on economy of india Ece impacts  of infrastructural development on economy of india
Ece impacts of infrastructural development on economy of india
 

Recently uploaded

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCall Girls in Nagpur High Profile
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfAsst.prof M.Gokilavani
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024Mark Billinghurst
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...srsj9000
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZTE
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort servicejennyeacort
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and usesDevarapalliHaritha
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxDeepakSakkari2
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130Suhani Kapoor
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxpurnimasatapathy1234
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service NashikCollege Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
College Call Girls Nashik Nehal 7001305949 Independent Escort Service Nashik
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdfCCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
CCS355 Neural Network & Deep Learning Unit II Notes with Question bank .pdf
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024IVE Industry Focused Event - Defence Sector 2024
IVE Industry Focused Event - Defence Sector 2024
 
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
Gfe Mayur Vihar Call Girls Service WhatsApp -> 9999965857 Available 24x7 ^ De...
 
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
ZXCTN 5804 / ZTE PTN / ZTE POTN / ZTE 5804 PTN / ZTE POTN 5804 ( 100/200 GE Z...
 
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort serviceGurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
Gurgaon ✡️9711147426✨Call In girls Gurgaon Sector 51 escort service
 
power system scada applications and uses
power system scada applications and usespower system scada applications and uses
power system scada applications and uses
 
Biology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptxBiology for Computer Engineers Course Handout.pptx
Biology for Computer Engineers Course Handout.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
VIP Call Girls Service Hitech City Hyderabad Call +91-8250192130
 
Microscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptxMicroscopic Analysis of Ceramic Materials.pptx
Microscopic Analysis of Ceramic Materials.pptx
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
VICTOR MAESTRE RAMIREZ - Planetary Defender on NASA's Double Asteroid Redirec...
 

Strings CPU GTU

  • 1. STRINGS Subject :Computer Programming and Utilization( Active Learning Assignment(ALA)
  • 2. Strings • String:- A sequence of characters. • The ending character is always null character ‘0’. • Strings are important in many programming. Examples:- “The cow jump over the moon.” “123456” etc.
  • 3. Declaration of Strings • Syntax: char string_name[length]; • Example: char name[5]; • Since string is an array, the declaration of a string is the same as declaring a char array. char string_var[30]; char string_var[20] = “Initial value”; "A String" A 0gnirtS
  • 4. Memory Storage for a String • The string is always ended with a null character ‘0’. • The characters after the null character are ignored. • e.g., char str[20] = “Initial value”; n i t i a l v a l u e ? ? …I 0 [0] [13]
  • 5. Input/Output of a String • The placeholder %s is used to represent string arguments in printf and scanf. – printf(“Topic: %sn”, string_var); • The string can be right-justified by placing a positive number in the placeholder. – printf(“%8s”, str); • The string can be left-justified by placing a negative number in the placeholder. – printf(“%-8s”, str);
  • 6. Various Operations Possible on Strings 1. To find length of string 2. To copy content of one string to another string 3. To change the case of letters in the strings 4. To do encryption and decryption operation with string 5. To find out tokens from the string 6. To do some string manipulation and rearranging the strings 7. To search match
  • 7. Some String Functions from string.h Function Purpose Example strcpy Makes a copy of a string strcpy(s1, “Hi”); strcat Appends a string to the end of another string strcat(s1, “more”); strcmp Compare two strings alphabetically strcmp(s1, “Hu”); strlen Returns the number of characters in a string strlen(“Hi”) returns 2. strrev Returns the reverse string of string. strrev(“Hello”); Returns olleH.
  • 8. Function strcat • Functions strcat appends a copy of string s2 to the end of s1 and terminate s1 with null and returns s1. • Example:- #include<stdio.h> #include<string.h> main() { char a[100],b[100]; printf("enter the 1st string"); scanf("%s",a); printf("enter the 2nd string"); scanf("%s",b); strcat(a,b); printf("string obtained is %sn",a); } Output:- enter the 1st string Hello enter the 2nd string World string obtained is Hello World
  • 9. Function strcpy • Function strcpy copies one string into another string. • Example:- #include<stdio.h> #include<string.h> main() { char s1[15],s2[15]; printf("enter s1"); scanf("[^n]",s1); strcpy(s2,s1); printf("n copied s2 is %s",s2); } • Output:- enter s1 Hello copied s2 is Hello
  • 10. • Write a program to accept the string and find out its length using string. #include<stdio.h> #include<string.h> void main() { char s[80]; int i,tn; printf(“Enter a string:”); scanf(“%s”,&s); tn=strlen(s); printf(“nLength of %s is %d”,s,tn); } Output:- Enter a string:Computer Length of Computer is 8
  • 11. Distinction Between Characters and Strings • The representation of a char (e.g., ‘Q’) and a string (e.g., “Q”) is essentially different. –A string is an array of characters ended with the null character. Q Character ‘Q’ Q 0 String “Q”
  • 12. String Comparison (1/2) • Suppose there are two strings, str1 and str2. – The condition str1 < str2 compare the initial memory address of str1 and of str2. • The comparison between two strings is done by comparing each corresponding character in them. – The characters are comapared against the ASCII table. – “thrill” < “throw” since ‘i’ < ‘o’; – “joy” < joyous“; • The standard string comparison uses the strcmp and strncmp functions.
  • 13. String Comparison (2/2) Relationship Returned Value Example str1 < str2 Negative “Hello”< “Hi” str1 = str2 0 “Hi” = “Hi” str1 > str2 Positive “Hi” > “Hello” • e.g., we can check if two strings are the same by if(strcmp(str1, str2) != 0) printf(“The two strings are different!”);
  • 14. Array of Strings • Table of strings is called as Array of strings. Example: char name[3][10]={“Sharda”,”Neelima”,”Geeta”}; //(Array of strings)Two dimensional array of strings • Name is an array of 3 strings;each containing 10 characters.The total storage for name requires 30 bytes as below. S h a r d a 0 N e e l i m a 0 G e e t a 0