computers and programming the 3 rd lecture jiří Šebesta
Post on 27-Dec-2015
226 Views
Preview:
TRANSCRIPT
Computers and programming
The 3rd lecture
Jiří Šebesta
TOPIC
1. Iteration statements II.2. Jump statements3. String terminated by NULL4. Functions stdio.h for characters and strings 5. Library string.h for strings
Iteration statements II. (1/4)
while(test) statement;
• while
• Meaning of while is identical with for (initialization and increment outside of the head)
Example: Ex23.c
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;
Iteration statements II. (2/4)
• while
Iteration statements II. (3/4)
do statement; while(test);
• do - while
Example: Ex24.c
int code; // number for passworddo{
printf("Insert access code:\n");scanf("%d", &code); //number from stdin to
variable code}while(code != 12345); // test of proper password 12345printf("\n\nAccess allowed");printf("\n\nInsert <space>+ENTER for exit");do //exit after <space> insertion
c=getchar();while (c!=' ');return 0;
Iteration statements II. (4/4)
• do - while
Jump statements (1/3)
• Utilization:
– in bodies of loops (for, do, while);– in body of switch (switch).
• Interrupting a performed iteration cycle:– break: leaving the whole loop– continue: continuing by the next iteration
• continue, break
Jump statements (2/3)
• break
Example: Ex25.c
char c; int code, test, n=3; // n is the number of trialsdo{
printf("Insert access code:\n");scanf("%d", &code); //number from stdin to
variable coden--; //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
Jump statements (3/3)
• continue
Example: divider searching
Example: Ex26.c
int num; //input numberint test, n, m=0;printf("Input number:");scanf("%d", &num); //number from stdin to variable numfor(n=2; n<=100; n++) //loop for n from 2 to 100{
test = (num%n == 0); //test if n is dividerif(!test)
continue; //if not, next nprintf(“\n%d", n); //if yes, print nm++; //number of dividers – incr.
}printf(“Number of dividers: %d\n", m);
Strings terminated by null (1/8)
• String: an array of characters (in memory: 1 byte/character – ASCII coding)
• NTS (Null Terminated Strings) : the last character of string null – spec. notation v C/C++ ’\0’
• Array elements: pointers to characters
Strings terminated by null (2/8)
• String as an array of ASCII codes of characters in memory:
• 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 by null (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;}
Ex . All characters printing including Extended ASCII
Example: Ex27.c
Strings terminated by null (4/8)
• Declaration methods:• char strA[5] = {‘A’,’B’,’C’,’D’,’\0’};
– 4 characters + terminating character null
• char strB[5] = ”ABCD”;– 4 characters, null inserted automatically – the same principle as string in the funtion printf()
• char *strC = ”ABCD”;- pointer to the first character, the first character points to the second character etc. up to null (this declaration is not recommended – potential conflict in memory)
Strings terminated by null (5/8)
• Special characters:\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
Strings terminated by null (6/8)
Example: Ex28.c
• Ex. Arithmetic manipulation with characters
#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;}
Strings terminated by null (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,
not possible s_out = s_inp; printf("%s\n", s_out); scanf("%c", &c); return 0;}
• Copying of strings (using strcpy form string.h)
Example: Ex29.c
Strings terminated by null (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
Funct. stdio.h for 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, ..."); do c=getchar(); while(c!=' '); // wait for space return 0;}
• Functions puts() and getchar()
Example: Ex31.c
Funct. stdio.h for 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 the stdout - capital
putchar(c+32); // smallputchar('\n'); // new line
}
do c=getchar(); while(c!=' '); // wait for space return 0;}
• Function putchar()
Example: Ex32.c
Funct. stdio.h for 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;}
• Function gets()
Example: Ex33.c
Library string.h for 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
Library string.h for 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
Library string.h for 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
Library string.h for strings (4/4)
char txt1[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
Strings – examples (1/2)
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 .
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);
Example: Ex38.c
Strings – examples (2/2)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.
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; }
Example: Ex39.c
TOPIC OF THE NEXT LECTURE
Pointers and arrays
THANK YOU FOR YOUR ATTENTION
top related