ECE Application Programming

Download Report

Transcript ECE Application Programming

16.216
ECE Application Programming
Instructor: Dr. Michael Geiger
Fall 2014
Lecture 22
More string examples
Lecture outline

Announcements/reminders



Program 6 due 10/30
Program 7 posted; due 11/10
Exam 2: Wednesday, 11/5


Midterm grades posted



Weighted avg. based on 1st 2 programs (60%), Exam 1 (40%)
SAT (70+), CAU (60-69), FAI (<60), NA (0)
Review


Allowed one double-sided 8.5” x 11” sheet of notes
Character arrays and strings
Today’s lecture

7/8/2015
More string examples
ECE Application Programming: Lecture 22
2
Review Example: Strings

strncat(s1,s2,10);
n1 = strlen(s1);
printf(“s1 = %s\n”, s1);
printf(“Length of s1 = %d\n\n”,
n1);
What does the following program print?
int main() {
char s1[15];
int n1;
char s2[10] = “.216”;
int n;
// Assume user inputs: ABC ABD
printf(“Enter two strings:”);
scanf(“%s%s”, s1, s2);
n = strncmp(s1, s2, 15);
strncpy(s1, “16”, 15);
n1 = strlen(s1);
printf(“s1 = %s\n”, s1);
printf(“Length of s1 = %d\n\n”,
n1);
if (n > 0)
printf(“%s > %s\n”, s1, s2);
else if (n < 0)
printf(“%s < %s\n”, s1, s2);
else
printf(“%s == %s\n”, s1, s2);
return 0;
printf(“%c\n\n”, s1[1]);
}
7/8/2015
ECE Application Programming: Lecture 22
3
Example solution
s1 = 16
Length of s1 = 2
Initial value of s1
6
s1[1]
s1 = 16.216
Length of s1 = 6
s1 after strncat()
Enter two strings: ABC ABD
ABC < ABD
Result of strncmp()
7/8/2015
ECE Application Programming: Lecture 22
4
Example: Using string functions



Works with main program in PE
Assume input strings have max of 49 chars (+ ‘\0’)
Write a function to do each of the following:

int readStrings(char *s);


void copyNull(char *s1, char *s2, int n);


Repeatedly read strings from standard input until the input string
matches s. Return the number of strings read.
Copy the first n characters of s2 into s1, and make sure that the
new version of s1 terminates with a null character.
int fillString(char *s, int n);

Repeatedly read strings from standard input and concatenate them
to s until there is no room in the string. Return the final length of
the string.


7/8/2015
For example, if s is a 6-character array already holding “abcd”:

User enters “e”—string is full; return 5

User enters “ef”—there’s not enough room; return 4
Assume s initially contains null terminated string (or is empty)
ECE Application Programming: Lecture 22
5
Example solution
int readStrings(char *s) {
char str[50];
// Assume max 50 chars
int count = 0;
do {
scanf(“%s”, str); // NOTE: do not
// need &str
count++;
} while (strcmp(str, s) != 0);
return count;
}
7/8/2015
ECE Application Programming: Lecture 22
6
Example solution (cont.)
void copyNull(char *s1, char *s2, int n) {
strncpy(s1, s2, n);
s1[n] = ‘\0’;
}
7/8/2015
ECE Application Programming: Lecture 22
7
Example solution (cont.)
int fillString(char *s, int n) {
char input[50];
// Assume max
//
50 chars
int charsLeft;
// Space remaining
//
in s
do {
scanf(“%s”, input);
// Calculate # chars left in array if input
//
string is added. Need to leave room for ‘\0’
charsLeft = n – (strlen(s) + 1) – strlen(input);
if (charsLeft > 0)
strcat(s, input);
// Enough space to add this string
//
and continue
else {
if (charsLeft == 0)
strcat(s, input);
return strlen(s);
}
} while (1);
// Out of room
// Can add input, but then out of room
}
7/8/2015
ECE Application Programming: Lecture 22
8
Final notes

Next time


File I/O
Reminders:



Program 6 due 10/30
Program 7 posted; due 11/10
Exam 2: Wednesday, 11/5


Midterm grades posted


7/8/2015
Allowed one double-sided 8.5” x 11” sheet of notes
Weighted avg. based on 1st 2 programs (60%), Exam 1
(40%)
SAT (70+), CAU (60-69), FAI (<60), NA (0)
ECE Application Programming: Lecture 22
9