Professional Documents
Culture Documents
_________
Character
Strings
1-1
Prog 9.1 (A): Concatenating Char Arrays
#include <stdio.h>
char s3[11];
int i;
printf ("\n");
return 0;
}
1-2
Prog 9.1 (B): Concatenating Char Arrays
1-3
Prog 9.2: Variable-Length Char Strings
// Count the number of characters in a VL string
#include <stdio.h>
return count;
}
return 0;
} 1-4
Prog 9.3 (A): Concat() with Variable-Length
Char Strings
#include <stdio.h>
char s3[20];
return 0;
}
1-5
Prog 9.3 (B): Concat() with Variable-Length
Char Strings
result [i + j] = '\0';
}
1-6
Prog 9.4 (A): Testing Char Strings for
Equality
#include <stdio.h>
#include <stdbool.h>
printf ("%i\n",
equalStrings (strA, strB) );
printf ("%i\n",
equalStrings (strA, strA) );
printf ("%i\n",
equalStrings (strB,
"string") );
return 0;
}
1-7
Prog 9.4 (B): Testing Char Strings for
Equality
int i = 0;
bool areEqual;
return areEqual;
}
1-8
Prog 9.5: Reading Strings with scanf
#include <stdio.h>
int main (void) {
return 0;
}
_________________________________________
Enter the three strings:
NAMAL Institute, Mianwali.
str1 = NAMAL
str2 = Institute,
str3 = Mianwali.
1-9
Prog 9.6 (A): Reading full lines of text, without scanf()
#include <stdio.h>
int main (void) {
int i; char line [81];
void readLine (char buffer[]);
readLine(line);
printf (“%s\n”, line);
return 0;
}
#include <stdio.h>
int main (void) {
return 0;
}
do {
c = getchar();
buffer [i++] = c;
}
while (c != ‘\n’);
#include <stdio.h>
int main (void) {
int i; char line [81];
void readLine (char buffer[]);
readLine (line);
printf (“%s\n”, line);
return 0;
}
int i, wordCount = 0;
bool lookingForNextWord = true;
if (alphabetic (string[i])) {
if (lookingForNextWord == true) {
++wordCount;
lookingForNextWord = false;
}
}
i++;
}
return wordCount;
} 1-14
Prog 9.7(B): Counting words in a String
int countWords (const char string[]) {
int i, wordCount = 0;
bool lookingForNextWord = true;
if (alphabetic (string[i])) {
if (lookingForNextWord == true) {
++wordCount;
lookingForNextWord = false;
}
}
i++;
}
return wordCount;
} 1-15
Prog 9.7 (C): Counting words in a String
1-16
Prog 9.8: Counting words in piece of text
#include <stdio.h> #include <stdbool.h>
/* insert the alphabetic(), readLine() and
countWords() functions here ---- */
// Remember the case: char buffer[100] = “”;
int main (void) {
int countWords (const char string[]);
int readLine (char buffer[]);
char text[81];
int totalWords = 0;
bool endOfText = false;
while (!endOfText) {
readLine(text);
//see if ENTER pressed twice
if (text [0] == ‘\0’)
endOfText = true;//text a null string
else
totalWords += countWords (text);
}
#include <stdio.h>
#include <stdbool.h>
struct entry
{
char word[15];
char definition[50];
};
return areEqual;
}
1-18
Prog 9.9 (B): Dictionary Lookup
return -1;
}
1-19
Prog 9.9 (C): Dictionary Lookup
int main (void) {
const struct entry dictionary[100] = {
{ "aardvark", "a burrowing African mammal" },
{ "abyss", "a bottomless pit" },
{ "acumen", "mentally sharp; keen" },
{ "addle", "to become confused" },
{ "aerie", "a high nest" },
{ "affix", "to append; attach" },
{ "agar", "a jelly made from seaweed" },
{ "ahoy", "a nautical call of greeting" },
{ "aigrette", "an ornamental cluster of feathers" },
{ "ajar", "partially opened" }
};
char word[10];
int entries = 10;
int entry;
int lookup (const struct entry dictionary[],
const char search[],
const int entries);
if (entry != -1)
printf ("%s\n",
dictionary[entry].definition);
else
printf (“The word %s is not in my
dictionary.\n", word);
return 0;
} 1-20
Prog 9.10 (A): Dictionary Lookup using Binary Search
// Dictionary lookup program
#include <stdio.h>
struct entry
{
char word[15];
char definition[50];
};
return answer;
}
1-21
Prog 9.10 (B): Dictionary Lookup using Binary Search
// Function to look up a word inside a
dictionary
int low = 0;
int high = entries - 1;
int mid, result;
if (result == -1)
low = mid + 1;
else if (result == 1)
high = mid - 1;
else
return mid; /* found it */
}
if (entry != -1)
printf ("%s\n",
dictionary[entry].definition);
else
printf (“The word %s is not in my
dictionary.\n", word);
return 0;
1-23
}
Prog 9.11: Function to convert a string to an integer
#include <stdio.h>
for (i = 0;
string[i] >= '0' && string[i] <= '9';
++i) {
intValue = string[i] - '0';
result = result * 10 + intValue;
}
return result;
}
return 0;
}
1-24
Tutorial 1: Problem Solving
1-25
Tutorial 2: Problem Solving
c – ‘a’ + ‘A’