string functions
play

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]


  1. 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] ="Friday"; pDays[6] ="Saturday"; 1

  2. Pointers to Strings Walking thro’ Array char **pWalker, **pLast ; pLast = &pDays[6] For (pWalker = pDays; pWalker <= pLast; pWalker++;) {Body of the Loop} printf("%p -> %s\n", pWalker, *pWalker) 2

  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 3

  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 4

  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); 5

  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 6

  7. s t r c p y s t r n c p y 7

  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 8

  9. strncmp(str1,str2,size) str1 str2 Size Results Returns "ABC123" "ABC123" 8 equal 0 "ABC123" "ABC456" 3 equal 0 "ABC123" "ABC456" 4 str1 < str2 <0 "ABC123" "ABC" 3 equal 0 "ABC123" "ABC" 4 str1 < str2 >0 "ABC" "ABC123" 3 equal 0 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 9

  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 10

  11. strchr & strrchr String in String char *strstr(const char *strng, const char *sub_str) strstr – returns pointer to first match 11

  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 o k 12

  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 13

  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 14

  15. sprintf Allows ease of use and control 15

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend