# SM 514 Introduction to Software Testing

## Transkript

SM 514 Introduction to Software Testing
```Strings
 Strings are multiple characters in a certain order
 “my car”
 C allows variable types for single characters, but not for strings
 char type arrays are used to represent strings
 Strings in C are terminated by the null character, „\0‟
 So the string “my car”
 Notice that space is counted as a character
 Need to add the null character to the end of the string
 Minimum length of array needed is 7 (including the null character)
 What is the minimum length of an array to store “Please enter three integers” ?
28
CENG 114 – Spring 2009-10
127
Declaring and Initializing Strings
 Strings are declared as arrays of characters
char FirstName[15];
char LastName[25];
 Strings can also be defined as constants
#define ERR1
“Date is wrong”
#define ERR2
“Cannot Leave Name Field Blank”
 Strings can be initialized when they are declared
char
char
char
char
FirstName[15] = {S, a, d, i, k};
LastName[25] = {E, s, m, e, l, i, o, g, l, u};
LastName[25] = “Esmelioglu”;
CENG 114 – Spring 2009-10
128
64
Strings in Memory
Problem
 char x[15] = “John Smith”
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
x[8]
x[9]
x[10]
x[11]
x[12]
x[13]
x[14]
w
y
z
7
0
0
0
J
7
0
0
1
o
7
0
0
2
h
7
0
0
3
n
7
0
0
4
7
0
0
5
S
7
0
0
6
m
7
0
0
7
i
7
0
0
8
t
7
0
0
9
h
7
0
1
0
\0
7
0
1
1
?
7
0
1
2
?
7
0
1
3
?
7
0
1
4
?
7
0
1
5
15
7
0
1
6
-1
7
0
1
7
23
 char x[15] = “John Adam Smith”
x[0]
x[1]
x[2]
x[3]
x[4]
x[5]
x[6]
x[7]
x[8]
x[9]
x[10]
x[11]
x[12]
x[13]
x[14]
w
y
z
7
0
0
0
J
7
0
0
1
o
7
0
0
2
h
7
0
0
3
n
7
0
0
4
7
0
0
5
A
7
0
0
6
d
7
0
0
7
a
7
0
0
8
m
7
0
0
9
7
0
1
0
S
7
0
1
1
m
7
0
1
2
i
7
0
1
3
t
7
0
1
4
h
7
0
1
5
\0
7
0
1
6
-1
7
0
1
7
23
 Length of the string should be one less than the array size
CENG 114 – Spring 2009-10
129
Arrays of Strings
 Strings are arrays of characters  single dimensional arrays
 Lists are arrays of strings  two dimensional arrays
 Players
 Ahmet
 Hasan
 Naciye
 Ayse
 Nuri
 This list can be represented as a two dimensional array (5 rows, ? columns)
char Players[5][7] = {“Ahmet”, “Hasan”, “Naciye”, “Ayse”, “Nuri”}
CENG 114 – Spring 2009-10
130
65
Storing in Memory
Player[0]
&Player[0][0]
Player
Player[0]
Player[1]
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
A
h
m
e
t
\0
H
a
s
a
n
\0
Player[2]
Player[3]
All have a
value of
7000
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
N
a
c
i
y
e
\0
A
y
s
e
\0
CENG 114 – Spring 2009-10
Player[4]
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
N
u
r
i
\0
131
Printing Strings
 Use printf and scanf
 %s is the placeholder for strings
 example
printf(“%s”, “This is a book”);
prints
>This is a boo k
printf(“%s*%s”, “123456”, “123”);
prints
>123456*123
printf(“%8s*%5s”, “12345”, “123”);
prints
>
12345* 12 3
CENG 114 – Spring 2009-10
132
66
Printing Strings
 Normally strings are right justified, but a minus sign after the % sign makes the
string left justified
printf(“%s*%s”, “123456”, “123”);
prints
> 1 23 45 6* 12 3
printf(“%5s\n%5s\n%5s\n”, “Ana”, “Baba”, “Cocuk”);
prints
> A na
> Ba ba
> C oc uk
printf(“%-5s\n%-5s\n%-5s\n”, “Ana”, “Baba”, “Cocuk”);
prints
> A na
> B ab a
> C oc uk
CENG 114 – Spring 2009-10
133
Printing Arrays of Strings
#include <stdio.h>
int main(void)
{
int i;
char Players[5][7] = {"Ahmet", "Hasan", "Naciye", "Ayse", "Nuri"};
printf ( "%s\n", Players);
printf ( "%s\n", Players[0]);
printf ( "%c\n", Players[0][0]);
for(i=0;i<=4;i++) printf("%s\n", Players[i]);
return(0);
}
CENG 114 – Spring 2009-10
134
67
 Reading strings with scanf works the same way as
 scanning for numbers begin with a numeric character
(or decimal point) and ends when a non-numeric
character is encountered.
 scanning for strings starts after the first white space
character and ends when a white space character is
reached (white space characters are spaces, tabs,
newlines, …)
 a null character (\0) is placed after the last character is
CENG 114 – Spring 2009-10
char x[8];
scanf(“%s”, x);
user enters
>John
>
or user enters
>
>
135
or user enters
John
>
>
> John
>
The entry is stored in memory as shown:
x[0] x[1] x[2] x[3] x[4] x[5] x[6] x[7]
J
o
h
n
\0
CENG 114 – Spring 2009-10
136
68
char
int
char
int
c [5];
n;
d [5];
t;
scanf(“%s %d %s %d”, c, &n, d, &t);
user enters
>CENG
>
114
Thu
240
or user enters
>CENG
>
114
>
240
>
Thu
The entries are stored in memory as shown:
Var
Val
Binary
c[0]
7000
C
01000011
c[1]
7001
E
01000101
c[2]
7002
N
01001110
c[3]
7003
G
01000111
c[4]
7004
\0
00000000
n
7005
114
01110010
d[0]
7006
T
01010100
d[1]
7007
h
01101000
d[2]
7008
u
01110101
d[3]
7009
\0
00000000
d[4]
7010
???
?Unknown?
t
7011
240
11110000
CENG 114 – Spring 2009-10
137
Example
#include <stdio.h>
#define NUM_STD100
#define NM_LEN15
/* Maximum number of students */
/* Maximum lenght of each name */
int main(void)
{
char name[NUM_STD][NM_LEN];
int
int
i = 0;
int
num_std = 0;
char ans = 'y';
char junk;
/* Array of student names */
/* Array of student grades */
/* Loop index */
/* Actual num of students read */
/* if equals 'y' continue reading */
CENG 114 – Spring 2009-10
138
69
Example
while(ans == 'y') {
i++;
printf("\nAre there more students? [y/n]: ");
scanf("%c%c%c", &junk, &ans, &junk);
}
num_std = i;
for(i = 0; i < num_std; i++)
return(0);
}
CENG 114 – Spring 2009-10
139
String Library Functions
 #include <string.h>
 strlen return the length of string not counting \0
 strcopy copies string from source to dest
 strncopy copies n chars from source to dest
 strcat appends string from source to end of dest
 strncat appends n chars from source to end of dest
 strcmp compares two strings alphabetically
 strncmp compares the first n chars of two strings
 strtok breaks string into tokens using delimiters
CENG 114 – Spring 2009-10
140
70
```

Detaylı

Detaylı

### Yenilenebilir Enerji

Proje / Projects İmalat / Manufacturing İnşaat / Construction Enerji / Energy Montaj / installation

Detaylı

Detaylı

Detaylı