Documente Academic
Documente Profesional
Documente Cultură
Chapter 11
Strings In general, a string is a series of characters treated as
a unit. Computer science has long recognized the
Objectives
❏ To understand design concepts for fixed-length and variable-
importance of strings, but it has not adapted a
standard for their implementation. We find, therefore,
❏ To understand the design implementation for C-language
length strings
that a string created in Pascal differs from a string
created in C.
❏ To write programs that read, write, and manipulate strings
delimited strings
Computer Science: A Structured Programming Approach Using C 1 Computer Science: A Structured Programming Approach Using C 2
1
character literal – a single character in single
Differences Between Strings and Character Arrays
quotes ‘ ’
FIGURE 11-3 Storing Strings FIGURE 11-4 Storing Strings and Characters FIGURE 11-8 String Literal References
Computer Science: A Structured Programming Approach Using C 5 Computer Science: A Structured Programming Approach Using C 6
Note
Memory for strings must be allocated before the
string can be used.
Declaration of String
Example:
char symbol; // declare a character
char str[80]; // declare a string
Example
#define NUM_STUDENTS 30 //defined constants
#define NAME_LEN 25 //defined constants
Initializing Strings
Computer Science: A Structured Programming Approach Using C 7 Computer Science: A Structured Programming Approach Using C 8
2
Initialization of a String Sample program:
#include <stdio.h>
int main()
{
To assign value into the string or character int i, size;
char name[10][20];
Example:
printf("How many names do you plan to input?(max is 10): ");
char message[81]; //declare scanf("%d", &size);
How many names do you plan
printf("Enter names:\n"); to input?(max is 10): 3
for(i=0; i<size; i++)
message[] = “Shah”; //assign value scanf("%s", &name[i]);
Enter names:
razin
mahmud
OR habibah
printf("\nThese are the names:\n");
message[81] = “Shah”; for(i=0; i<size; i++)
These are the names:
printf("%s\n", name[i]); razin
OR mahmud
return 0; habibah
message[81] ={‘S’,‘h’,‘a’,‘h’}; } Press any key to continue
Computer Science: A Structured Programming Approach Using C 9 Computer Science: A Structured Programming Approach Using C 10
3
Example:Read Student Names and Scores
#include <stdio.h>
int main()
{
char first[80]="Abdul Halim";
char last[80]="Abdul Razak";
int score=88;
return 0;
}
Computer Science: A Structured Programming Approach Using C 13 Computer Science: A Structured Programming Approach Using C 14
printf("Here is your string: %s", str); printf("Here is your string: %s", str);
return 0; return 0;
} }
Please enter a string: How are you? Please enter a string: How are you?
Here is your string 1: How are you?Press any key to continue Here is your string: How are you?
Press any key to continue
Computer Science: A Structured Programming Approach Using C 15 Computer Science: A Structured Programming Approach Using C 16
4
Demonstrate puts Operation
#include <stdio.h>
int main()
{
char str[80]="How are you?";
puts(str);
puts(str+5);
return 0;
}
Computer Science: A Structured Programming Approach Using C 17 Computer Science: A Structured Programming Approach Using C 18
5
PROGRAM 11-8 Print Selected Sentences PROGRAM 11-10 Array of string: Print Days of the Week
//Echo keyboard input that begins with capital letter. Consider, for example, the need to store the days of the week in their textual format.
#include <stdio.h> We could create a two-
two-dimensional array of seven days by ten characters.
#include <ctype.h> #include <stdio.h>
int main() int main()
{ {
char str[80]; int i;
char days[7][10]={"Sunday","Monday","Tuesday",
while(fgets(str, sizeof(str), stdin)) "Wednesday","Thursday","Friday",
if(isupper(str[0])) "Saturday"};
fputs(str, stdout);
printf("The days of the week\n");
return 0;
} The days of the week
for(i=0; i<7; i++) Sunday
Now is the time puts(days[i]); Monday
Now is the time Tuesday
for all good students return 0; Wednesday
to come to the aid } Thursday
of their school. Friday
Amen Saturday
Amen Press any key to continue
Computer Science: A Structured Programming Approach Using C 21 Computer Science: A Structured Programming Approach Using C 22
Computer Science: A Structured Programming Approach Using C 23 Computer Science: A Structured Programming Approach Using C 24
6
Sample of String Functions (strcat) Sample of String Functions (strcpy, strlen)
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
int main() int main()
{ {
char word1[12]="alpha“, word2[12]="beta"; char word1[12]="alpha“, word2[12]="beta";
strcat(word1,word2); int length;
Word 1 is: alphabeta strcpy(word1,word2); Word 1 is: beta
printf("Word 1 is: %s\n", word1); Word 2 is: beta Word 2 is: beta
printf("Word 2 is: %s\n", word2); Press any key to continue printf("Word 1 is: %s\n", word1); Length of Word 1 is: 4
printf("Word 2 is: %s\n", word2); Press any key to continue
return 0; length=strlen(word1);
} printf("Length of Word 1 is: %d\n", length);
return 0;
word1 word2 strcat(word1,word2) }
int num1=strcmp(word1,word2);
int num2=strcmp(word1,word3);
int num3=strcmp(word1,word4);
printf("%d\n", num1);
printf("%d\n", num2);
printf("%d\n", num3);
0
return 0;
-1
}
1
Press any key to continue FIGURE 11-17 String Compares
Computer Science: A Structured Programming Approach Using C 27 Computer Science: A Structured Programming Approach Using C 28
7
#include <stdio.h>
#include <string.h>
int main()
{
Example of strcmp( ) usage
String Library Functions (ctype.h)
char name[3][10];
char temp[10];
int i, j;
isalpha
printf("Enter 3 names:\n");
for (i=0; i<3; i++) returns true if the character is in the range of A-Z or a-z.
scanf("%s", &name[i]); isdigit
for (i=3; i>=0; i--) //bubble sort algorithm
returns true if the character is in the range of 0-9.
{ islower
for(j=1; j<=i; j++){
if (strcmp(name[j-1], name[j])>0){ returns true if the character is in the range of a-z.
strcpy(temp, name[j-1]); isupper
strcpy(name[j-1], name[j]);
strcpy(name[j], temp); returns true if the character is in the range of A-Z.
}
Enter 3 names:
tolower
}
} bakri if isupper is true, return the lowercase character otherwise return the
ali character.
abdul
printf("Names in alphabetic order:\n");
Names in alphabetic order: toupper
for (i=0; i<3; i++)
printf("%s\n", name[i]); abdul if islower is true, return the uppercase character otherwise return the
ali character.
return 0; bakri
} Press any key to continue
Computer Science: A Structured Programming Approach Using C 29 Computer Science: A Structured Programming Approach Using C 30
Sample program:
String Library Functions (ctype.h) #include <stdio.h>
#include <ctype.h>
isalpha toupper (character) int main()
{
isalpha (‘A’) returns 1 toupper (‘g’) returns ‘G’
int i;
char name[3]="Ab4";
isalpha (‘4’) returns 0 toupper (‘G’) returns ‘G’ for(i=0; i<3; i++)
{
isupper printf("Index[%d] which is %c\n", i, name[i]);