Lecture - Notes - III Strings
Lecture - Notes - III Strings
types of strings:
return 0;
}
to overcome that:
#include <iostream>
using namespace std;
int main() {
char str[50];
cout<<“enter a string: ";
cin.get(str, 50);
cout<<“entered string is "<< str <<endl;
return 0;
}
return 0;
}
string examples
expected output:
strlen(str1) : ???
exercise
https://www.tutorialspoint.com/cplusplus/cpp_strings.htm
pointers
“a pointer is a variable whose value is the
address of another variable”
syntax
example1 = &example2;
syntax
example1 = *example2;
pointer arithmetic
- four arithmetic operators that can be used on
pointers: ++, --, +, –
value of var1 is 10
value of *pointer1 is 10
data structures
type_name variable_name ;
- structure variable variable_name is defined which
is of type structure type_name.
.
the dot operator( ) is used.
e.g.
to access age of structure variable stud1 and assign
10 to age.
stud1.age = 10;
example
#include <iostream>
using namespace std;
struct student {
char regnumber[15]
char name[50];
int age;
char gender;
};
int main() {
student stud1;
cout << "Enter registration number: ";
cin.get(stud1.regnumber, 15);
cout << "Enter Full name: ";
cin.get(stud1.name, 50);
cout << "Enter age: ";
cin >> stud1.age;
cout << "Enter gender: ";
cin >> stud1.gender;
cout << "\n Student Details" << endl;
cout << “Reg number: " << stud1.regnumber << endl;
cout << "Name: " << stud1.name << endl;
cout <<"Age: " << stud1.age << endl;
cout << “Gender: " << stud1.gender;
return 0;
}
output
Student Details
Reg number:
Name:
Age:
gender: