Download presentation
Presentation is loading. Please wait.
1
Characters and Strings
A character constant is an integer value represented as a character in single quotes. ‘z’ represents the integer value of z (122 in ASCII code). ‘\n’ represents the integer value of z (10 in ASCII code). characters and strings manipulation
2
characters and strings manipulation
A string is a series of characters treated as a single unit. String literals (string constants) are written in double quotations. “99 Main Street” (a street address literal) A string in C++ is an array of characters ending with the null character (‘\0’). A string is accessed via a pointer to the first character (like array). characters and strings manipulation
3
characters and strings manipulation
String definition char color[] = ”blue”; char *colorptr = ”blue”; char color[] = {‘b’, ‘l’, ‘u’, ‘e’, ‘\0’}; characters and strings manipulation
4
characters and strings manipulation
String definition char word[20]; cin>>word; Note that the string is no longer than 19 characters. We can use cin.getline to read a line (more than one word). characters and strings manipulation
5
characters and strings manipulation
String definition char sentence[80]; cin.getline(sentence, 80, ‘\n’); The cin.getline has ‘\n’ as a default value, so the preceding function call could have been written as: cin.getline(sentence, 80); characters and strings manipulation
6
characters and strings manipulation
String manipulation Header file: <string.h> char *strcpy( char *s1, const char *s2); Copies s2 into s1, the value of s1 is returned. char *strcat(char *s1, const char *s2); Append the string s2 to the string s1, the value of s1 is returned. characters and strings manipulation
7
characters and strings manipulation
String manipulation int strcmp(const char *s1, const char *s2); Compares s1 with s2, returns a value of zero, less than zero or greater than zero if s1 is equal to, less than or greater than s2, respectively. int strlen(const char *s); Determines the lengthof string s. characters and strings manipulation
8
characters and strings manipulation
Lexical Analyzer #include <iostream.h> #include <string.h> void main() { char line[100], lexeme[20], ch; strcpy(lexeme,""); //lexeme=nothing strcpy(line,""); //line=nothing cout<<"enter a sentence? "; cin.getline(line, 100); int i=0; //line counter int k=0; //lexeme counter ch=line[i]; characters and strings manipulation
9
characters and strings manipulation
while ( i<=strlen(line)) { if (ch==' '|| ch==';'|| ch=='\0') lexeme[k]='\0'; cout<<lexeme<<endl; strcpy(lexeme,""); k=0; //reset lexeme i++; } else lexeme[k]=ch; k++; //add ch to lexeme ch=line[i]; characters and strings manipulation
10
characters and strings manipulation
Structure Definition Structures are user defined data types built using elements of other types. struct student { long number; char *name; int mark[3]; float average; }; characters and strings manipulation
11
characters and strings manipulation
Structure Definition Structures variables are declared like variables of other types: student s, y; //one structure student std[10]; //array of structures student *studentptr; //point to a structure characters and strings manipulation
12
Accessing Members of Structures
Members of a structure (or of a class) are accessed using the member access operator - or dot operator - (.), the arrow operator (->). The dot operator accesses a structure member via the variable name for the object. cin>>s.name; cout<<std[1].average; s.average=(s.mark[0]+s.mark[1]+s.mark[2])/3; characters and strings manipulation
13
Accessing Members of Structures
The arrow operator accesses a structure member via a pointer to the object. cin>>studentptr->name; cout<<studentptr->average; characters and strings manipulation
14
characters and strings manipulation
Example Example: Write C++ program reads in the name, number and average of 10 students using array of structures then prints this list. The program contains 2 functions for reading and printing. characters and strings manipulation
15
characters and strings manipulation
#include <iostream.h> #include <string.h> struct student { char name[20]; long number; float average; }; void reading(student *, const int); void printing(student *, const int); int main() const int size=100; student stud[size]; reading(stud, size); printing(stud, size); return 0; } characters and strings manipulation
16
characters and strings manipulation
void reading(student *s, const int size) { for (int i=0; i<=size-1; i++) cout<<"Enter Name,Number and Average\n"; cin>>s[i].name>>s[i].number>>s[i].average; } void printing(student *s, const int size) cout<<s[i].name<<"\t"<<s[i].number<<"\t“ <<s[i].average<<endl; characters and strings manipulation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.