C++ Program To Remove All Characters In
A String Except Alphabet
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char line[150];
int i,j;
cout << "Enter A String:
";
cin.getline(line, 150);
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';
}}
cout << "\nOutput String:
" << line;
return 0;
}
C++ Program To Find The Number Of
Vowels, Consonants, Digits And White Spaces In A String
#include<iostream>
#include<cstring>
using namespace std;
int main() {
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
cout<<"Enter A Sentence:
";
cin.getline(line, 150);
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'&&
line[i]<='9')
++d;
else if (line[i]==' ')
++s;
}
cout << "\nVowels: "
<< v;
cout << "\nConsonants: "
<< c;
cout << "\nDigits: "
<< d;
cout << "\nWhite spaces:
" << s;
cout << "\n\n";
}
C++ Program To Find The Length Of A
String
Method
1:
#include<iostream>
#include<cstring>
using namespace std;
int main() {
int i,length=0;
char str[1000]; // string length <
1000
cout <<"Enter A Sentence:
";
cin.getline(str, 1000);
for(i=0;str[i]!='\0';i++) {
length++;
}
cout <<"\nString Length:
" << length << "\n";
return 0;
}
Method
2:
#include<iostream>
#include<cstring>
using namespace std;
int main() {
int length;
char str[1000];
cout <<"Enter A Sentence:
";
cin.getline(str, 1000);
length = strlen(str);
cout <<"\nString Length:
" << length << "\n";
return 0;
}