0% found this document useful (0 votes)
17 views

Lecture - Notes - III Strings

1. A data structure is a collection of variables of different types grouped together under a single name. 2. Structures allow users to create custom data types that bundle together different data types. 3. To define a structure, the struct keyword is used followed by the structure name and its data members. Structure members can then be accessed using the dot operator.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Lecture - Notes - III Strings

1. A data structure is a collection of variables of different types grouped together under a single name. 2. Structures allow users to create custom data types that bundle together different data types. 3. To define a structure, the struct keyword is used followed by the structure name and its data members. Structure members can then be accessed using the dot operator.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

strings

types of strings:

• C-style character string.

• string class type in standard C++.


C-style character string

- from C and supported in C++.

- 1-D array of characters which is terminated by a


null character '\0'.

- a null-terminated string contains the characters


that comprise the string followed by a null.
initialising C-strings

char str[4] = {'C','+','+','\0'};


- creates a string consisting of the word “C++".

- size of the character array containing the string is


one more than the number of characters in the
word. (???)

• the array can also be initialised as follows:


char str[4] = "C++";

char str[] = {'C','+','+','\0'};


string example
#include <iostream>
using namespace std;
int main ()
{
char str[5] = {'C','+','+', '!','\0'};
cout << “my first string is ";
cout << str << endl;

return 0;
}

my first string is C++


string example
#include <iostream>
using namespace std;
int main() {
char str[50];
cout<<“enter 1st string: ";
cin>>str;
cout<<“1st string is "<<str << endl;
cout<<"\n enter 2nd string: ";
cin>>str;
cout<<“2nd string is "<<str<<endl;
return 0;
}

1st string is: ... 2nd string is: ...


string example
extraction operator >> considers a space has a
terminating character.

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;
}

cin.get function takes two arguments: 1st argument is name of


string & 2nd argument is the maximum size of the array.
string functions
some fnctns that manipulate null-terminated strings:
strcpy(s1,s2) - copies string s2 into string s1.

strcat(s1,s2) - concatenates string s2 onto the


end of string s1.

strlen(s1) - returns the length of string s1.

strcmp(s1,s2) - returns 0 if s1 and s2 are the same; less than 0


if s1<s2; greater than 0 if s1>s2.

strchr(s1,c) - returns a pointer to the first occurrence of


character c in string s1.

strstr(s1,s2) - returns a pointer to the first occurrence of string


s2 in string s1.
string example
#include <iostream>
#include <cstring>
using namespace std;
int main ()
{
char str1[12] = "Hello ";
char str2[12] = "World";
char str3[12];
int len ;

// copy str1 into str3


strcpy( str3, str1);
cout << "strcpy( str3, str1) : " <<
str3 << endl;
string example
// concatenates str2 onto str1
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;
}
string examples

expected output:

strcpy( str3, str1) : Hello

strcat( str1, str2): Hello World

strlen(str1) : ???
exercise

write the program using string class in C++.

https://www.tutorialspoint.com/cplusplus/cpp_strings.htm
pointers
“a pointer is a variable whose value is the
address of another variable”

“a pointer is a variable that stores the address of


another variable”

pointers point to variables whose address they store.

a pointer can be used to access a variable pointed to


directly.

a pointer must be declared before being used.


pointer declaration
syntax
type *example;

- type is the pointer’s base type.


- example is the name of the pointer variable.
- * is used to declare a variable as pointer.
e.g.
double *example; // pointer to a double

data type of all pointers is a hexadecimal number


that represents a memory address.

only difference between pointers is the data type of


the variable or constant the pointer points to.
reference operator (&)
used to obtain the address of a variable (address-of-
operator).

syntax

example1 = &example2;

example1 gets the address value of the variable


example2.

ampersand sign (&) precedes the name of a variable.


address of a variable in memory cannot be known
before runtime.
dereference operator (*)
used to access the value of a variable a pointer points
to.

syntax

example1 = *example2;

example1 gets the value pointed to by example2.

reference and dereference operators are


complementary (opposite meaning)  an address
obtained with & can be dereferenced with *.
example
#include <iostream>
using namespace std;
int main() {
int var1 = 10;
int var2 = 20;
int var3 = 30;
cout<<&var1<<endl;
cout<<&var2<<endl;
cout<<&var3<<endl;
}

0x6ffe4c 0x6ffe48 0x6ffe44


pointer concepts
null pointer
- a constant with a value of zero defined in
several standard libraries.

pointer arithmetic
- four arithmetic operators that can be used on
pointers: ++, --, +, –

pointers & arrays


- close relationship between pointers and arrays
pointer concepts
pointer to pointer
- allows to have pointer on a pointer and so on.

passing pointers to functions


- passing an argument by reference or by address
both enable the passed argument to be changed in
the calling function by the called function.

return pointer from functions


- allows a function to return a pointer to local
variable, static variable and dynamically allocated
memory as well.
example
#include <iostream>
using namespace std;
int main ()
{
int var1 = 10; // variable declaration.
int *pointer1; // pointer declaration
pointer1 = &var1; // store address of var1 in pointer1
cout << “value of var1 is ";
cout << var1 << endl;

// display the address stored in pointer_1


cout << “address stored in pointer1 is ";
cout << pointer1 << endl;
// access the value at the address available in pointer1
cout << “value of *pointer1 is ";
cout << *pointer1 << endl;
return 0;
}
output

value of var1 is 10

address stored in pointer1: (hexadecimal value)???

value of *pointer1 is 10
data structures

“a data structure is a group of data elements


grouped together under one name.”

“structure is another user defined data type


which allows you to combine data items of
different kinds.”

“collection of variables of different types under a


single name”

data structures represent a record where members


can have different data types and different lengths.
data structures
syntax:
struct type_name {
member_type1 member_name1;
member_type2 member_name2;
member_type3 member_name3;
...
} object_names;
struct statement is used to define a new data type, with
more than one member.

type_name is a name for the structure type.


object_name can be a set of valid identifiers for objects
that have the type of this structure.
{ } hold a list of data members, each one with a type and
a valid identifier(name) .
data structures
defining a structure variable:

type_name variable_name ;
- structure variable variable_name is defined which
is of type structure type_name.

accessing members of a structure:

.
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

Enter registration number:


Enter Full name:
Enter age:
Enter gender:

Student Details
Reg number:
Name:
Age:
gender:

You might also like