# Arrays. Part 4. Examples of solving tasks using character strings

## Arrays. Part 4. Examples of solving tasks using character strings

This topic is solved some tasks using strings of characters. At the decision of these tasks standard functions from library “string.h” are used.

This topic is the continuation of the study of topics: one-dimensional arrays and multidimensional arrays.

### Contents

##### 1. Tasks for searching for a character in a string. Examples

Example 1. Given a string of characters. Determine, is the specified character c in the string of characters.

```// Search for a character in a string
char S[50]; // characters string
char c; // searched character
int i;
bool f_is; // f_is=true - character is in the string, otherwise f_is=false

// string S input
// ...
// character c input
// ...

for (i=0; i<strlen(S); i++)
if (S[i]==c)
{
f_is = true; // character c is in the string S
break;
}

if (f_is)
label1->Text = "Character " + c.ToString() + " is in the string";
else
label1->Text = "Character " + c.ToString() + " is not in the string";```

In this example, to determine the length of the string S function

`strlen(S)`

is used.

This function returns the number of characters in the string without regard to the last character ‘\0’.

To use this function in Visual C++ you need at the beginning of the file before the namespace definition to include one of the libraries <string.h> or <cstring>.

To do this, type one of the following strings:

`#include <string.h>`

or

`#include <cstring>`

Example 2. Let there is given some text. Calculate how many times repeated preassigned symbol a.

```// finding the number of occurrences of a character in a string
char S[50]; // characters string
char a; // given character
int i;
int k; // The result is the number of occurrences of the symbol a in the string S

// string S input
// ...
// character a input
// ...

k = 0; // in the beginning, reset the counter k
for (i=0; i<strlen(S); i++)
if (S[i]==a)
k++; // Increase the counter by 1```

Example 3. In the given text, count the number of symbols ‘+’ and ‘ – ‘.

```// Counting the number of characters in a string
char str[50]; // given text
int i;
int n_p; // result - number of symbols '+'
int n_m; // result - number of symbols '-'

// Input of array str
// ...

n_p = 0;
n_m = 0;
for (i=0; i<strlen(str); i++)
{
if (str[i] == '+')
n_p++;
if (str[i] == '-')
n_m++;
}```

##### 2. Solving the tasks to replace characters. Examples

Example 1. In the given text, replace all the characters ‘+’ with ‘ – ‘.

```// symbols replacement
char str[50]; // a given text
int i;

// text input
// ...

for (i=0; i<strlen(str); i++)
if (str[i] == '+')
str[i] = '-';```

Example 2. Let several lines of text be given (two-dimensional array of symbols). Replace all ‘+’ characters with ‘ – ‘.

```// Replacing characters in an array of strings
char str[5][50]; // A given array that contains 5 rows
int i, j;

// Input an array of 5 rows
// ...

for (i=0; i<5; i++)
for (j=0; j<strlen(str[i]); j++)
if (str[i][j] == '+')
str[i][j] = '-';```

Example 3. In the given text, replace all the characters ‘+’ with ‘+++’.
This task is easily solved by introducing an auxiliary array into the text of the program.

```// Replacing '+' with '+++'
char str[50]; // a given text
int i, j;
char str2[150]; // auxiliary array

// input of array str
// ...

j = 0; // current position in array str2
for (i=0; i<strlen(str); i++)
if (str[i] == '+')
{
str2[j++] = '+';
str2[j++] = '+';
str2[j++] = '+';
}
else
{
str2[j] = str[i];
j++;
}

str2[j] = '\0';
strcpy(str, str2); // Copy str2 to str```

The listing uses the strcpy() function from the library <string.h> (or <cstring>), which copies one string to another.

Example 4. In the given text, replace the sequence of the characters ‘…’ with ‘ . ‘ .

```// Replacing the characters '...' with '.'
char str[50]; // a given text
int i, j;
char str2[50]; // auxiliary array
int d;

// input of array str
// ...

j = 0; // current position in the array str2
d = strlen(str); // the length of string str

for (i=0; i<d; i++)
if (i<d-2)
{
if ((str[i]=='.')&&(str[i+1]=='.')&&(str[i+2]=='.'))
{
str2[j++] = '.';
i+=2;
}
else
{
str2[j++] = str[i];
}
}
else
{
str2[j++] = str[i];
}

str2[j] = '\0';
strcpy(str, str2); // Copy the auxiliary array to the source array```

Just like in the previous example, an auxiliary array is used. In the original array, the sequence of the ‘…’ is checked by using the

`if ((str[i]=='.')&&(str[i+1]=='.')&&(str[i+2]=='.'))`

If such a sequence is found, then ‘ . ‘ is added to the auxiliary array.

```str2[j++] = '.';
i+=2;```

In another case, the character from the source array is copied to the auxiliary array.

`str2[j++] = str[i];`

##### 3. Tasks for determining the position of a given symbol. Examples

Example 1. Given a string of characters. In the given text, determine the position of the first point ‘ . ‘. Assume that the first character in the string has position 1.

```// Search for the first occurrence of a character in a text
char str[50]; // a given text
int i;
int pos; // the desired value - position

// input of array str
// ...

pos = -1;

for (i=0; i<strlen(str); i++)
if (str[i]=='.')
{
pos = i+1; // save the position
break; // Exit from the loop, further looping does not make sense
}

if (pos == -1)
label1->Text = " The symbol is absent in the text.";
else
label1->Text = pos.ToString();```

Example 2. A string of characters is specified. Determine the position of the last point ‘ . ‘ in the text.

```// Find the last occurrence of a character in the text
char str[50]; // specified text
int i;
int pos; // the desired value - position

// input of array str
// ...

pos = -1;

for (i=strlen(str)-1; i>=0; i--) // String scan from end to beginning
if (str[i]=='.')
{
pos = i+1; // save the position
break; // Exit from the loop, further looping does not make sense
}

if (pos == -1)
label1->Text = " No symbol in the text.";
else
label1->Text = pos.ToString();```

##### 4. Solving the tasks on the transformation of text. Examples

Example 1. A string of characters is specified. Create a new string, which is formed from this string and it is read from the end to the begin.

```// Reverse of a string
char str[50]; // specified string
char str2[50]; // result string
int i;
int d;

// input of array str
// ...

d = strlen(str);

for (i=0; i<d; i++)
str2[d-i-1] = str[i];

str2[d] = '\0';```

Example 2. Given word. Check whether the word is read from left to right and vice versa.

```// checking the string
char str[50]; // specified string
int i;
int d;
bool f_yes; // result variable

// input of array str
// ...

d = strlen(str);
f_yes = true;
for (i=0; i<d/2; i++) // scan of string from end to start
if (str[i] != str[d-i-1])
f_yes = false;

if (f_yes)
label1->Text = "The string is read from left to right and vice versa";
else
label1->Text = "The string is not read from left to right and vice versa";```

Example 3. Check whether these words are inverse to each other, that is, the first word is read from left to right in the same way as the second word from right to left.

```// testing the two words for reversibility
char str1[50]; // string 1
char str2[50]; // string 2
int i;
int d1, d2;
bool f_yes; // result variable

// input of arrays str1, str2
// ...
d1 = strlen(str1);
d2 = strlen(str2);

f_yes = true;

if (d1 == d2) // Are equal the lengths of strings?
{
for (i=0; i<d1; i++)
if (str1[i] != str2[d1-i-1])
f_yes = false;
}
else
f_yes = false;

if (f_yes)
label1->Text = " Words are reversible among themselves";
else
label1->Text = " Words are not reversible among themselves";```

##### 5. Solving the tasks for counting the number of characters in the text. Examples

Example 1. A string of characters is specified. Count the number of ‘+’ characters in this string.

```// counting the number of '+' characters in the string
char str[50];
int i;
int k; // Result - the number of characters

// input of string str
// ...

k = 0; // reset the counter

for (i=0; i<strlen(str); i++)
if (str[i] == '+')
k++;

// output of result
label1->Text = k.ToString();```

Example 2. Multiple character strings are specified. Count the number of ‘-‘ characters in these strings.

```// counting the number of '-' characters in an array of 6 strings
char s[6][50];
int i, j;
int k; // Result - the number of characters

// Input the strings s
// ...
k = 0; // reset the counter

// calculation of k
for (i=0; i<6; i++)
for (j=0; j<strlen(s[i]); j++)
if (s[i][j] == '-')
k++;

// output of result
label1->Text = k.ToString();```

Example 3. A string of characters is specified. Count the number of words in this string. Consider that words are separated by one of the characters ‘ ‘ (space), ‘ , ‘ (comma), ‘ . ‘ (dot).

```// counting the number of words in a string
char s[50]; // the string of characters
int i;
int k; // result - the number of words
bool f; // auxiliary variable - defines the end of the previous word

// input of string s
// ...

k = 0; // reset the counter
f = true;

// calculation of k
for (i=0; i<strlen(s); i++)
if ((s[i] == ' ')||(s[i]==',')||(s[i]=='.'))
{
f = true; // end of the word
}
else
{
if (f)
{
// the beginning of a new word
k++;
f = false;
}
}

// output of result
label1->Text = k.ToString();```

In the example above, using the auxiliary variable f, the end of the preceding word is determined.

`f = true`

hense, the end of the preceding word was reached. Otherwise, a new word begins and the counter k is incremented by 1.

Example 4. Several lines of characters are specified (two-dimensional array of symbols). Calculate the number of words that begin with the characters ‘a’ or ‘A’. To consider that the word ends with one of the characters ‘ ‘ (space), ‘ . ‘ (Dot) or ‘ , ‘ (comma).

```// counting the number of words in a string array
char s[5][50]; // array of character strings
int i, j;
int k; // result - the number of words
bool f; // auxiliary variable - defines the end of the previous word

// input of strings array s
// ...

k = 0; // reset the counter

// calculation of k

for (i=0; i<5; i++)
{
f = true;
for (j=0; j<strlen(s[i]); j++)
if ((s[i][j] == ' ')||(s[i][j]==',')||(s[i][j]=='.'))
{
f = true; // the end of the word
}
else
{
if (f)
{
// the beginning of a new word
if ((s[i][j]=='a')||(s[i][j]=='A'))
k++;
f = false;
}
}
}

// output of result
label1->Text = k.ToString();```

##### 6. Sorting character strings using the “bubble” method. Example

Example. Multiple character strings are specified. Sort these strings in alphabetical order.

```// sorting a string array using the "bubble" method
char s[5][50]; // array of character strings
int i, j;
char ts[50]; // auxiliary string

// input of strings array s
// ...

for (i=0; i<4; i++)
for (j=i; j>=0; j--)
if (strcmp(s[j],s[j+1])>0)
{
// swap strings
strcpy(ts, s[j]);
strcpy(s[j], s[j+1]);
strcpy(s[j+1], ts);
}```

In the above listing, to compare strings in lexicographical order, use the strcmp() function from the library <string.h> (cstring). The function has the general form:

`int strcmp(string1, string2);`

where string1, string2 – strings that are compared to each other.

The function returns a number:

• 0, if string1 follows after string2 in lexicographical order;
• =0, if the strings are the equals;
• <0, if string1 follows before string2 in lexicographical order.