Strings
Strings
#include <stdio.h>
void main() {
char LastName[11];
char FirstName[11];
scanf("%10s%*[^,],%10s",LastName,FirstName);
FirstName,LastName);
}
ARRAY OF STRINGS
#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
scanf("%9s",TheDay);
day = 0;
day++;
if (day < 7)
else
}
ARRAY OF STRINGS EXAMPLE
int i;
if (strlen(s1) != strlen(s2))
return 0;
return 1;
}
STRING FUNCTIONS
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)
STRCPY/STRCMP EXAMPLE
#include <stdio.h>
#include <string.h>
void main() {
char fname[81];
char prevline[101] = "";
char buffer[101];
FILE *instream;