1 / 26

Strings

Strings. A sequence of characters delimited by “ “ (not part of the string) “hello” “Neko” “This is some random sentence that I typed!” C does not have a string type It uses an array of characters Array contains a null character (‘’) to denote the end of the string Uses %s to print.

maitland
Télécharger la présentation

Strings

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Strings • A sequence of characters delimited by “ “ (not part of the string) • “hello” • “Neko” • “This is some random sentence that I typed!” • C does not have a string type • It uses an array of characters • Array contains a null character (‘\0’) to denote the end of the string • Uses %s to print

  2. Stores “hello” in character array of size 10 Only represent strings of length 9 Unless we increase the size of str %s specifes string format specifier causes the printf function to step through the character array and print each character until it encounters the null character No null character (‘\0’) could print undesirable results Tedious way of initalizing strings Example // declare a character array //that can hold 10 characters char str[10]; str[0] = 'h'; str[1] = 'e'; str[2] = 'l'; str[3] = 'l'; str[4] = 'o'; str[5] = '\0'; // null character // now print the string to display printf ("%s \n", str); hello

  3. Strings • Initialization • char word[ ]="hello"; • char word[ ]={"hello“}; • char word[10] = “hello”; • char word[10] = {“hello”}; word[0] word[0]

  4. Getting String Input • Several ways – scanf is simplest but most dangerous • Example take input and print it // demonstrates string input #include <stdio.h> main () { // variables declaration char name[11]; // get input from user printf ("Your name (10 letters max):\n"); scanf ("%s", &name); printf ("Hello %s \n", name); } Your name (10 letters max): Neko Hello Neko

  5. Find length of a string char str[ ] = “do it”; int i, len; i = 0; len = 0; while (str[i] != '\0') { i = i + 1; } len = i; return len;

  6. Compare Two Strings char str1[ ] = “do its”, str2[] = “do it”; int i = 0; while (str1[i] == str2[i] && str1[i] != '\0' && str2[i] != '\0') { i = i + 1; } if (str1[i] == str2[i]) { return 1; } return 0;

  7. String Library • C provides a set of functions to manipulate strings. • pp 470 – 472 in book • These functions are located in the library file string.h. • If you want to make use of the string library, you first have to include it in your programs using the #include directive • #include <string.h>

  8. strlen • int strlen (char str[]) • This function determines the length of a string. • Takes a null-terminated character array as input • Returns a count of the number of characters till the first null value is encountered.

  9. strlen - example #include <stdio.h> #include <string.h> int main(void) { char str[] = "This is a string"; printf("length(%s) = %i\n", str, strlen(str)); return 0; } length(This is a string) = 16

  10. strlen – example2 #include <stdio.h> #include <string.h> int main(void) { char str[] = ""; printf("length(%s) = %i\n", str, strlen(str)); return 0; } length() = 0

  11. strcmp • int strcmp (char str1[], char str2[]) • This function compares two strings lexicographically (dictionary order) • Returns negative number if the first string is less than second string • Less than means comes before in the dictionary • Returns 0 if the two strings are equal • Returns positive number if the first string is greater than second string • Greater than means comes after in the dictionary • Not case sensitive

  12. strcmp – example 1 #include <stdio.h> #include <string.h> int main(void) { char str1[] = "boot"; char str2[] = "book"; printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2)); printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1)); return 0; } strcmp(boot, book) = 9 strcmp(book, boot) = -9

  13. strcmp – example 2 #include <stdio.h> #include <string.h> int main(void) { char str1[] = “boot"; char str2[] = “foot"; printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2)); printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1)); return 0; } strcmp(boot, foot) = -4 strcmp(foot, boot) = 4

  14. strcmp – example 3 #include <stdio.h> #include <string.h> int main(void) { char str1[] = “boot"; char str2[] = “boot"; printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2)); printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1)); return 0; } strcmp(boot, boot) = 0 strcmp(boot, boot) = 0

  15. strcmp – example 4 #include <stdio.h> #include <string.h> int main(void) { char str1[] = “Boot"; char str2[] = “boot"; printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2)); printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1)); return 0; } strcmp(Boot, boot) = -32 strcmp(boot, Boot) = 32

  16. strcpy • int strcpy (char dest[], char src[]) • This function copies the string in the character array src[] into the character array dest[]. • It assumes that dest[] is big enough to hold the string in src[]. • It assumes the size of dest >= strlen(src) + 1

  17. strcpy – example #include <stdio.h> #include <string.h> int main(void) { char str1[] = "say hello to my little friend"; char str2[100]; strcpy(str2, str1); printf("strcmp(%s, %s) = %i\n", str1, str2, strcmp(str1, str2)); printf("strcmp(%s, %s) = %i\n", str2, str1, strcmp(str2, str1)); return 0; } strcmp(say hello to my little friend, say hello to my little friend) = 0 strcmp(say hello to my little friend, say hello to my little friend) = 0

  18. strcat • int strcat (char dest[], char src[]) • This function appends the second string to the end of the first string. • The string in the character array src[] is appended to the end of the string in the character array dest[]. • It assumes that dest[] is big enough to accomodate the string in src[].

  19. strcat – example #include <stdio.h> #include <string.h> int main(void) { char str1[100] = "say hello"; char str2[] = " to my little friend"; printf("str1 before: %s\n", str1); strcat(str1, str2); printf("str1 after: %s\n", str1); return 0; } str1 before: say hello str1 after: say hello to my little friend

  20. Math Library • Supplies function definitions for common mathematical operations • #include <math.h> • pp482 – 487 in book

  21. <math.h> - Functions • double ceil(x) – rounds up to nearest integer value • double floor(x) – rounds down to nearest integer value • double round(x) – rounds to nearest integer value • double fabs(x) – calculate absolute value • double cos(r) – r is in radians • double sin(r) – r is in radians • double tan(r) – r is in radians

  22. <math.h> - Functions(cont) • double exp(x) – calculate e^x • double pow(x, y) – returns x^y • double log(x) – returns ln(x) • double log10(x) – returns log10(x)

  23. Strings to Numbers • Sometimes you have a number as a character array • Then you want it as a number type • C provides functions for converting character arrays to numbers • p 479 – 481 • #include<stdlib.h>

  24. Miscellaneous Functions • Random useful functions • pp 490 – 491 • #include<stdlib.h>

  25. <stdlib.h> Functions • int atoi(s) – atoi(“1234”) ;  1234 • double atof(s) – atoi(“12.34”) ;  12.34 • int abs(n) - also has (labs and llabs) • void exit(n) • int rand(void) – returns pseudo random number, x • 0<= x <= RAND_MAX • How to get number between 0 and 1? • int srand(unsigned seed) – seeds the rng • int system(s)

  26. <stdio.h> • int sprintf(char* s, const char* format, …); • Prints provided string to s and ends with ‘\0’ • sprintf(s, “%i”, val); • s is char[ ] large enough to hold the printed string • val is an integer value

More Related