Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Fundamental

Similar presentations


Presentation on theme: "Programming Fundamental"— Presentation transcript:

1 Programming Fundamental
Instructor Name: Muhammad Safyan Lecture-18

2 Lecture outline String handling Character handling

3 String Handling

4 String Handling Initializing String string month = "March";
string text( "Hello" ); #include<iostream> #include<conio.h> using namespace std; main ( ) { string month = "March"; string text( "Hello" ); cout<< month<<endl; cout<<text; getch(); }

5 String Assignment string string1( "cat" ); string string2;
string2 = string1; // assign string1 to string2 string3.assign( string1 ); // assign string1 to string3 string2[ 0 ] = string3[ 2 ] = 'r'; #include<iostream> #include<conio.h> using namespace std; main ( ) { string string1( "cat" ); string string2; string string3; string2 = string1; // assign string1 to string2 cout<<string2; string3.assign( string1 ); // assign string1 to string3 string2[ 0 ] = string3[ 2 ] = 'r'; cout<<string3; getch(); }

6 String Concatenation // demonstrating member function at
for ( int i = 0; i < string3.length(); i++ ) cout << string3.at( i ); string a("gc university lahore"); string b=a+" Pakistan"; b.append(" Asia"); string string5; string5.append( a, 4, a.length() - 4 ); string a("gc university lahore"); for(int i=0; i<a.length(); i++) cout<<a.at(i);

7 Substrings string a("gc university lahore");
cout<<a.substr(2,7);

8 Swapping strings string a("gc university lahore");
string b("paksitan"); a.swap(b); cout<<a<<endl; cout<<b;

9 Erase ,Find and Insert string a="department of computer science";
cout<<a.erase(2); cout<<a.erase(2,5); int position = a.find(" "); cout<<position; string1.insert( 10, string2 ); #include<iostream> #include<conio.h> using namespace std; main ( ) { string a="safyan"; string string1="department of computer science"; string1.insert( 10, a); cout<<endl<<string1; getch(); }

10 #include<ctype.h>
Header File ctype.h #include<ctype.h> Storage Requirements for Text and Binary Modes Storage Requirements for Text and Binary Modes for an Integer: for an Integer: Page 32 Page 32 The fwrite() Function The fwrite() Function size_t fwrite (void *ptr, size_t size, size_t nmemb, size_t fwrite (void *ptr, size_t size, size_t nmemb, FILE *fp); FILE *fp); fwrite() writes, from the address pointed to by ptr, fwrite() writes, from the address pointed to by ptr, up to nmemb elements whose size is specified by up to nmemb elements whose size is specified by size, to the file pointed by fp size, to the file pointed by fp fwrite() returns the number of elements fwrite() returns the number of elements successfully written, which will be less than nmemb successfully written, which will be less than nmemb only if a write error is encountered. only if a write error is encountered. double earnings[10]; double earnings[10]; fwrite(earnings,sizeof(double),10,fp); fwrite(earnings,sizeof(double),10,fp); 32 32 Page 33 Page 33 The fread() Function The fread() Function size_t fread(void *ptr, size_t size, size_t nmemb, size_t fread(void *ptr, size_t size, size_t nmemb, FILE *fp); FILE *fp); fread() reads, into the array pointed to by ptr, up to fread() reads, into the array pointed to by ptr, up to nmemb elements whose size is specified by size, nmemb elements whose size is specified by size, form the file pointed to by fp. form the file pointed to by fp. fread() returns the number of elements fread() returns the number of elements successfully read, which may be less than nmemb successfully read, which may be less than nmemb is a read error or end-of-file is encountered. is a read error or end-of-file is encountered. 33 33 Page 34 Page 34 Writing file data using block I/O functions Writing file data using block I/O functions #include <stdio.h> #include <stdio.h> typedef struct { typedef struct { char name[20]; char name[20]; int serial_code; int serial_code; int amount; int amount; float float cost; cost; } component; } component; main(void) main(void) { component comp_data; { component comp_data; FILE FILE *fp; *fp; char char filename[80]; filename[80]; int numofcomp; int numofcomp; int i; int i; printf("Enter the file name: "); printf("Enter the file name: "); gets(filename); gets(filename); 34 34 Page 35 Page 35 if ((fp = fopen(filename, "wb")) == NULL) { if ((fp = fopen(filename, "wb")) == NULL) { printf("can't open file \n"); printf("can't open file \n"); exit(1); exit(1); }printf("Enter the number of components: "); }printf("Enter the number of components: "); scanf("%d", &numofcomp); scanf("%d", &numofcomp); for (i=0; i<numofcomp; i++) { for (i=0; i<numofcomp; i++) { printf("Name of the component: "); printf("Name of the component: "); gets(comp_data.name); gets(comp_data.name); printf("Serial code of the component: "); printf("Serial code of the component: "); scanf("%d", &comp_data.serial_code); scanf("%d", &comp_data.serial_code); printf("Amount of the component: "); printf("Amount of the component: "); scanf("%d", &comp_data.amount); scanf("%d", &comp_data.amount); printf("Cost of each component: "); printf("Cost of each component: "); scanf("%f", &comp_data.cost); scanf("%f", &comp_data.cost); fwrite(&comp_data, sizeof(comp_data), 1, fp); fwrite(&comp_data, sizeof(comp_data), 1, fp); }fclose(fp); }fclose(fp); return 0; return 0; } } 35 35 Page 36 Page 36 12.5 Random Access 12.5 Random Access Random Access: fseek() and ftell() Random Access: fseek() and ftell() fseek, ftell: All the previous I/O functions do reading fseek, ftell: All the previous I/O functions do reading and writing sequentially, i.e. read the 1st datum, 2rd and writing sequentially, i.e. read the 1st datum, 2rd datum, 3rd datum ..., etc. datum, 3rd datum ..., etc. fseek fseek and and ftell ftell help doing help doing I/O in non-sequential manner, i.e. read the 10th I/O in non-sequential manner, i.e. read the 10th datum, then go back and read the 2nd datum. datum, then go back and read the 2nd datum. 36 36 Page 37 Page 37 File position pointer File position pointer The system keeps a file position pointer for The system keeps a file position pointer for each open file. each open file. It indicates the location in the file at It indicates the location in the file at which data will be read or written. which data will be read or written. file position marker file position marker ...... ...... Disk Disk 37 37 Page 38 Page 38 The fseek() Function The fseek() Function The function prototype of The function prototype of fseek() fseek() is is int fseek(FILE *fp, long int offset, int mode); int fseek(FILE *fp, long int offset, int mode); * * fseek() fseek() returns returns if OK, and if OK, and -1 -1 if there is an error. if there is an error. * * offset offset tells how far to move from the starting point tells how far to move from the starting point (depending on the mode). It can be +ve (move (depending on the mode). It can be +ve (move forward) or -ve (move backward) or 0. forward) or -ve (move backward) or 0. * * mode mode identifies the starting point. In stdio.h, the identifies the starting point. In stdio.h, the following constants can be assigned to mode: following constants can be assigned to mode: Mode Mode Measure offset from Measure offset from SEEK_SET SEEK_SET Beginning of file Beginning of file SEEK_CUR SEEK_CUR Current position Current position SEEK_END SEEK_END End of file End of file 38 38 Page 39 Page 39 The ftell() Function The ftell() Function The function prototype of The function prototype of ftell() ftell() is is long int ftell(FILE *fp); long int ftell(FILE *fp); returns the current file location. returns the current file location. The rewind() Function The rewind() Function The function prototype of The function prototype of rewind() rewind() is is void rewind(FILE *fp); void rewind(FILE *fp); Resets the file position marker to the beginning of Resets the file position marker to the beginning of the file. This is equivalent to the file. This is equivalent to fseek(fp, 0L, SEEK_SET); fseek(fp, 0L, SEEK_SET); 39 39 Page 40 Page 40 /* reverse.c - displays a file in reverse order */ /* reverse.c - displays a file in reverse order */ #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> #define CNTL_Z '\032' #define CNTL_Z '\032' /* eof marker in DOS textfiles */ /* eof marker in DOS textfiles */ { int main(int argc, char *argv[]) { int main(int argc, char *argv[]) char ch; char ch; FILE *fp; FILE *fp; long count, last; long count, last; if ((fp=fopen(argv[1],"rb"))== NULL) { if ((fp=fopen(argv[1],"rb"))== NULL) { printf("reverse can't open %s\n", argv[1]); printf("reverse can't open %s\n", argv[1]); exit(1); exit(1); } } fseek(fp,0L, SEEK_END); fseek(fp,0L, SEEK_END); /* goto eof */ /* goto eof */ last=ftell(fp); last=ftell(fp); for (count=1L; count<=last; count++) { for (count=1L; count<=last; count++) { fseek(fp,-count,SEEK_END); fseek(fp,-count,SEEK_END); ch=getc(fp); ch=getc(fp); if (ch!=CNTL_Z && ch!= '\r') if (ch!=CNTL_Z && ch!= '\r') putchar(ch); putchar(ch); }fclose(fp); }fclose(fp); return(0); return(0); 40 40 } }

11 Example #include<iostream> #include<conio.h>
using namespace std; main ( ) { int i ; char c ; for( i = 0; i < 256 ; i ++ ) c = i ; cout<<i <<"\t" << c <<endl ; } getch();

12 ctype Functions int isspace ( int c ) int isdigit ( int c )
int isalpha ( int c ) int isalnum ( int c ) int isxdigit ( int c ) int islower ( int c ) int isupper ( int c ) int tolower ( int c ) int toupper ( int c )

13 isdigit ( ) Function int isdigit ( int c ) ;

14 isalpha ( ) Function int isalpha ( int c ) ;

15 isalnum ( ) Function int isalnum ( int c ) ;

16 islower ( ) Function int islower ( int c ) ;

17 isupper ( ) Function int isupper ( int c ) ;

18 tolower ( ) Function int tolower ( int c ) ;

19 toupper ( ) Function int toupper ( int c ) ;

20 getchar ( ) ;

21 #include<string>
#include<conio.h> #include<iostream.h> using namespace std; //#include<ctype.h> main() { int lc=0; int uc=0; int dig=0; int ws=0; int pun=0; int oth=0; char c; cout<<"enter characters"; while((c=getchar())!='\n') if (islower(c)) lc ++ ; else if (isupper(c)) uc ++ ; else if (isdigit(c)) dig ++; else if (isspace(c)) ws ++ ; else if (ispunct(c)) pun ++ ; else oth ++ ; } cout<<"lower character"<<lc; getch();


Download ppt "Programming Fundamental"

Similar presentations


Ads by Google