Char S "Hello" - Error: Char Type of Variable Can Store A Single Character. Example
Char S "Hello" - Error: Char Type of Variable Can Store A Single Character. Example
char vs string
char type of variable can store a single character.
example:
char c=‘A’;
Example:
char str[6]={‘H’,’e’,’l’,’l’,’o’,’\0’};
char str[6]=“Hello”; // ‘\0’ will be included at the end.
getline(cin,str) vs cin.getline(str,100)
getline(cin,str) is used for reading a string object. It will not
work for char array.
example:
1. string str;
getline(cin,str); it is used with string class.
2. char str[10];
cin.getline(str,100); it is used with char array.
sizeof() vs strlen()
sizeof is used for finding size of a data type or variable. It cannot
find string length
strlen(str) is a function used for finding length of a string.
strtok()
It is used for tokenising a string
In first call we pass a string from second call we pass NULL, it will
use same string given in first call.
First call
strtok(“hello:how:are:you”,”:”); this is the first call
• In first call it will return first token “hello” as a string
• char *t; is used for taking tokens.
• In first call, it will store the string internally.
Second call
strtok(NULL,”:”); this is the second call.
• NULL means, it will use the same string given in first call
• From second call onwards it will give second token onwards.
• Same call is repeated with NULL to get all the tokens.
cin.ignore()
• When we enter any input from keyboard, it is transferred to an
input buffer.
• Program reads the data from input buffer
• After entering value from keyboard, we hit enter.
• Program will read the value and ignore enter key from buffer.
• If program doesn’t ignore it the it may not read next input.
• cin.ignore() is used for forcing the program to ignore it.
• Usually programs don’t read a string value because of enter key.
• Use cin.ignore() before reading a string.
#include<cstring> vs #include<string>
#include<cstring> this library contains C language function
#include<string> this contains C++ class for string