You are on page 1of 2

The values of each of the following variables:

● 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;
}

You might also like