SlideShare a Scribd company logo
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

PPTX
Presentation more c_programmingcharacter_and_string_handling_
PDF
SPL 13 | Character Array(String) in C
PPTX
Mikhail Khristophorov "Introduction to Regular Expressions"
PPT
PPTX
Handling of character strings C programming
PPTX
Strings in C language
PPTX
String in c programming
PDF
Strinng Classes in c++
Presentation more c_programmingcharacter_and_string_handling_
SPL 13 | Character Array(String) in C
Mikhail Khristophorov "Introduction to Regular Expressions"
Handling of character strings C programming
Strings in C language
String in c programming
Strinng Classes in c++

What's hot (20)

DOC
String in c
PPT
Strings in c
PPTX
String function in my sql
PPT
Strings
PDF
The Ring programming language version 1.5.3 book - Part 24 of 184
PDF
String.ppt
PPT
Rubymacros
PDF
05 c++-strings
PPTX
Strings in Python
PPTX
Strings in C
PDF
The Ring programming language version 1.9 book - Part 31 of 210
PPTX
Formal methods 3 - languages and machines
PPT
strings
PPT
Strings
PDF
Character Array and String
PDF
The Ring programming language version 1.6 book - Part 26 of 189
PPT
Operation on string presentation
PDF
String
PPTX
The string class
String in c
Strings in c
String function in my sql
Strings
The Ring programming language version 1.5.3 book - Part 24 of 184
String.ppt
Rubymacros
05 c++-strings
Strings in Python
Strings in C
The Ring programming language version 1.9 book - Part 31 of 210
Formal methods 3 - languages and machines
strings
Strings
Character Array and String
The Ring programming language version 1.6 book - Part 26 of 189
Operation on string presentation
String
The string class
Ad

Similar to Strings CPU GTU (20)

PDF
5 2. string processing
PPSX
Strings
PPT
CPSTRINGSARGAVISTRINGS.PPT
PPT
BHARGAVISTRINGS.PPT
PPTX
String (Computer programming and utilization)
PPTX
Slide -231, Math-1151, lecture-15,Chapter, Chapter 2.2,2.3, 5.1,5.2,5.6.pptx
PPT
CP-STRING.ppt
PPT
CP-STRING.ppt
PPT
CP-STRING (1).ppt
PPT
String & its application
PPT
THE FORMAT AND USAGE OF STRINGS IN C.PPT
PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
PPTX
Module-2_Strings concepts in c programming
PPTX
All_About_Strings_in_C_Programming.pptxn
PDF
Strings in c mrs.sowmya jyothi
PPT
String manipulation techniques like string compare copy
PPT
String and string manipulation
PPT
Strings
PPTX
UNIT 4C-Strings.pptx for c language and basic knowledge
5 2. string processing
Strings
CPSTRINGSARGAVISTRINGS.PPT
BHARGAVISTRINGS.PPT
String (Computer programming and utilization)
Slide -231, Math-1151, lecture-15,Chapter, Chapter 2.2,2.3, 5.1,5.2,5.6.pptx
CP-STRING.ppt
CP-STRING.ppt
CP-STRING (1).ppt
String & its application
THE FORMAT AND USAGE OF STRINGS IN C.PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
Chapterabcdefghijklmnopqrdstuvwxydanniipo
Module-2_Strings concepts in c programming
All_About_Strings_in_C_Programming.pptxn
Strings in c mrs.sowmya jyothi
String manipulation techniques like string compare copy
String and string manipulation
Strings
UNIT 4C-Strings.pptx for c language and basic knowledge
Ad

More from Maharshi Dave (9)

PPTX
Geographical information system
PPTX
Types of loads
PPT
TYPES OF PILE FOUNDATION & APPLICATIONS
PPTX
Application of Laplace Transforme
PPTX
Reality show
PPTX
Introduction to environment,ecology and ecosystem
PPTX
Air compressor
PPTX
Application of partial derivatives
PPTX
Ece impacts of infrastructural development on economy of india
Geographical information system
Types of loads
TYPES OF PILE FOUNDATION & APPLICATIONS
Application of Laplace Transforme
Reality show
Introduction to environment,ecology and ecosystem
Air compressor
Application of partial derivatives
Ece impacts of infrastructural development on economy of india

Recently uploaded (20)

PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Sustainable Sites - Green Building Construction
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPT
Mechanical Engineering MATERIALS Selection
PPTX
Geodesy 1.pptx...............................................
PPTX
OOP with Java - Java Introduction (Basics)
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PDF
Well-logging-methods_new................
PDF
Digital Logic Computer Design lecture notes
PPTX
Construction Project Organization Group 2.pptx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PDF
PPT on Performance Review to get promotions
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Sustainable Sites - Green Building Construction
UNIT-1 - COAL BASED THERMAL POWER PLANTS
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Mechanical Engineering MATERIALS Selection
Geodesy 1.pptx...............................................
OOP with Java - Java Introduction (Basics)
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Internet of Things (IOT) - A guide to understanding
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
Well-logging-methods_new................
Digital Logic Computer Design lecture notes
Construction Project Organization Group 2.pptx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPT on Performance Review to get promotions

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