Clilstore Facebook WA Linkedin Email

This is a Clilstore unit. You can link all words to dictionaries.

Character strings and functions to work with them


Character variable is called a variable that has a value as characters (letters, digits, signs).

To work with character variables the special functions are used, stored in the header file string.h.

Character variables are described as follows:

char name_of variable;

For example: char a,b;

char x;

The string of characters can be described as a character array.

For example: char x[10];

There are two ways to initialize the string:

using a string constant

For example: char x[20]=”Informatika”;

char x[]=”Informatika”;

in the form of list of characters

For example: char x[20]={‘I’,’n’, ‘f’, ‘o’, ‘r’, ‘m’, ‘a’, ‘t’, ‘i’, ‘k’, ‘a’,’\0’};

\0’- character, defining the end of the line.

In the character constant, each character is numbered from left to right starting with 0.

For example: x[0]=’I’, x[5]=’m’ ит.д.

Consider functions for processing of string variables defined in the header file string.h.

1.Function strlen(S) determines the length of the string S. The string length - is a number of characters in a character variable. String constant length does not exceed 255 characters. Result of the function - an integer number.

Example 39: Find lengths of the two entered words.

#include <iostream.h>

#include <conio.h>

#include <string.h>


{char a[20], b[10];

int n;

cout<<"enter 1st word"; cin>>a;

cout<<"\n enter 2nd word"; cin>>b;

cout<<"\n length of 1st word="<<strlen(a);


cout<<"\n length of 2nd word ="<<n;



Function strcat(S1, S2) assign string S2 to string S1 (concatenation).

Example 40:


{char a[]="Science";

char b[]="Informatics";


cout<<"\n a= " <<a;


As a result of the program work will be displayed on the screen:


Function strcpy(S1, S2) copies entirely string S2 in string S1. As a result, the old value of string S1 will be lost.

Example 41:


{char a[]="Informatics";

char b[]="science";


cout<<"\n a= " <<a<<"\n b="<<b;


As a result of the program work: a=”science", b=”science".

Function strncpy(S1, S2, n) copies n characters of string S2 in string S1 the “tail” is dropped or added with spaces.

Example 42:


{int n=3; char X[]="1234567";

char Y[]="110000";


cout<<"\n X= " <<X;


As a result,a value will be on the screen Х=”1104567”.

Consider the program, using the above listed functions.

Example 43. Count the number of letters 'a' in this word.


{ char S[20];int i,n=0;

cout<<"\n S=";cin>>S;

for (i=0; i<strlen(S); i++)

if (S[i]=='a') n++;

cout<<"\n n= " <<n;



Will generalize the task, i.e. will count the number of letters equal to the entered letter. Then the task will be written as follows:

Example 44.


{ char a[20]; char b[10]; int i, n=0;

cout<<"\n Enter word="; cin>>a;

cout<<"\n Enter letter="; cin>>b;

for (i=0; i<strlen(a); i++)

if (a[i]==b[0]) n++;

cout<<"\n Number of letters = " <<n;


To display the character string in language Turbo С, statement puts() is used.

General format:

puts(“Character string”);

Examples: puts(“enter word”);

puts(“\n answer”);

To enter a character string is used statement gets().

General format of this statement:

gets(Character variable);

For example: char a[10], S1[20], S2[20];



Consider examples of programs with these statements.

Example 45.Count the number of sentences in the text.


{char S[50]; int i, n=0;

puts("Enter text"); gets(S);

for (i=0; i<strlen(S); i++)

if (S[i]==' ' || S[i]=='.') n++;

cout<<"\n n= "<<n;


Example 46.Count how many words in the sentence begins with letter 'a'.


{ char S[50]; int i, n=0;

puts("Enter sentence”); gets(S);

for (i=0; i<strlen(S); i++)

if (S[i]==' ' && S[i+1]=='a') n++;

if (S[0]=='a' || S[0]=='A') n++;

cout<<"\n n= "<<n;


Example 47. Given two words separated by a hyphen. Display the first of them.


{ char S[50]; int i, k;


for (i=0; i<strlen(S); i++)

if (S[i]=='-') k=i;

for (i=0; i<k; i++)



Example 48. Every word in the message is encoded (written vice versa). The program that decodes the sentence.


{ char S[20]; int i;


for (i=strlen(S); i>=0; i--)



Each character of the string value has a numeric code according to the ASCII code table. ASCII code (American Standard Code for Information Interchange) has the main standard and its extension. The main standard is international and is used to encode control characters, digits and letters of the Latin alphabet; pseudographics charactersand letters of national alphabets are used for the extension of the standard.

Codes from 0 to 32 - are different control codes.Characters in ASCII-table begin with code 32.

The following codes are often used to solve tasks:

from48to57 - are digit codes from0to 9.

from65to90 - Latin capital letters (A-65, B-66 etc)

from97 to122 - Latin lower case letters (a-97, b-98 etc)

from128 to159 - capital letters of the Russian alphabet (А-128, В-129 etc)

from160 to175 andfrom224 to239 - lower case letters of the Russian alphabet

keycodes: TAB -9, ESC-27, space -32, point-46, comma-44, question sign-63.

In language C++ there is a function, which returns character according to ASCII code. Function to ASCII translates the given virtual-key code and keyboard state to the corresponding character or characters. This function has the format:

toascii(int c)

This function is stored in the header file ctype.h.

Example 49. The program, which displays character codes from 32 to 127, and their corresponding characters.

#include <iostream.h>

#include <conio.h>



{ int i; char k;

for (i=32; i<=127; i++)


cout<<"\n i="<<i<<" k="<<k;





Example 50 . The program replaces Latin capital letters to Latin lowercase letters in the word.

#include <iostream.h>

#include <conio.h>


#include <string.h>


{ int i;char S[]="PaiNt BruSh";

for (i=0; i<=strlen(S); i++)

if (toascii(S[i])>=65 && toascii(S[i])<97) S[i]=toascii(S[i])+32;

cout<<"\n S="<<S;



As a result of the program work, the value of the variable will be S=paint brush.



Short url: