SlideShare a Scribd company logo
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 1
Handout#8
Assignment/Program Statement:
Write a C program using String – Find Number of Vowels, Consonants, Digits and
White Space Character
Learning Objectives:
Students will be able to
- declare and initialize and the string
- read and display the string
- write the program to manipulate the string
Theory:
 In C programming, array of character are called strings. A string is
terminated by null character /0. For example:
"c string tutorial"
Here, "c string tutorial" is a string. When, compiler encounters strings, it
appends null character at the end of string.
 Declaration of strings
Strings are declared in C in similar manner as arrays. Only difference is that,
strings are of char type.
char s[5];
Strings can also be declared using pointer.
char *p
 Initialization of strings
In C, string can be initialized in different number of ways.
char c[]="abcd";
OR,
char c[5]="abcd";
OR,
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 2
char c[]={'a','b','c','d','0'};
OR;
char c[5]={'a','b','c','d','0'};
String can also be initialized using pointers
char *c="abcd";
 Reading Strings from user
o Reading words from user.
char c[20];
scanf("%s",c);
String variable c can only take a word. It is beacause when white
space is encountered, the scanf() function terminates.
Write a C program to illustrate how to read string from terminal.
#include <stdio.h>
int main(){
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s.",name);
return 0;
}
Output
Enter name: Dennis Ritchie
Your name is Dennis.
Here, program will ignore Ritchie because, scanf() function takes
only string before the white space.
o Reading a line of text
C program to read line of text manually.
#include <stdio.h>
int main(){
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 3
char name[30],ch;
int i=0;
printf("Enter name: ");
while(ch!='n') // terminates if user hit enter
{
ch=getchar();
name[i]=ch;
i++;
}
name[i]='0'; // inserting null character at end
printf("Name: %s",name);
return 0;
}
This process to take string is tedious. There are predefined functions
gets() and puts in C language to read and display string respectively.
int main(){
char name[30];
printf("Enter name: ");
gets(name); //Function to read string from user.
printf("Name: ");
puts(name); //Function to display string.
return 0;
}
Both, the above program has same output below:
Output
Enter name: Tom Hanks
Name: Tom Hanks
 C supports a wide range of functions that manipulate null-terminated strings
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 4
So.No. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater
than 0 if s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string
s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
 gets() and puts()
Functions gets() and puts() are two string functions to take string input from
user and display string respectively as mentioned in previous chapter.
#include<stdio.h>
int main(){
char name[30];
printf("Enter name: ");
gets(name); //Function to read string from user.
printf("Name: ");
puts(name); //Function to display string.
return 0;
}
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 5
Though, gets() and puts() function handle string, both these functions are
defined in "stdio.h" header file
[Reference: http://www.tutorialspoint.com/cprogramming/c_strings.htm and
http://www.programiz.com/c-programming/string-handling-functions ]
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 6
Flowchart for Problem Statement:
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 7
Algorithm:
1. Start
2. v=c=d=s=0
3. read the line of string
4. if line[i]!=’0’
if (line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' ||
line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
c++
go to step 4
elseif ((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
v++
go to step 4
elseif(line[i]>='0'&&c<='9')
d++
go to step 4
elseif (line[i]==' ')
s++
go to step 4
else
go to step 5
5. Display Vowels, Consonants, Digits and White spaces
6. Stop
Program:
#include<stdio.h>
int main()
{
char line[150];
int i,v,c,ch,d,s,o;
v=c=d=s=0;
printf("Enter a line of string:n");
gets(line);
for(i=0;line[i]!='0';++i)
{
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 8
if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' ||
line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U')
++v;
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
++c;
else if(line[i]>='0'&&c<='9')
++d;
else if (line[i]==' ')
++s;
}
printf("Vowels: %d",v);
printf("nConsonants: %d",c);
printf("nDigits: %d",d);
printf("nWhite spaces: %d",s);
return 0;
}
Input:
Enter a line of string:
This program is easy 2 understand
Output:
Vowels: 9
Consonants: 18
Digits: 1
White spaces: 5
Practice Problem Statement:
1. Write a C program to find the Length of a String.
2. Write a C program to Concatenate Two Strings.
3. Write a C program to Copy a String.
4. Write a C program to remove all characters in a String except alphabet.
C-Programming
Walchand Institute of Technology (RC1131), Solapur Page 9
Conclusion:
Thus a C program using string – Find Number of Vowels, Consonants, Digits and
White Space Character is implemented.
Learning Outcomes:
At the end of this assignment, students are able to
- declare and initialize and the string
- read and display the string
- write the program to manipulate the string

More Related Content

PDF
CP Handout#9
PDF
CP Handout#5
PDF
CP Handout#7
PDF
CP Handout#2
PDF
CP Handout#10
PDF
CP Handout#6
PDF
CP Handout#3
PDF
CP Handout#4
CP Handout#9
CP Handout#5
CP Handout#7
CP Handout#2
CP Handout#10
CP Handout#6
CP Handout#3
CP Handout#4

What's hot (20)

PDF
CP Handout#1
PPTX
C programming(Part 1)
PPSX
Concepts of C [Module 2]
PPTX
Programming in C (part 2)
PPT
Mesics lecture 5 input – output in ‘c’
PPTX
What is c
PDF
Programming with c language practical manual
PPTX
Introduction to Basic C programming 02
PPSX
Programming in C [Module One]
DOC
Ocs752 unit 1
DOC
Ocs752 unit 2
PDF
C Programming
DOC
Ocs752 unit 3
PDF
C programming Workshop
PPTX
Basic Input and Output
PPTX
C programming(part 3)
PDF
Learning the C Language
PPT
Unit i intro-operators
PPT
Input And Output
CP Handout#1
C programming(Part 1)
Concepts of C [Module 2]
Programming in C (part 2)
Mesics lecture 5 input – output in ‘c’
What is c
Programming with c language practical manual
Introduction to Basic C programming 02
Programming in C [Module One]
Ocs752 unit 1
Ocs752 unit 2
C Programming
Ocs752 unit 3
C programming Workshop
Basic Input and Output
C programming(part 3)
Learning the C Language
Unit i intro-operators
Input And Output
Ad

Similar to CP Handout#8 (20)

PDF
Strings IN C
DOCX
string , pointer
PPTX
C Programming Unit-3
PPTX
Week6_P_String.pptx
PPT
14 strings
PPTX
C language
PPTX
ession Outcomes: By the end of this session, learners will be able to: Unde...
PDF
C programing Tutorial
PDF
VTU PCD Model Question Paper - Programming in C
PDF
answer-model-qp-15-pcd13pcd
PPTX
programming for problem solving using C-STRINGSc
PDF
Chapter 3 - Characters and Strings - Student.pdf
PDF
35001622067_SOUMYADIP MAITY .pdf C programming
PPTX
Array and string
PDF
Arrays and strings in c++
DOC
Assignment c programming
PPT
OIT 116 - Lecture 3_2 Arrays, Pointers and Strings (1) (1).ppt
DOCX
C Programming Strings.docx
PPT
Strings v.1.1
Strings IN C
string , pointer
C Programming Unit-3
Week6_P_String.pptx
14 strings
C language
ession Outcomes: By the end of this session, learners will be able to: Unde...
C programing Tutorial
VTU PCD Model Question Paper - Programming in C
answer-model-qp-15-pcd13pcd
programming for problem solving using C-STRINGSc
Chapter 3 - Characters and Strings - Student.pdf
35001622067_SOUMYADIP MAITY .pdf C programming
Array and string
Arrays and strings in c++
Assignment c programming
OIT 116 - Lecture 3_2 Arrays, Pointers and Strings (1) (1).ppt
C Programming Strings.docx
Strings v.1.1
Ad

More from trupti1976 (10)

PDF
MobileAppDev Handout#3
PDF
MobileAppDev Handout#10
PDF
MobileAppDev Handout#9
PDF
MobileAppDev Handout#8
PDF
MobileAppDev Handout#7
PDF
MobileAppDev Handout#6
PDF
MobileAppDev Handout#5
PDF
MobileAppDev Handout#4
PDF
MobileAppDev Handout#2
PDF
MobileAppDev Handout#1
MobileAppDev Handout#3
MobileAppDev Handout#10
MobileAppDev Handout#9
MobileAppDev Handout#8
MobileAppDev Handout#7
MobileAppDev Handout#6
MobileAppDev Handout#5
MobileAppDev Handout#4
MobileAppDev Handout#2
MobileAppDev Handout#1

Recently uploaded (20)

PDF
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
PPTX
UNIT 4 Total Quality Management .pptx
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PPTX
Geodesy 1.pptx...............................................
PDF
III.4.1.2_The_Space_Environment.p pdffdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
PDF
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
PPTX
Construction Project Organization Group 2.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Embodied AI: Ushering in the Next Era of Intelligent Systems
PDF
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
PPTX
Fundamentals of Mechanical Engineering.pptx
PPTX
Artificial Intelligence
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PPTX
additive manufacturing of ss316l using mig welding
PDF
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
PPTX
Foundation to blockchain - A guide to Blockchain Tech
A SYSTEMATIC REVIEW OF APPLICATIONS IN FRAUD DETECTION
UNIT 4 Total Quality Management .pptx
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Geodesy 1.pptx...............................................
III.4.1.2_The_Space_Environment.p pdffdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
CARTOGRAPHY AND GEOINFORMATION VISUALIZATION chapter1 NPTE (2).pptx
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
Artificial Superintelligence (ASI) Alliance Vision Paper.pdf
Unit I ESSENTIAL OF DIGITAL MARKETING.pdf
Construction Project Organization Group 2.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Embodied AI: Ushering in the Next Era of Intelligent Systems
PREDICTION OF DIABETES FROM ELECTRONIC HEALTH RECORDS
Fundamentals of Mechanical Engineering.pptx
Artificial Intelligence
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
additive manufacturing of ss316l using mig welding
null (2) bgfbg bfgb bfgb fbfg bfbgf b.pdf
Foundation to blockchain - A guide to Blockchain Tech

CP Handout#8

  • 1. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 1 Handout#8 Assignment/Program Statement: Write a C program using String – Find Number of Vowels, Consonants, Digits and White Space Character Learning Objectives: Students will be able to - declare and initialize and the string - read and display the string - write the program to manipulate the string Theory:  In C programming, array of character are called strings. A string is terminated by null character /0. For example: "c string tutorial" Here, "c string tutorial" is a string. When, compiler encounters strings, it appends null character at the end of string.  Declaration of strings Strings are declared in C in similar manner as arrays. Only difference is that, strings are of char type. char s[5]; Strings can also be declared using pointer. char *p  Initialization of strings In C, string can be initialized in different number of ways. char c[]="abcd"; OR, char c[5]="abcd"; OR,
  • 2. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 2 char c[]={'a','b','c','d','0'}; OR; char c[5]={'a','b','c','d','0'}; String can also be initialized using pointers char *c="abcd";  Reading Strings from user o Reading words from user. char c[20]; scanf("%s",c); String variable c can only take a word. It is beacause when white space is encountered, the scanf() function terminates. Write a C program to illustrate how to read string from terminal. #include <stdio.h> int main(){ char name[20]; printf("Enter name: "); scanf("%s",name); printf("Your name is %s.",name); return 0; } Output Enter name: Dennis Ritchie Your name is Dennis. Here, program will ignore Ritchie because, scanf() function takes only string before the white space. o Reading a line of text C program to read line of text manually. #include <stdio.h> int main(){
  • 3. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 3 char name[30],ch; int i=0; printf("Enter name: "); while(ch!='n') // terminates if user hit enter { ch=getchar(); name[i]=ch; i++; } name[i]='0'; // inserting null character at end printf("Name: %s",name); return 0; } This process to take string is tedious. There are predefined functions gets() and puts in C language to read and display string respectively. int main(){ char name[30]; printf("Enter name: "); gets(name); //Function to read string from user. printf("Name: "); puts(name); //Function to display string. return 0; } Both, the above program has same output below: Output Enter name: Tom Hanks Name: Tom Hanks  C supports a wide range of functions that manipulate null-terminated strings
  • 4. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 4 So.No. Function & Purpose 1 strcpy(s1, s2); Copies string s2 into string s1. 2 strcat(s1, s2); Concatenates string s2 onto the end of string s1. 3 strlen(s1); Returns the length of string s1. 4 strcmp(s1, s2); Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if s1>s2. 5 strchr(s1, ch); Returns a pointer to the first occurrence of character ch in string s1. 6 strstr(s1, s2); Returns a pointer to the first occurrence of string s2 in string s1.  gets() and puts() Functions gets() and puts() are two string functions to take string input from user and display string respectively as mentioned in previous chapter. #include<stdio.h> int main(){ char name[30]; printf("Enter name: "); gets(name); //Function to read string from user. printf("Name: "); puts(name); //Function to display string. return 0; }
  • 5. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 5 Though, gets() and puts() function handle string, both these functions are defined in "stdio.h" header file [Reference: http://www.tutorialspoint.com/cprogramming/c_strings.htm and http://www.programiz.com/c-programming/string-handling-functions ]
  • 6. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 6 Flowchart for Problem Statement:
  • 7. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 7 Algorithm: 1. Start 2. v=c=d=s=0 3. read the line of string 4. if line[i]!=’0’ if (line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U') c++ go to step 4 elseif ((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z')) v++ go to step 4 elseif(line[i]>='0'&&c<='9') d++ go to step 4 elseif (line[i]==' ') s++ go to step 4 else go to step 5 5. Display Vowels, Consonants, Digits and White spaces 6. Stop Program: #include<stdio.h> int main() { char line[150]; int i,v,c,ch,d,s,o; v=c=d=s=0; printf("Enter a line of string:n"); gets(line); for(i=0;line[i]!='0';++i) {
  • 8. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 8 if(line[i]=='a' || line[i]=='e' || line[i]=='i' || line[i]=='o' || line[i]=='u' || line[i]=='A' || line[i]=='E' || line[i]=='I' || line[i]=='O' || line[i]=='U') ++v; else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z')) ++c; else if(line[i]>='0'&&c<='9') ++d; else if (line[i]==' ') ++s; } printf("Vowels: %d",v); printf("nConsonants: %d",c); printf("nDigits: %d",d); printf("nWhite spaces: %d",s); return 0; } Input: Enter a line of string: This program is easy 2 understand Output: Vowels: 9 Consonants: 18 Digits: 1 White spaces: 5 Practice Problem Statement: 1. Write a C program to find the Length of a String. 2. Write a C program to Concatenate Two Strings. 3. Write a C program to Copy a String. 4. Write a C program to remove all characters in a String except alphabet.
  • 9. C-Programming Walchand Institute of Technology (RC1131), Solapur Page 9 Conclusion: Thus a C program using string – Find Number of Vowels, Consonants, Digits and White Space Character is implemented. Learning Outcomes: At the end of this assignment, students are able to - declare and initialize and the string - read and display the string - write the program to manipulate the string