exercise 7 strings. an array of characters used to store text another way to initialize: char a[...
Post on 21-Dec-2015
229 views
TRANSCRIPT
Exercise 7
Strings
Strings
An array of characters Used to store text Another way to initialize:
char A[ ]=“blabla”;
The Terminator
Strings terminate with NULL character, signed by ‘\0’ (ascii code 0)
This is a convention used to know where the string ends
It means that in order to hold a string of 7 chars we need an array of length 8
So the previous initialization is equivalent to
char A[ ] = {‘b’, ‘l’, ‘a’, ‘b’, ‘l’, ‘a’, ‘\0’};
The fool on the null
#include <stdio.h>
int main(void){ char str[]="I'm a full string"; printf("%s\n", str); str[7]='o'; str[8]='o'; printf("%s\n", str); str[11]='\0'; printf("%s\n", str); str[11] = ‘s’; printf("%s\n", str);
return 0;}
Reading-in strings
There are several ways of accepting strings as input from the user
The obvious way to go is read character by character using getchar()
Example
Read_String_with_getchar.c
Reading-in strings: scanf
A simpler way is to use scanf To read in a string to a variable str,
write : scanf(“%s”, str); Note there’s no ‘&’ sign!!!
Reading-in strings - scanf
scanf reads-in letters until a space or newline (‘\n’) is encountered
The maximum length can be stated in the parentheses: scanf(“%10s”, str); This will read in 10 letters, plus the ‘\0’
sign (so str should have place for 11 characters)
Example
Read_String_with_scanf.c
Comparing strings
We cannot just compare strings’ contents by == char A[7]=“Hello”; char B[7]=“Hello”;
if (A==B) {
... } Because A and B are addresses of A[0] and B[0]
A==B only if A and B are the same string in memory In order to compare the contents we must scan char by char
‘H’
‘e’
‘l’ ‘l’ ‘o’
‘\0’
….
B
….
….
‘H’
‘e’
‘l’ ‘l’ ‘o’
‘\0’
….
A
….
….
Example
Compare.c
Compare – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
0
i
‘Y’ ‘e’ ‘w’ ‘\0’
A
Compare – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
0
i
‘Y’ ‘e’ ‘w’ ‘\0’
A
Compare – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
1
i
‘Y’ ‘e’ ‘w’ ‘\0’
A
Compare – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
1
i
‘Y’ ‘e’ ‘w’ ‘\0’
A
Compare – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
2
i
‘Y’ ‘e’ ‘w’ ‘\0’
A
Compare – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
2
i
‘Y’ ‘e’ ‘w’ ‘\0’
A
Compare – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
2
i
‘Y’ ‘e’ ‘w’ ‘\0’
A
Compare – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
2
i
‘Y’ ‘e’ ‘w’ ‘\0’
A
Equal strings – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
0
i
‘Y’ ‘e’ ‘s’ ‘\0’
A
Equal strings – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
0
i
‘Y’ ‘e’ ‘s’ ‘\0’
A
Equal strings – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
1
i
‘Y’ ‘e’ ‘s’ ‘\0’
A
Equal strings – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
1
i
‘Y’ ‘e’ ‘s’ ‘\0’
A
Equal strings – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
2
i
‘Y’ ‘e’ ‘s’ ‘\0’
A
Equal strings – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
2
i
‘Y’ ‘e’ ‘s’ ‘\0’
A
Equal strings – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
3
i
‘Y’ ‘e’ ‘s’ ‘\0’
A
Equal strings – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
3
i
‘Y’ ‘e’ ‘s’ ‘\0’
A
Different length – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
0
i
‘Y’ ‘e’ ‘\0’
A
Different length – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
0
i
‘Y’ ‘e’ ‘\0’
A
Different length – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
1
i
‘Y’ ‘e’ ‘\0’
A
Different length – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
1
i
‘Y’ ‘e’ ‘\0’
A
Different length – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
2
i
‘Y’ ‘e’ ‘\0’
A
Different length – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
1
i
‘Y’ ‘e’ ‘\0’
A
Different length – step by step
for(i=0; A[i]!='\0' || B[i]!='\0'; i++) if(A[i]!=B[i])
{ printf("A is different from B!\n"); return 0; } printf("A and B are the same!\n"); return 0;
B
‘Y’ ‘e’ ‘s’ ‘\0’
1
i
‘Y’ ‘e’ ‘\0’
A
String library
Like in the case of stdio.h and math.h, we have a special library for handling strings
We should #include string.h Functions:
strlen(s) – returns the length of s strcmp(s1, s2) – compares s1 with s2 strcpy(s1, s2) – copies to contents of s2 to
s1 and more…
Example
easy_compare.c
If there’s time…
Exercise: write a program that: gets an input string from the user (up to 100
chars, no white spaces) gets 2 characters from the user replaces each occurrence of the first character
in the string by the second character, and prints the result.
Example: input: “papa”, ‘p’, ‘m’ output: “mama”
Solution
replace.c
If there’s time…
Exercise: write a function: void my_to_lower(char str[]); The function receives an arbitrary string and
converts all its letters to lower-case letters
Demonstrate the use of your function by some example that prints the input and output on the screen