SlideShare une entreprise Scribd logo
1  sur  29
Strings
• A special kind of array is an array of characters
ending in the null character 0 called string array
s
• A string is declared as an array of characters
• char s[10]
• char p[30]
• When declaring a string don’t forget to leave a s
pace for the null character which is also known a
s the string terminator character
C offers four main operations on str
ings
• strcpy - copy one string into another
• strcat - append one string onto the right si
de of the other
• strcmp – compare alphabetic order of two
strings
• strlen – return the length of a string
strcpy
• strcpy(destinationstring, sourcestring)
• Copies sourcestring into destinationstring
• For example
• strcpy(str, “hello world”); assigns “hello wo
rld” to the string str
Example with strcpy
#include <stdio.h>
#include <string.h>
main()
{
char x[] = “Example with strcpy”;
char y[25];
printf(“The string in array x is %s n “, x);
strcpy(y,x);
printf(“The string in array y is %s n “, y);
}
strcat
• strcat(destinationstring, sourcestring)
• appends sourcestring to right hand side of destin
ationstring
• For example if str had value “a big ”
• strcat(str, “hello world”); appends “hello world” to
the string “a big ” to get
• “ a big hello world”
Example with strcat
#include <stdio.h>
#include <string.h>
main()
{
char x[] = “Example with strcat”;
char y[]= “which stands for string concatenation”;
printf(“The string in array x is %s n “, x);
strcat(x,y);
printf(“The string in array x is %s n “, x);
}
strcmp
• strcmp(stringa, stringb)
• Compares stringa and stringb alphabetically
• Returns a negative value if stringa precedes stri
ngb alphabetically
• Returns a positive value if stringb precedes strin
ga alphabetically
• Returns 0 if they are equal
• Note lowercase characters are greater than Upp
ercase
Example with strcmp
#include <stdio.h>
#include <string.h>
main()
{
char x[] = “cat”;
char y[]= “cat”;
char z[]= “dog”;
if (strcmp(x,y) == 0)
printf(“The string in array x %s is equal to t
hat in %s n “, x,y);
continued
if (strcmp(x,z) != 0)
{printf(“The string in array x %s is not equal to that in z %s n “,
x,z);
if (strcmp(x,z) < 0)
printf(“The string in array x %s precedes that in z %s n “, x,z);
else
printf(“The string in array z %s precedes that in x %s n “, z,x);
}
else
printf( “they are equal”);
}
strlen
• strlen(str) returns length of string excluding
null character
• strlen(“tttt”) = 4 not 5 since 0 not counted
Example with strlen
#include <stdio.h>
#include <string.h>
main()
{
int i, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if (x[i] == ‘t’) count++;
}
printf(“The number of t’s in %s is %d n “, x,count);
}
Vowels Example with strlen
#include <stdio.h>
#include <string.h>
main()
{
int i, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if ((x[i] == ‘a’)||(x[i]==‘e’)||(x[i]==‘I’)||(x[i]==‘o’)||(x[i]==‘u’)) count+
+;
}
printf(“The number of vowels’s in %s is %d n “, x,count);
}
No of Words Example with strlen
#include <stdio.h>
#include <string.h>
main()
{
int i, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if ((x[i] == ‘ ‘) count++;
}
printf(“The number of words’s in %s is %d n “, x,count+1);
}
No of Words Example with more th
an one space between words
#include <stdio.h>
#include <string.h>
main()
{
int i,j, count;
char x[] = “tommy tucket took a tiny ticket ”;
count = 0;
for (i = 0; i < strlen(x);i++)
{
if ((x[i] == ‘ ‘)
{ count++;
for(j=i;x[j] != ‘ ‘;j++);
i = j;
}
}
printf(“The number of words’s in %s is %d n “, x,count+1);
}
Input output functions of characters
and strings
• getchar() reads a character from the scree
n in a non-interactive environment
• getche() like getchar() except interactive
• putchar(int ch) outputs a character to scre
en
• gets(str) gets a string from the keyboard
• puts(str) outputs string to screen
Characters are at the heart of string
s
Exercise 1
Output
1
1 2
1 2 3
1 2 3 4
………….
1 2 3 4 5 6 7 8 9 10
Exercise 1
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
for(i=1;i <= j;i++)
{
printf(“%d “,i);
}
printf(“n“);
}
}
Exercise 2
Output
*
* *
* * *
* * * *
…………….
* * * * * * * * * *
Exercise 2
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
for(i=1;i <= j;i++)
{
printf(“* “);
}
printf(“n“);
}
}
Exercise 3
• Output
***********
* *
* *
* *
* *
* *
* *
* *
* *
***********
#include <stdio.h>
main()
{
int i,j;
for(j = 1; j <= 10; j++)
{
printf(“* “);
for(i=1;i <= 8;i++)
{
if ((j==1) || (j==10)) printf(“* “);
else
printf(“ “);
}
printf(“* n “);
}
}
Some Useful C Character Functi
ons
• Don't forget to #include <ctype.h> to get t
he function prototypes.
Functions
• Function Return true if
• int isalpha(c); c is a letter.
• int isupper(c); c is an upper case
letter.
• int islower(c); c is a lower case letter.
• int isdigit(c); c is a digit [0-9].
More Functions
• Function Return true if
• int isxdigit(c); c is a hexadecimal digit
[0-9A-Fa-f].
• int isalnum(c); c is an alphanumeric character (c
is a letter or a digit);
• int isspace(c); c is a SPACE, TAB, RETURN,
NEWLINE, FORMFEED,
or vertical tab character.
Even More C Functions
• Function Return true if
• int ispunct(c); c is a punctuation
character (neither
control nor
alphanumeric).
• int isprint(c); c is a printing character.
• int iscntrl(c); c is a delete character
or ordinary control
character.
Still More C Functions
• Function Return true if
• int isascii(c); c is an ASCII character,
codeless than 0200.
• int toupper(int c); convert character c to
upper case (leave it
alone if not lower)
• int tolower(int c); convert character c to
lower case (leave it
alone if not upper)
• Program to Reverse Strings
• #include <stdio.h>
#include <string.h>
int main ()
{
• int i;
char a[10];
char temp;
//clrscr(); // only works on windows
gets(a);
• for (i = 0; a[i] != '0' ; i++);
• i--;
• for (int j = 0; j <= i/2 ; j++)
{
• temp = a[j];
a[j] = a[i - j];
a[i - j] = temp;
• }
printf("%s",a);
return(0);
•
Program to count the number of vo
wels in a string :
• Note Two different ways to declare strings
• One using pointers *str
• Two using character array char a[]
• #include <stdio.h>
#include <string.h>
• void main() {
• char *str;
• char a[]="aeiouAEIOU";
• int i,j,count=0;
• clrscr();
• printf("nEnter the stringn");
• gets(str);
• for(i=0;str[i]!='0';i++)
• {
• for(j=0;a[j]!='0';j++)
• if(a[j] == str[i]
• {
• count++;
• break;
• }
printf("nNo. of vowels = %d",count);
• }
•

Contenu connexe

Tendances (15)

C++ string
C++ stringC++ string
C++ string
 
String Handling in c++
String Handling in c++String Handling in c++
String Handling in c++
 
14 strings
14 strings14 strings
14 strings
 
Strings Functions in C Programming
Strings Functions in C ProgrammingStrings Functions in C Programming
Strings Functions in C Programming
 
String in c programming
String in c programmingString in c programming
String in c programming
 
P2 2017 python_strings
P2 2017 python_stringsP2 2017 python_strings
P2 2017 python_strings
 
05 c++-strings
05 c++-strings05 c++-strings
05 c++-strings
 
Strings in Python
Strings in PythonStrings in Python
Strings in Python
 
Arrays
ArraysArrays
Arrays
 
Strings in C language
Strings in C languageStrings in C language
Strings in C language
 
Strings
StringsStrings
Strings
 
Array and string
Array and stringArray and string
Array and string
 
2017 biological databasespart2
2017 biological databasespart22017 biological databasespart2
2017 biological databasespart2
 
Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)Passing an Array to a Function (ICT Programming)
Passing an Array to a Function (ICT Programming)
 
String C Programming
String C ProgrammingString C Programming
String C Programming
 

Similaire à Presentation more c_programmingcharacter_and_string_handling_

Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01Md. Ashikur Rahman
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programmingAppili Vamsi Krishna
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdfJoyPalit
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdfssusere19c741
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxJStalinAsstProfessor
 
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
 
Cs1123 9 strings
Cs1123 9 stringsCs1123 9 strings
Cs1123 9 stringsTAlha MAlik
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)teach4uin
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functionsAwinash Goswami
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programmingmikeymanjiro2090
 
SPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in CSPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in CMohammad Imam Hossain
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3karmuhtam
 

Similaire à Presentation more c_programmingcharacter_and_string_handling_ (20)

Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
 
Handling of character strings C programming
Handling of character strings C programmingHandling of character strings C programming
Handling of character strings C programming
 
Lecture14.pdf
Lecture14.pdfLecture14.pdf
Lecture14.pdf
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
 
The string class
The string classThe string class
The string class
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
fundamentals of c programming_String.pptx
fundamentals of c programming_String.pptxfundamentals of c programming_String.pptx
fundamentals of c programming_String.pptx
 
lecture5.ppt
lecture5.pptlecture5.ppt
lecture5.ppt
 
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
 
Strings
StringsStrings
Strings
 
Cs1123 9 strings
Cs1123 9 stringsCs1123 9 strings
Cs1123 9 strings
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
L13 string handling(string class)
L13 string handling(string class)L13 string handling(string class)
L13 string handling(string class)
 
Lecture 1 string functions
Lecture 1  string functionsLecture 1  string functions
Lecture 1 string functions
 
introduction to strings in c programming
introduction to strings in c programmingintroduction to strings in c programming
introduction to strings in c programming
 
SPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in CSPL 13 | Character Array(String) in C
SPL 13 | Character Array(String) in C
 
Unitii string
Unitii stringUnitii string
Unitii string
 
[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++
 
Strings
StringsStrings
Strings
 
Data structure week 3
Data structure week 3Data structure week 3
Data structure week 3
 

Plus de KarthicaMarasamy (13)

Roles of Datascience.pptx
Roles of Datascience.pptxRoles of Datascience.pptx
Roles of Datascience.pptx
 
DATASCIENCE.pptx
DATASCIENCE.pptxDATASCIENCE.pptx
DATASCIENCE.pptx
 
Software Testing 1.pptx
Software Testing 1.pptxSoftware Testing 1.pptx
Software Testing 1.pptx
 
powerpoint 1.pdf
powerpoint 1.pdfpowerpoint 1.pdf
powerpoint 1.pdf
 
class 3.pptx
class 3.pptxclass 3.pptx
class 3.pptx
 
class 2.pptx
class 2.pptxclass 2.pptx
class 2.pptx
 
Software Testing
Software TestingSoftware Testing
Software Testing
 
Network (Hub,switches)
Network  (Hub,switches)Network  (Hub,switches)
Network (Hub,switches)
 
Computer network layers
Computer network layersComputer network layers
Computer network layers
 
C programming
C programmingC programming
C programming
 
Fundamentals steps in Digital Image processing
Fundamentals steps in Digital Image processingFundamentals steps in Digital Image processing
Fundamentals steps in Digital Image processing
 
DIGITAL IMAGE PROCESSING
DIGITAL IMAGE PROCESSINGDIGITAL IMAGE PROCESSING
DIGITAL IMAGE PROCESSING
 
Network
NetworkNetwork
Network
 

Dernier

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfchloefrazer622
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...PsychoTech Services
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Dernier (20)

Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Disha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdfDisha NEET Physics Guide for classes 11 and 12.pdf
Disha NEET Physics Guide for classes 11 and 12.pdf
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
IGNOU MSCCFT and PGDCFT Exam Question Pattern: MCFT003 Counselling and Family...
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Presentation more c_programmingcharacter_and_string_handling_

  • 1. Strings • A special kind of array is an array of characters ending in the null character 0 called string array s • A string is declared as an array of characters • char s[10] • char p[30] • When declaring a string don’t forget to leave a s pace for the null character which is also known a s the string terminator character
  • 2. C offers four main operations on str ings • strcpy - copy one string into another • strcat - append one string onto the right si de of the other • strcmp – compare alphabetic order of two strings • strlen – return the length of a string
  • 3. strcpy • strcpy(destinationstring, sourcestring) • Copies sourcestring into destinationstring • For example • strcpy(str, “hello world”); assigns “hello wo rld” to the string str
  • 4. Example with strcpy #include <stdio.h> #include <string.h> main() { char x[] = “Example with strcpy”; char y[25]; printf(“The string in array x is %s n “, x); strcpy(y,x); printf(“The string in array y is %s n “, y); }
  • 5. strcat • strcat(destinationstring, sourcestring) • appends sourcestring to right hand side of destin ationstring • For example if str had value “a big ” • strcat(str, “hello world”); appends “hello world” to the string “a big ” to get • “ a big hello world”
  • 6. Example with strcat #include <stdio.h> #include <string.h> main() { char x[] = “Example with strcat”; char y[]= “which stands for string concatenation”; printf(“The string in array x is %s n “, x); strcat(x,y); printf(“The string in array x is %s n “, x); }
  • 7. strcmp • strcmp(stringa, stringb) • Compares stringa and stringb alphabetically • Returns a negative value if stringa precedes stri ngb alphabetically • Returns a positive value if stringb precedes strin ga alphabetically • Returns 0 if they are equal • Note lowercase characters are greater than Upp ercase
  • 8. Example with strcmp #include <stdio.h> #include <string.h> main() { char x[] = “cat”; char y[]= “cat”; char z[]= “dog”; if (strcmp(x,y) == 0) printf(“The string in array x %s is equal to t hat in %s n “, x,y);
  • 9. continued if (strcmp(x,z) != 0) {printf(“The string in array x %s is not equal to that in z %s n “, x,z); if (strcmp(x,z) < 0) printf(“The string in array x %s precedes that in z %s n “, x,z); else printf(“The string in array z %s precedes that in x %s n “, z,x); } else printf( “they are equal”); }
  • 10. strlen • strlen(str) returns length of string excluding null character • strlen(“tttt”) = 4 not 5 since 0 not counted
  • 11. Example with strlen #include <stdio.h> #include <string.h> main() { int i, count; char x[] = “tommy tucket took a tiny ticket ”; count = 0; for (i = 0; i < strlen(x);i++) { if (x[i] == ‘t’) count++; } printf(“The number of t’s in %s is %d n “, x,count); }
  • 12. Vowels Example with strlen #include <stdio.h> #include <string.h> main() { int i, count; char x[] = “tommy tucket took a tiny ticket ”; count = 0; for (i = 0; i < strlen(x);i++) { if ((x[i] == ‘a’)||(x[i]==‘e’)||(x[i]==‘I’)||(x[i]==‘o’)||(x[i]==‘u’)) count+ +; } printf(“The number of vowels’s in %s is %d n “, x,count); }
  • 13. No of Words Example with strlen #include <stdio.h> #include <string.h> main() { int i, count; char x[] = “tommy tucket took a tiny ticket ”; count = 0; for (i = 0; i < strlen(x);i++) { if ((x[i] == ‘ ‘) count++; } printf(“The number of words’s in %s is %d n “, x,count+1); }
  • 14. No of Words Example with more th an one space between words #include <stdio.h> #include <string.h> main() { int i,j, count; char x[] = “tommy tucket took a tiny ticket ”; count = 0; for (i = 0; i < strlen(x);i++) { if ((x[i] == ‘ ‘) { count++; for(j=i;x[j] != ‘ ‘;j++); i = j; } } printf(“The number of words’s in %s is %d n “, x,count+1); }
  • 15. Input output functions of characters and strings • getchar() reads a character from the scree n in a non-interactive environment • getche() like getchar() except interactive • putchar(int ch) outputs a character to scre en • gets(str) gets a string from the keyboard • puts(str) outputs string to screen
  • 16. Characters are at the heart of string s
  • 17. Exercise 1 Output 1 1 2 1 2 3 1 2 3 4 …………. 1 2 3 4 5 6 7 8 9 10
  • 18. Exercise 1 #include <stdio.h> main() { int i,j; for(j = 1; j <= 10; j++) { for(i=1;i <= j;i++) { printf(“%d “,i); } printf(“n“); } }
  • 19. Exercise 2 Output * * * * * * * * * * ……………. * * * * * * * * * *
  • 20. Exercise 2 #include <stdio.h> main() { int i,j; for(j = 1; j <= 10; j++) { for(i=1;i <= j;i++) { printf(“* “); } printf(“n“); } }
  • 21. Exercise 3 • Output *********** * * * * * * * * * * * * * * * * ***********
  • 22. #include <stdio.h> main() { int i,j; for(j = 1; j <= 10; j++) { printf(“* “); for(i=1;i <= 8;i++) { if ((j==1) || (j==10)) printf(“* “); else printf(“ “); } printf(“* n “); } }
  • 23. Some Useful C Character Functi ons • Don't forget to #include <ctype.h> to get t he function prototypes.
  • 24. Functions • Function Return true if • int isalpha(c); c is a letter. • int isupper(c); c is an upper case letter. • int islower(c); c is a lower case letter. • int isdigit(c); c is a digit [0-9].
  • 25. More Functions • Function Return true if • int isxdigit(c); c is a hexadecimal digit [0-9A-Fa-f]. • int isalnum(c); c is an alphanumeric character (c is a letter or a digit); • int isspace(c); c is a SPACE, TAB, RETURN, NEWLINE, FORMFEED, or vertical tab character.
  • 26. Even More C Functions • Function Return true if • int ispunct(c); c is a punctuation character (neither control nor alphanumeric). • int isprint(c); c is a printing character. • int iscntrl(c); c is a delete character or ordinary control character.
  • 27. Still More C Functions • Function Return true if • int isascii(c); c is an ASCII character, codeless than 0200. • int toupper(int c); convert character c to upper case (leave it alone if not lower) • int tolower(int c); convert character c to lower case (leave it alone if not upper)
  • 28. • Program to Reverse Strings • #include <stdio.h> #include <string.h> int main () { • int i; char a[10]; char temp; //clrscr(); // only works on windows gets(a); • for (i = 0; a[i] != '0' ; i++); • i--; • for (int j = 0; j <= i/2 ; j++) { • temp = a[j]; a[j] = a[i - j]; a[i - j] = temp; • } printf("%s",a); return(0); •
  • 29. Program to count the number of vo wels in a string : • Note Two different ways to declare strings • One using pointers *str • Two using character array char a[] • #include <stdio.h> #include <string.h> • void main() { • char *str; • char a[]="aeiouAEIOU"; • int i,j,count=0; • clrscr(); • printf("nEnter the stringn"); • gets(str); • for(i=0;str[i]!='0';i++) • { • for(j=0;a[j]!='0';j++) • if(a[j] == str[i] • { • count++; • break; • } printf("nNo. of vowels = %d",count); • } •