1 / 27

The 3 rd lecture Jiří Šebesta

Computers and programming. The 3 rd lecture Jiří Šebesta. TOPIC. Iteration statements II. Jump statements String terminated by NULL Functions stdio.h for characters and strings Library string.h for strings. w hile. Iteration statements II. ( 1 / 4 ). while ( test ) statement ;.

Download Presentation

The 3 rd lecture Jiří Šebesta

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computers and programming The 3rd lecture Jiří Šebesta

  2. TOPIC Iteration statements II. Jump statements String terminated by NULL Functions stdio.h for characters and strings Librarystring.hfor strings

  3. while Iteration statements II. (1/4) while(test) statement; float temp[31]={-1.1,-0.3,-7.6,-2.7,-1.2,-2.6,-3.3, … … 2.4};// day temperatures in January unsigned int day = 1; while(temp[day-1]<=0)// pass the loop while temp<=0 day++; printf("%d. January temperature ovecomes 0, it was %3.1f deg. C!\n", day, temp[day-1]); return 0; Meaning of while is identical withfor (initializationand increment outside of the head) Example: Ex23.c

  4. while Iteration statements II. (2/4)

  5. do - while Iteration statements II. (3/4) dostatement; while(test); int code; // number for password do { printf("Insert access code:\n"); scanf("%d", &code);//number from stdin to variable code } while(code != 12345);// test of proper password 12345 printf("\n\nAccess allowed"); printf("\n\nInsert <space>+ENTER for exit"); do//exit after <space> insertion c=getchar(); while (c!=''); return 0; Example: Ex24.c

  6. do - while Iteration statements II. (4/4)

  7. continue, break Jump statements (1/3) Utilization: in bodies of loops (for, do, while); in bodyof switch (switch). • Interrupting a performed iteration cycle: • break: leaving the whole loop • continue: continuing by the next iteration

  8. break Jump statements (2/3) char c; intcode, test, n=3;// n is the number of trials do { printf("Insert access code:\n"); scanf("%d", &code);//number from stdin to variable code n--; //decrementation of the number of trials test=(code == 12345);// test of proper password ”12345” if ((test==0)&&(n==0)) break;// if no proper // code and 3 trials done - acces denied } while(!test); // in test is info about access,// 0 means denied, 1 means allowed Example: Ex25.c

  9. continue • Example: divider searching Jump statements (3/3) int num;//input number int test, n, m=0; printf("Input number:"); scanf("%d", &num);//number from stdin to variable num for(n=2; n<=100; n++)//loop for n from 2 to 100 { test = (num%n == 0);//test if n is divider if(!test) continue;//if not, next n printf(“\n%d", n);//if yes, print n m++; //number of dividers – incr. } printf(“Number of dividers: %d\n", m); Example: Ex26.c

  10. Strings terminated bynull (1/8) • NTS (Null Terminated Strings) : the last character of stringnull – spec. notation v C/C++’\0’ • Array elements:pointers to characters String: an array of characters(in memory: 1 byte/character – ASCII coding)

  11. ASCII codes in range 0 - 127 worldwide standardized • ASCII codes in range 128 – 255 (unsigned) depend on actual character set (national characters, UTF-8, Windows-1250, ISO-8859-x) – Extended ASCII Strings terminated bynull (2/8) String as an array of ASCII codes of charactersin memory:

  12. Ex . All characters printing including Extended ASCII Strings terminated bynull (3/8) int main( void) { unsigned char c; printf("Actual character set: \n\n"); for(c=32; c<=255 && c>=32; c++) printf("\n ASCII code %d = character %c", c, c); scanf("%c", &c); return 0; } Example: Ex27.c

  13. Strings terminated bynull (4/8) • char strA[5] = {‘A’,’B’,’C’,’D’,’\0’}; • 4 characters +terminating characternull • char strB[5] = ”ABCD”; • 4 characters, nullinserted automatically – the same principle asstring in the funtionprintf() Declaration methods: • char *strC = ”ABCD”; • -pointer to the first character, the first character points to the second character etc. up tonull(this declaration is not recommended – potential conflict in memory)

  14. Strings terminated bynull (5/8) \b - backspace BS \f - form feed FF (also clear screen) \n - new line NL \r - carriage return CR \t - horizontal tab HT \v - vertical tab (not all versions) \“ - double quotes (not all versions) \' - single quote character ' \\ - backslash character \ \ddd - character ddd, where ddd is an ASCII code given in octal base \xhhh - character hhh, where hhh is an ASCII code given in hexadecimal base Special characters:

  15. Strings terminated bynull (6/8) #include<stdio.h> int main(void) { char text[] = "Vjku\"oguucig\"ku\"ugetgv#"; unsigned int n; for(n=0; text[n]!='\0'; n++)// all chars in string if(text[n]!='') // excluding space text[n] -= 2;// character code shift printf("%s\n", text); scanf("%c", &c); return 0; }  Ex. Arithmetic manipulation with characters Example: Ex28.c

  16. Strings terminated bynull (7/8) #include"stdafx.h" #include"stdio.h" #include"string.h"//library for ops. with strings int main(void) { char s_inp[6] = {'a','b','c','d','e','\0'}; // or char s_inp[6] = "abcde"; char s_out[6], c; strcpy(s_out, s_inp);//copying of string, notpossibles_out = s_inp; printf("%s\n", s_out); scanf("%c", &c); return 0; } Copying of strings(usingstrcpyformstring.h) Example: Ex29.c

  17. Strings terminated bynull (8/8) char S[7], T[7], c; char R[7] = "ABCDEF"; int n; for( n=0; n<6; n++) // last -> first S[5-n] = R[n];//or S[5-n] = *(R+n); S[6] = '\0'; printf("%s\n", S); for( n=0; n<6; n++) // capital -> small T[n] = R[n]+32;//or *(T+n) = *(R+n)+32; T[n] = '\0'; printf("%s\n", T); scanf("%c", &c); Changeover of text, small letter => capital letter Example: Ex30.c

  18. Funct.stdio.hfor chars and strings (1/3) int main(void) { char c,d; int n; puts("Is C/C++ …"); // unformated printing of string to stdout followed by new line c=getchar();// get char.from the standard input if (c=='y') puts("Right, ..."); if (c=='n') puts("I disagree,..."); doc=getchar(); while(c!=' '); // wait for space return 0; } Functionsputs() andgetchar() Example: Ex31.c

  19. Funct.stdio.hfor chars and strings (2/3) int main(void) { char c; for(c='A';c<='Z';c++)// loop for all alphabet { putchar(c); // printing character to thestdout - capital putchar(c+32); // small putchar('\n'); // new line } do c=getchar(); while(c!=''); // wait for space return 0; } Functionputchar() Example: Ex32.c

  20. Funct.stdio.hfor chars and strings (3/3) int main(void) { char fname[20], sname[20], c; printf("Insert your first name:"); gets(fname);// reading characters from stdin and stores them as a string - first name printf("\nInsert your surname:"); gets(sname);// reading surname printf("\nYour whole name is: %s%s", fname,sname); scanf("%c", &c); return 0; } Functiongets() Example: Ex33.c

  21. Librarystring.hfor strings (1/4) char S[20], T[20], c; char R[20] = "ABCDEF"; int n; for(n=0; n<strlen(R); n++) // last -> first S[strlen(R)-n-1] = R[n]; S[n] = '\0'; printf("%s\n", S); for( n=0; n<strlen(R); n++) // cap. -> small T[n] = R[n]+32; T[n] = '\0'; printf("%s\n", T); scanf("%c", &c); Length of string – strlen() Example: Ex34.c

  22. Librarystring.hfor strings (2/4) char final[30], c; char first[] = "wine"; char second[] = "women"; char third[] = "songs"; strcpy(final, first); strcat(final, second);// appends a copy of the source string to the destination string strcat(final, third); printf("%s\n", final); scanf("%c", &c); return 0; Concatenation of strings – strcat() Example: Ex35.c

  23. Librarystring.hfor strings (3/4) char number[11], c; char *ptr, digit = '8'; strcpy(number,"1487265098"); printf("Original string: %s\n", number); ptr = strchr(number,digit); while( ptr) { *ptr = 'x'; ptr = strchr(number, digit); } printf("Modified string: %s", number); scanf("%c", &c); Character searching in string – strchr() Example: Ex36.c

  24. Librarystring.hfor strings (4/4) chartxt1[51], txt2[5],txt3[5]="----",*ptr, c; int n; printf("Insert text1:"); gets(txt1); printf("Insert text2 (searched):"); gets(txt2); n=strlen(txt2); ptr = strstr(txt1, txt2); while( ptr) { strncpy(ptr,txt3,n); ptr = strstr(txt1, txt2); } printf("Modified string: %s", txt1); String searching in string– strstr()+strncpy() Example: Ex37.c

  25. Strings – examples (1/2) char num[30] = "12;3.8;100;94.5;33;44;", c; int n, total=0, rat=0; printf("Numbers: %s\n\n", num); for(n=0; n<strlen(num); n++) { if(num[n] == ';') // total number of num. total++; if(num[n] == '.') // number of rational n. rat++; } printf("Total number of num.: %4d\n", total); printf("Integer numbers: %4d\n", total-rat); scanf("%c", &c); In the variable num, unknown number of integer and rational numbers is stored. Each number is ended by semicolon. Compute the total number of numbers and the number of integer numbers in the string . Example: Ex38.c

  26. Strings – examples (2/2) char num[30] ="12;4;100;95;33;44;"; char part[10], c; int m=0, n; printf("Numbers: %s\n\n", num); for(n=0; n<strlen(num);n++) if(num[n] != ';') part[m++] = num[n]; else { part[m] = '\0'; printf("%6s\n", part); m = 0; } In the string num, unknown number of integer numbers is stored. Each number is ended by semicolon. Print the numbers to the column and justify them properly. Example: Ex39.c

  27. TOPIC OF THE NEXT LECTURE Pointers and arrays THANK YOUFOR YOUR ATTENTION

More Related