SLIDE 1
String Functions COMP 1002/1402 Arrays of Strings char *pDays[7]; - - PDF document
String Functions COMP 1002/1402 Arrays of Strings char *pDays[7]; - - PDF document
String Functions COMP 1002/1402 Arrays of Strings char *pDays[7]; pDays[0] ="Sunday"; pDays[1] ="Monday"; pDays[2] ="Tuesday"; pDays[3] ="Wednesday"; pDays[4] ="Thursday"; pDays[5]
SLIDE 2
SLIDE 3
3
Functions : gets and fgets
char *gets (char *strPtr); /*stdin */ char *fgets (char *strPtr, int size, FILE *fp);/* from file */
Fetches input string till '\n'
- If there is a problem it returns NULL
- Otherwise it returns the pointer
- gets Changes '\n' to '\0’; Not fgets
Functions gets and fgets
SLIDE 4
4
Functions puts and fputs
int puts (const char *strPtr);/*to stdout */
- puts Changes '\0' to newline
int fputs (const char *strPtr, FILE
*fp);
/* outputs to file */
- fputs does not change '\0' to newline
- '\0' is dropped
- Outputs string may or may not have newline
- If there is a problem both return EOF
Functions : puts and fputs
SLIDE 5
5
String Manipulation Functions
C has a variety of string functions
- Included in <string.h>
- All begin with str
Function : strlen (length)
Outputs : String Length (not including '\0') int strlen(const char *s); How to Use: char *pstr="Hello"; int i = strlen(pstr);
SLIDE 6
6
Function : strlen (length)
char strng[81]; … fputs(strng, outFile); if (strng[strlen(strng) – 1] != '\n'){ fputs("\n", outFile); } /* If the last character in a “line” is not newline it inserts one….*/
String Copy (returns to)
char *strcpy(char *to, const char *from);
strcpy – Not safe, length not specified
char *strncpy(char *to, const char *from, int size);
strncpy – Safer because length is specified
SLIDE 7
7
s t r c p y s t r n c p y
SLIDE 8
8
String Compare
int strcmp(const char *str1,const char*str2);
strcmp – Length not specified
int strncmp(const char *str1,const char *str2, int size);
strncmp – Compare up to specified length
s t r c m p
SLIDE 9
9
strncmp(str1,str2,size)
equal 3 "ABC123" "ABC" >0 str1 < str2 4 "ABC" "ABC123" equal 3 "ABC" "ABC123" <0 str1 < str2 4 "ABC456" "ABC123" equal 3 "ABC456" "ABC123" equal 8 "ABC123" "ABC123" Returns Results Size str2 str1
String Concatenate (returns to)
char *strcat(char *to, const char *from);
strcat – Not safe, length not specified
char *strncpy(char *to, const char *from, int size);
strncat – Safer because length is specified
SLIDE 10
10
strcat & strncat Character in String
char *strchr(const char *string, char ch)
strchr – returns pointer to first match
char *strrchr(const char *string, char ch)
strrchr – returns pointer to last match
SLIDE 11
11
strchr & strrchr String in String
char *strstr(const char *strng, const char *sub_str)
strstr – returns pointer to first match
SLIDE 12
12
String Token
char *strtok(const char *strng, const char *delim)
strtok – returns pointer to first match after first delimiter If strng is NULL then returns next pointer
- NB. In the next slide the top string is wrong. There should only be a space
between SUM and =, and no the null character, ‘\0’.
s t r t
- k
SLIDE 13
13
Scan Memory String
int sscanf(char *str, const char *format,…);
“Reads” memory into variables Uses format string of scanf... Returns number of variables read
sscanf
Allows more control over data from file
SLIDE 14
14
Example : sscanf
Input: Einstein, Albert; 1234 67 D Code:
i = sscanf(strIn, "%25[^;]%*c%4s%d%*[^ABCDF]%c", name, stuNo, &score, &grade);
Format Memory String
int sprintf(char *str, const char *format,…);
“prints” variables into memory Uses format string of printf... Returns EOF if error occurs
SLIDE 15