0% found this document useful (0 votes)
15 views16 pages

Lecture 04 Strings Programs

The document outlines a summer course schedule covering topics such as arrays, pointers, structures, classes, GUI, files, and strings, culminating in an exam. It includes several programming exercises focused on string manipulation, such as finding character frequency, counting vowels and consonants, reversing strings, and removing non-alphabet characters. Each program is accompanied by sample output and code snippets.

Uploaded by

abodiab100100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views16 pages

Lecture 04 Strings Programs

The document outlines a summer course schedule covering topics such as arrays, pointers, structures, classes, GUI, files, and strings, culminating in an exam. It includes several programming exercises focused on string manipulation, such as finding character frequency, counting vowels and consonants, reversing strings, and removing non-alphabet characters. Each program is accompanied by sample output and code snippets.

Uploaded by

abodiab100100
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Summer Course content

First Week:
 First Day: Arrays & Pointers
 Second Day: Structure & Classes
Second Week:
 Third Day: GUI
 Fourth Day: File & Strings
Third Week:
Fourth Week:
 Exam.(Sunday- 27/8/2017)

2
Summer Training
Strings programs
Strings
4
Program # 1
 Find the Frequency of a specified character in a given string.

 Output:

Enter a string: This website is good site.


Enter a character to find its frequency: e
Frequency of e = 3

5
Program # 1 Code
#include <iosteam.h>
void main()
{
char c[50],ch; int i=0,count=0;
cout<<"Enter a string: "<<endl;
gets(c);
cout<<"Enter a character to find its frequency: "<<endl;
cin>>ch;
while(c[i]!='\0‘)
{
if(ch==c[i])
++count;
i++;
}
cout<<"Frequency of "<<ch<<" = "<< count ; 6

}
Program # 2
 Find Number of Vowels, Consonants, Digits and White
Space Character.

 Output:

Enter a line of string:


This program is easy to understand
Vowels:
Consonants:
Digits:
White spaces:
7
Program # 2 Code
#include<iostream.h>
void main()
{
char line[150];
int i,v,c,ch,d,s,o;
i= o=v=c=ch=d=s=0;
cout<<"Enter a line of string:"<<endl;
gets(line);
while(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') // is
vowel
++v;
8
Program # 2 Code
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z')) // is
Consonants.
++c;
else if(line[i]>='0'&&c<='9') // is number
++d;
else if (line[i]==' ') // space
++s;
i++;
}//end loop
cout<<"Vowels: "<<v;
cout<<"\nConsonants: "<<c;
cout<<"\nDigits:"<<d;
cout<<"\nWhite spaces: "<<s;
9
}
Program # 3
 Program to Reverse a String by Passing it to Function.

 This program asks user to enter a string. Then, that string is


passed to a function which reverses the string and finally the
reversed string is displayed in the main( ) function.

 Output:
Enter a string to reverse: zimargorp
Reversed string: programiz

10
Program # 3 Code
#include<iostream.h>
#include<stdio.h>
#include<string.h>
void Reverse(char str[]);
void main()
{
char str[100];
cout<<"Enter a string to reverse: ";
gets(str);
Reverse(str);
cout<<"Reversed string: "<<str;
}

11
Program # 3 Code
void Reverse(char str[])
{
int i,j; char temp[100];
for(i=strlen(str)-1,j=0; i+1!=0; --i,++j)
{
temp[j]=str[i];
}
temp[j]='\0';
strcpy(str,temp);
}
// another mehtod
void Reverse(char str[])
{ int i,j;
for(i=0; j=strlen(str)-1;i<strlen(str)/2;i++;j--)
{char c=str[i];
str[i]=str[j];
str[j]=c;
12
}
Program # 4
 Program to Remove all Characters in a String Except
Alphabet.

 This program takes a strings from user and removes all


characters in that string except alphabets.

 Output:
Enter a string: p2'r"o@gram84iz./
Output String: programiz

13
Program # 4 Code
#include<iostream.h>
#include<stodio.h>

void main()
{
char line[150]; int i,j;
cout<<"Enter a string: ";
gets(line);

14
Program # 4 Code
for(i=0; line[i]!='\0'; ++i)
{
while (!((line[i]>='a'&&line[i]<='z') || (line[i]>='A'&&line[i]<='Z' ||
line[i]=='\0')))
{
for(j=i;line[j]!='\0';++j)
line[j]=line[j+1];
line[j]='\0';
}//end while
}//end for loop
cout<<"Output String: "<<line;
}// end main

15
Thank You

16

You might also like