SlideShare a Scribd company logo
Strings
• A special kind of array is an array of characters
ending in the null character 0 called string
arrays
• 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
space for the null character which is also known
as the string terminator character
C offers four main operations on
strings
• strcpy - copy one string into another
• strcat - append one string onto the right
side 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
world” 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
destinationstring
• 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
stringb alphabetically
• Returns a positive value if stringb precedes
stringa alphabetically
• Returns 0 if they are equal
• Note lowercase characters are greater than
Uppercase
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
that 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
than 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
screen in a non-interactive environment
• getche() like getchar() except interactive
• putchar(int ch) outputs a character to
screen
• gets(str) gets a string from the keyboard
• puts(str) outputs string to screen
Characters are at the heart of
strings
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
Functions
• Don't forget to #include <ctype.h> to get
the 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, code
less 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
vowels 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);
•
}
•
}

More Related Content

Similar to String manipulation techniques like string compare copy (20)

Chapterabcdefghijklmnopqrdstuvwxydanniipo
ChapterabcdefghijklmnopqrdstuvwxydanniipoChapterabcdefghijklmnopqrdstuvwxydanniipo
Chapterabcdefghijklmnopqrdstuvwxydanniipo
abritip
 
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++
Samsil Arefin
 
Strings part2
Strings part2Strings part2
Strings part2
yndaravind
 
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPTCfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
JITENDER773791
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
Strings(2007)
Strings(2007)Strings(2007)
Strings(2007)
svit vasad
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
C string
C stringC string
C string
University of Potsdam
 
Strings cprogramminglanguagedsasheet.pptx
Strings cprogramminglanguagedsasheet.pptxStrings cprogramminglanguagedsasheet.pptx
Strings cprogramminglanguagedsasheet.pptx
hyundaitvhamari
 
string in C
string in Cstring in C
string in C
CGC Technical campus,Mohali
 
string.ppt
string.pptstring.ppt
string.ppt
lakshmanarao027MVGRC
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
Hemantha Kulathilake
 
Unitii string
Unitii stringUnitii string
Unitii string
Sowri Rajan
 
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptxString_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
monimhossain14
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
ssusere19c741
 
07-Strings( string Functions and parameters C).pdf
07-Strings( string Functions and parameters C).pdf07-Strings( string Functions and parameters C).pdf
07-Strings( string Functions and parameters C).pdf
nithishkumar2867
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
Md. Ashikur Rahman
 
strings
stringsstrings
strings
teach4uin
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 
Strings in c++
Strings in c++Strings in c++
Strings in c++
International Islamic University
 
Chapterabcdefghijklmnopqrdstuvwxydanniipo
ChapterabcdefghijklmnopqrdstuvwxydanniipoChapterabcdefghijklmnopqrdstuvwxydanniipo
Chapterabcdefghijklmnopqrdstuvwxydanniipo
abritip
 
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++
Samsil Arefin
 
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPTCfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
Cfbcgdhfghdfhghggfhghghgfhgfhgfhhapter11.PPT
JITENDER773791
 
Strings in c
Strings in cStrings in c
Strings in c
vampugani
 
C programming - String
C programming - StringC programming - String
C programming - String
Achyut Devkota
 
Strings cprogramminglanguagedsasheet.pptx
Strings cprogramminglanguagedsasheet.pptxStrings cprogramminglanguagedsasheet.pptx
Strings cprogramminglanguagedsasheet.pptx
hyundaitvhamari
 
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptxString_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
String_Slide.pptxStructure.pptxStructure.pptxStructure.pptxStructure.pptx
monimhossain14
 
0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf0-Slot21-22-Strings.pdf
0-Slot21-22-Strings.pdf
ssusere19c741
 
07-Strings( string Functions and parameters C).pdf
07-Strings( string Functions and parameters C).pdf07-Strings( string Functions and parameters C).pdf
07-Strings( string Functions and parameters C).pdf
nithishkumar2867
 
Cse115 lecture14strings part01
Cse115 lecture14strings part01Cse115 lecture14strings part01
Cse115 lecture14strings part01
Md. Ashikur Rahman
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
Tasnima Hamid
 

Recently uploaded (20)

IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
Principles of Building planning and its objectives.pptx
Principles of Building planning and its objectives.pptxPrinciples of Building planning and its objectives.pptx
Principles of Building planning and its objectives.pptx
PinkiDeb4
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
Environmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptxEnvironmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptx
SheerazAhmed77
 
Introduction to AI agent development with MCP
Introduction to AI agent development with MCPIntroduction to AI agent development with MCP
Introduction to AI agent development with MCP
Dori Waldman
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
ANFIS Models with Subtractive Clustering and Fuzzy C-Mean Clustering Techniqu...
Journal of Soft Computing in Civil Engineering
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Structural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptxStructural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptx
gunjalsachin
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Influence line diagram for truss in a robust
Influence line diagram for truss in a robustInfluence line diagram for truss in a robust
Influence line diagram for truss in a robust
ParthaSengupta26
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank22PCOAM16 _ML_Unit 3 Notes & Question bank
22PCOAM16 _ML_Unit 3 Notes & Question bank
Guru Nanak Technical Institutions
 
IOt Based Research on Challenges and Future
IOt Based Research on Challenges and FutureIOt Based Research on Challenges and Future
IOt Based Research on Challenges and Future
SACHINSAHU821405
 
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbbTree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
Tree_Traversals.pptbbbbbbbbbbbbbbbbbbbbbbbbb
RATNANITINPATIL
 
Principles of Building planning and its objectives.pptx
Principles of Building planning and its objectives.pptxPrinciples of Building planning and its objectives.pptx
Principles of Building planning and its objectives.pptx
PinkiDeb4
 
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdfRearchitecturing a 9-year-old legacy Laravel application.pdf
Rearchitecturing a 9-year-old legacy Laravel application.pdf
Takumi Amitani
 
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra OpticaPruebas y Solucion de problemas empresariales en redes de Fibra Optica
Pruebas y Solucion de problemas empresariales en redes de Fibra Optica
OmarAlfredoDelCastil
 
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghjfHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
fHUINhKG5lM1WBBk608.pptxfhjjhhjffhiuhhghj
yadavshivank2006
 
Software Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha TasnuvaSoftware Engineering Project Presentation Tanisha Tasnuva
Software Engineering Project Presentation Tanisha Tasnuva
tanishatasnuva76
 
Software Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance OptimizationSoftware Developer Portfolio: Backend Architecture & Performance Optimization
Software Developer Portfolio: Backend Architecture & Performance Optimization
kiwoong (daniel) kim
 
Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)Call For Papers - International Journal on Natural Language Computing (IJNLC)
Call For Papers - International Journal on Natural Language Computing (IJNLC)
kevig
 
Environmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptxEnvironmental Engineering Wastewater.pptx
Environmental Engineering Wastewater.pptx
SheerazAhmed77
 
Introduction to AI agent development with MCP
Introduction to AI agent development with MCPIntroduction to AI agent development with MCP
Introduction to AI agent development with MCP
Dori Waldman
 
Influence line diagram in a robust model
Influence line diagram in a robust modelInfluence line diagram in a robust model
Influence line diagram in a robust model
ParthaSengupta26
 
FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.FISICA ESTATICA DESING LOADS CAPITULO 2.
FISICA ESTATICA DESING LOADS CAPITULO 2.
maldonadocesarmanuel
 
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.pptfy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
fy06_46f6-ht30_22_oil_gas_industry_guidelines.ppt
sukarnoamin
 
Structural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptxStructural Health and Factors affecting.pptx
Structural Health and Factors affecting.pptx
gunjalsachin
 
ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025ACEP Magazine Fifth Edition on 5june2025
ACEP Magazine Fifth Edition on 5june2025
Rahul
 
Influence line diagram for truss in a robust
Influence line diagram for truss in a robustInfluence line diagram for truss in a robust
Influence line diagram for truss in a robust
ParthaSengupta26
 
First Review PPT gfinal gyft ftu liu yrfut go
First Review PPT gfinal gyft  ftu liu yrfut goFirst Review PPT gfinal gyft  ftu liu yrfut go
First Review PPT gfinal gyft ftu liu yrfut go
Sowndarya6
 
Ad

String manipulation techniques like string compare copy

  • 1. Strings • A special kind of array is an array of characters ending in the null character 0 called string arrays • 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 space for the null character which is also known as the string terminator character
  • 2. C offers four main operations on strings • strcpy - copy one string into another • strcat - append one string onto the right side 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 world” 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 destinationstring • 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 stringb alphabetically • Returns a positive value if stringb precedes stringa alphabetically • Returns 0 if they are equal • Note lowercase characters are greater than Uppercase
  • 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 that 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 than 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 screen in a non-interactive environment • getche() like getchar() except interactive • putchar(int ch) outputs a character to screen • gets(str) gets a string from the keyboard • puts(str) outputs string to screen
  • 16. Characters are at the heart of strings
  • 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 Functions • Don't forget to #include <ctype.h> to get the 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, code less 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 vowels 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); • } • }