Strings
Strings
void main() {
char LastName[11];
char FirstName[11];
printf("\n%s:\n",fname);
while (fgets(buffer,sizeof(buffer)-1,instream)
!= NULL)
fputs(buffer,stdout);
fclose(instream);
}
Array of Strings
• Sometimes useful to have an array of string values
• Each string could be of different length (producing
a ragged string array)
• Example:
char *MonthNames[13]; /* an array of 13 strings */
MonthNames[1] = “January”; /* String with 8 chars */
MonthNames[2] = “February”; /* String with 9 chars */
MonthNames[3] = “March”; /* String with 6 chars */
etc.
Array of Strings Example
#include <stdio.h>
void main() {
char *days[7];
char TheDay[10];
int day;
days[0] = "Sunday";
days[1] = "Monday";
days[2] = "Tuesday";
days[3] = "Wednesday";
days[4] = "Thursday";
days[5] = "Friday";
days[6] = "Saturday";
Array of Strings Example
printf("Please enter a day: ");
scanf("%9s",TheDay);
day = 0;
while ((day < 7) && (!samestring(TheDay,days[day])))
day++;
if (day < 7)
printf("%s is day %d.\n",TheDay,day);
else
printf("No day %s!\n",TheDay);
}
Array of Strings Example
int samestring(char *s1, char *s2) {
int i;
/* Not same if not of same length */
if (strlen(s1) != strlen(s2))
return 0;
/* look at each character in turn */
for (i = 0; i < strlen(s1); i++)
/* if a character differs, string not same */
if (s1[i] != s2[i]) return 0;
return 1;
}
String Functions
• C provides a wide range of string functions for
performing different string tasks
• Examples
strlen(str) - calculate string length
strcpy(dst,src) - copy string at src to dst
strcmp(str1,str2) - compare str1 to str2
• Functions come from the utility library string.h
– #include <string.h> to use
String Length
Syntax: int strlen(char *str)
returns the length (integer) of the string argument
counts the number of characters until an \0 encountered
does not count \0 char
Example:
char str1 = “hello”;
strlen(str1) would return 5
Copying a String
Syntax:
char *strcpy(char *dst, char *src)
copies the characters (including the \0) from the source string
(src) to the destination string (dst)
dst should have enough space to receive entire string (if not,
other data may get written over)
if the two strings overlap (e.g., copying a string onto itself) the
results are unpredictable
return value is the destination string (dst)
char *strncpy(char *dst, char *src, int n)
similar to strcpy, but the copy stops after n characters
if n non-null (not \0) characters are copied, then no \0 is copied
String Comparison
Syntax:
int strcmp(char *str1, char *str2)
compares str1 to str2, returns a value based on the first character
they differ at:
less than 0
if ASCII value of the character they differ at is smaller for
str1
or if str1 starts the same as str2 (and str2 is longer)
greater than 0
if ASCII value of the character they differ at is larger for str1
or if str2 starts the same as str1 (and str1 is longer)
0 if the two strings do not differ
String Comparison (cont)
strcmp examples:
strcmp(“hello”,”hello”) -- returns 0
strcmp(“yello”,”hello”) -- returns value > 0
strcmp(“Hello”,”hello”) -- returns value < 0
strcmp(“hello”,”hello there”) -- returns value < 0
strcmp(“some diff”,”some dift”) -- returns value < 0
expression for determining if two strings s1,s2 hold
the same string value:
!strcmp(s1,s2)
String Comparison (cont)
Sometimes we only want to compare first n chars:
int strncmp(char *s1, char *s2, int n)
Works the same as strcmp except that it stops at the
nth character
looks at less than n characters if either string is shorter
than n
strcmp(“some diff”,”some DIFF”) -- returns value > 0
strncmp(“some diff”,”some DIFF”,4) -- returns 0
strcpy/strcmp Example
#include <stdio.h>
#include <string.h>
void main() {
char fname[81];
char prevline[101] = "";
char buffer[101];
FILE *instream;
printf("Check which file: ");
scanf("%80s",fname);
if ((instream = fopen(fname,"r")) == NULL) {
printf("Unable to open file %s\n",fname);
exit(-1);
}
strcpy/strcmp Example
/* read a line of characters */
while (fgets(buffer,sizeof(buffer)-1,instream) != NULL) {
/* if current line same as previous */
if (!strcmp(buffer,prevline))
printf("Duplicate line: %s",buffer);
/* otherwise if the first 10 characters of the current
and previous line are the same */
else if (!strncmp(buffer,prevline,10))
printf(”Start the same:\n %s %s",prevline,buffer);
/* Copy the current line (in buffer) to the previous
line (in prevline) */
strcpy(prevline,buffer);
}
fclose(instream);
}
String Comparison (ignoring case)
Syntax:
int strcasecmp(char *str1, char *str2)
similar to strcmp except that upper and lower case characters
(e.g., ‘a’ and ‘A’) are considered to be equal
int strncasecmp(char *str1, char *str2, int n)
version of strncmp that ignores case
String Concatenation
Syntax:
char *strcat(char *dstS, char *addS)
appends the string at addS to the string dstS (after dstS’s
delimiter)
returns the string dstS
can cause problems if the resulting string is too long to fit in dstS
char *strncat(char *dstS, char *addS, int n)
appends the first n characters of addS to dstS
if less than n characters in addS only the characters in addS
appended
always appends a \0 character
strcat Example
#include <stdio.h>
#include <string.h>
void main() {
char fname[81];
char buffer[101];
char curraddress[201] = "";
FILE *instream;
int first = 1;
printf("Address file: ");
scanf("%80s",fname);
if ((instream = fopen(fname,"r")) == NULL) {
printf("Unable to open file %s\n",fname);
exit(-1);
}
strcat Example
/* Read a line */
while (fgets(buffer,sizeof(buffer)-1,instream) != NULL) {
if (buffer[0] == '*') { /* End of address */
printf("%s\n",curraddress); /* Print address */
strcpy(curraddress,""); /* Reset address to “” */
first = 1;
}
else {
/* Add comma (if not first entry in address) */
if (first) first = 0; else strcat(curraddress,", ");
/* Add line (minus newline) to address */
strncat(curraddress,buffer,strlen(buffer)-1);
}
}
fclose(instream);
}