Loop Example
#include <iostream>
int main (){
int a = 10;
while( a < 20 ) {
cout << "value of a: " << a << endl;
a++;
}
return 0;
}
====================================
menggunakan for;
#include <iostream>
int main(){
for ( int x = 0; x < 10; x++ ) {
cout<< x <<endl;
}
cin.get();
}
====================================
menggunakan while;
#include <iostream>
int main(){
int x = 0; // Don't forget to declare variables
while ( x < 10 ) { // While x is less than 10
cout<< x <<endl;
x++; // Update x so the condition can be met eventually
}
cin.get();
}
====================================
menggunakan do;
#include <iostream>
int main(){
int x;
x = 0;
do {
cout<<"Hello, world!\n";
} while ( x != 0 );
cin.get();
}
====================================
menggunakan if:
#include <iostream>
int main (){
for (int n=10; n>0; n--){
cout << n << ", ";
if (n==3) {
cout << "countdown aborted!";
break;
}
}
}
====================================
menggunakan label
#include <iostream>
int main (){
int n=10;
mylabel:
cout << n << ", ";
n--;
if (n>0) goto mylabel;
cout << "liftoff!\n";
}
====================================
menggunakan while dan good:
#include <iostream>
int main (){
int x;
while(true){
cout << "Enter an integer: "; // prompt the user for input
cin >> x; // get input
// check and see if the input statement succeeded
if (cin.good() == false) {
cout << "That was not an integer." << endl;
return -1;
}
cout << x << endl;
}
return 0;
}
====================================
menggunakan getch:
#include <iostream>
#include <string>
#include <conio.h>
int main(){
string name;
cout << "Please enter your first name: ";
cin >> name;
cout << "Welcome " << name << "!" << endl;
getch();//conio.h=loop display
}
TEORI STRING C++
Contoh: "c string tutorial" =>A string is terminated by null character /0.
Deklarasi String:
char s[5];
atau cara pointer. =>char *p
char c[]="abcd";//unlimited
OR,
char c[5]="abcd";//init 5 char
OR,
char c[]={'a','b','c','d','\0'};
OR;
char c[5]={'a','b','c','d','\0'};
====================================
menggunakan char untuk string:
#include <iostream>
using namespace std;
int main (){
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
cout << "Greeting message: ";
cout << greeting << endl;
return 0;
}
====================================
menggunakan printf:
#include <stdio.h>
int main(){
char name[20];
printf("Enter name: ");
scanf("%s",name);
printf("Your name is %s.",name);
return 0;
}
====================================
menggunakan getchar:
#include <stdio.h>
int main(){
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;
}
====================================
menggunakan gets:
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;
}
====================================
menggunakan gets
#include <stdio.h>
void Display(char ch[]);
int main(){
char c[50];
printf("Enter string: ");
gets(c);
Display(c); // Passing string c to function.
return 0;
}
void Display(char ch[]){
printf("String Output: ");
puts(ch);
}
====================================
menggunakan Manipulasi String function:
S.N. 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.
Strupr():string to uppercase
strlwr():string to lowercase
strcat():joints /concatenates two strings
EXAMPLE
#include <iostream>
#include <cstring>
int main (){
char str1[10] = "Hello";
char str2[10] = "World";
char str3[10];
int len ;
// copy str1 into str3
strcpy( str3, str1);
cout << "strcpy( str3, str1) : " << str3 << endl;
// concatenates str1 and str2
strcat( str1, str2);
cout << "strcat( str1, str2): " << str1 << endl;
// total lenghth of str1 after concatenation
len = strlen(str1);
cout << "strlen(str1) : " << len << endl;
return 0;
}
Output:
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
Source Code to Find the Frequency of Characters
#include <stdio.h>
int main(){
char c[1000],ch;//ch=char 1 pointer,c=string
int i,count=0;
printf("Enter a string: ");
gets(c);//get string
printf("Enter a characeter to find frequency: ");
scanf("%c",&ch);//get char
for(i=0;c[i]!='\0';++i) {
if(ch==c[i])
++count;
}
printf("Frequency of %c = %d", ch, count);
return 0;
}
Source Code to Find Number of Vowels, Consonants, Digits and White Space Character
#include<stdio.h>
int main(){
char line[150];
int i,v,c,ch,d,s,o;
o=v=c=ch=d=s=0;
printf("Enter a line of string:\n");
gets(line);
for(i=0;line[i]!='\0';++i)
{
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;
}
Source Code to Reverse String
#include<stdio.h>
#include<string.h>
void Reverse(char str[]);
int main(){
char str[100];
printf("Enter a string to reverse: ");
gets(str);
Reverse(str);
printf("Reversed string: ");
puts(str);
return 0;
}
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);
}
Source Code to Calculated Length without Using strlen() Function
#include <stdio.h>
int main()
{
char s[1000],i;
printf("Enter a string: ");
scanf("%s",s);
for(i=0; s[i]!='\0'; ++i);
printf("Length of string: %d",i);
return 0;
}
Source Code to Concatenate Two Strings Manually
#include <stdio.h>
int main()
{
char s1[100], s2[100], i, j;
printf("Enter first string: ");
scanf("%s",s1);
printf("Enter second string: ");
scanf("%s",s2);
for(i=0; s1[i]!='\0'; ++i); /* i contains length of string s1. */
for(j=0; s2[j]!='\0'; ++j, ++i)
{
s1[i]=s2[j];
}
s1[i]='\0';
printf("After concatenation: %s",s1);
return 0;
}
Source Code to Copy String Manually
#include <stdio.h>
int main()
{
char s1[100], s2[100], i;
printf("Enter string s1: ");
scanf("%s",s1);
for(i=0; s1[i]!='\0'; ++i)
{
s2[i]=s1[i];
}
s2[i]='\0';
printf("String s2: %s",s2);
return 0;
}
Source Code to Remove Characters in String Except Alphabets
#include<stdio.h>
int main(){
char line[150];
int i,j;
printf("Enter a string: ");
gets(line);
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';
}
}
printf("Output String: ");
puts(line);
return 0;
}
Output
Enter a string: p2'r"o@gram84iz./
Output String: programiz
Source Code to Sort Words in Dictionary Order
#include<stdio.h>
#include <string.h>
int main(){
int i,j;
char str[10][50],temp[50];
printf("Enter 10 words:\n");
for(i=0;i<10;++i)
gets(str[i]);
for(i=0;i<9;++i)
for(j=i+1;j<10 ;++j){
if(strcmp(str[i],str[j])>0)
{
strcpy(temp,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],temp);
}
}
printf("In lexicographical order: \n");
for(i=0;i<10;++i){
puts(str[i]);
}
return 0;
}
CLASS STRING
#include <iostream>
#include <string>
using namespace std;
int main()
{
string your_name;
cout << "Hello, What is your name? ";
cin >> your_name;
cout << "Pleased to meet you, " << your_name;
}
#include <iostream>
#include <string>
int main(){
cout << "Please enter your full name: ";
string full_name;
getline( cin, full_name );
int middle = full_name.size() / 2;
string greeting = "Hello " + full_name + ", ";
cout << greeting << "the middle character of your name is "
<< full_name[middle] << " at position "
<< middle;
}
The comparison operators >, <, ==, >=, <=, != ,strcmp
if(str == "Hello World!"){
std::cout << "Strings are equal!";
}
std::string haystack = "Hello World!";
std::string needle = "o";
std::cout << haystack.find(needle);
=4
string wikistr = "wikipedia is full of wikis (wiki-wiki means fast)";
for(string::size_type i = 0, tfind; (tfind = wikistr.find("wiki", i)) != string::npos; i = tfind + 1){
cout << "Found occurrence of 'wiki' at position " << tfind << endl;
}
=0 21 28 33
string::size_type strSize = str.size();
string::size_type strSize2 = str2.length();
string newstr = " Human";
str.insert (5,newstr);
=Hello Human World!
str.erase (6,11);
=Hello!
string str = "Hello World!";
string part = str.substr(6,5);
= World