The document defines algorithms for computing the length of a C-style string using strlen, extracting a substring using substr from a given position and length, and counting the number of occurrences of a character in a string using count. It also defines some sample variable values for testing purposes.
The document defines algorithms for computing the length of a C-style string using strlen, extracting a substring using substr from a given position and length, and counting the number of occurrences of a character in a string using count. It also defines some sample variable values for testing purposes.
The document defines algorithms for computing the length of a C-style string using strlen, extracting a substring using substr from a given position and length, and counting the number of occurrences of a character in a string using count. It also defines some sample variable values for testing purposes.
● size1 = 8, because str1 is of size 8, including the null terminating character.
● size2 = 9, because str2 is of size 9, including the null terminating character. ● str3 = [‘A’,‘f’,‘r’,‘i’,‘c’,‘a’,‘n’,‘ ’,‘c’,‘o’,‘u’,‘n’,‘t’,‘r’,‘i’,‘e’,‘s’,‘\0’]; ● str4 = [‘A’,‘f’,‘r’,‘i’,‘\0’]; ● nocc = 1, because the character 'a' occurs once in str1. Algorithm to compute the size of a C-like string:
int strlen(char* str)
{ int size = 0; while (str[size] != '\0') { size++; } return size; } Algorithm to compute a substring: perl
char* substr(char* str, int begin, int length)
{ char* sub = (char*)malloc((length + 1) * sizeof(char)); int i; for (i = 0; i < length; i++) { sub[i] = str[begin + i]; } sub[length] = '\0'; return sub; } Algorithm to count the number of times a character occurs in a string:
int count(char* str, char c)
{ int count = 0; int i = 0; while (str[i] != '\0') { if (str[i] == c) { count++; } i++; } return count; }