Skip to main content

Strings

char​

char type of variable can store only one character at a time.

Example 1 :

  #include <iostream>
using namespace std;

int main(){

char name = 'A';
cout<<name;

return 0;

}

Output :

A

Explanationn :

In a Line no 6 we declare name variable, datatype is char and store 'A' character.
In a Line no 7 print name variable using cout.

Example 2 :

  #include <iostream>
using namespace std;

int main(){

char name = 'RTC';
cout<<name;

return 0;

}

Output :

C

Explanationn :

In a Line no 6 we declare name variable, datatype is char and store 'RTC' character.
In a Line no 7 print name variable using cout.

Strings :​

It is a collection of characters .

'\0' = Null character (end of the string).

Example 3 :

 #include <iostream>
using namespace std;

int main() {
char arr[4] = {'R', 'T', 'C', '\0'};
cout<<arr;

return 0;
}

Output :

RTC

Explanation :

In a Line no 5 we declare array that name has arr, size of array is 4, and stored multiple variable {'R', 'T', 'C', '\0'}.
Print array using cout.

Example 4 :

    #include <iostream>
using namespace std;
int main() {

char str[50];
cout<<"Enter Your Name :";
cin>>str;

cout<<"Hello"<<str;

return 0;
}

Output :

Enter Character : pinki
Hello pinki

Explanation :

In a Line no 5 we declare array that name has str, size of array is 50.
Print Enter Your Name : using cout.
str variable using cin.
print Hello using cout and str varible.

Example 5 :

 #include<iostream>
using namespace std;

int main()
{
char str[50];
cout<<"Enter Your name: ";
cin>>str;

int count = 0;

while(str[count]!='\0')
{
count++;
}
cout<<"Length :"<<count;
return 0;
}

Output :

Enter Your name: pinki
Length :5

Explanation :

In a Line no 5 we declare array that name has str, size of array is 50.
Print Enter Your Name : using cout.
str variable using cin.
initialize count varible, store 0 value.
Start while loop check condition (str[count]!='\0') and increase the value of count by one.
Print Length = and count variable using cout.

Example 6 :

    #include <iostream>
using namespace std;
int main() {

char str[50];
cout<<"Enter Your Name :";
cin>>str;

int count = 0;

while(str[count] != '\0')
{
count++
}

cout<<"Lenght = " <<count;

return 0;
}

Output :

Enter Your name: Road To Code
Length = 10

Explanation :

In a Line no 5 we declare array that name has str, size of array is 50.
Print Enter Your Name : using cout.
str variable using cin.
initialize count varible, store 0 value.
Start while loop check condition (str[count]!='\0') and increase the value of count by one.
Print Length = and count variable using cout.

ASCII​

A = American
S = Standard
C = code
I = Information
I = Interchannge

A <- 65    a <- 97
B <- 66 b <- 98
C <- 67 c <- 99
. .
. .
. .
Z <- 90 z <- 122