Contacts

C commands for working with a string. Libraries and functions for working with rows in si. Operations on strings

In this lesson, we will discuss the strings in the style of C, perhaps you have already seen these lines on our website or in any other textbook. In fact, the S-rows are just arrays of symbols but, with their specificity, so we always know where the end of the line. In this article, we will look at several functions for working with rows, for example, you are copying, concatenation, to obtain a string length.

What is strings?

Note that along with strings in the style of C, which, in fact, are simple arrays, there are also string literals, such as this "Literal". In reality, the strings are that literals are just sets of characters located near the computer's memory. But between arrays and literals, all the same, there is a difference, literals can not be changed and lines are possible.

Any function that takes a string in style C cannot also be received as a parameter - literal. In C, there are also some entities that may look like lines, although, in fact, they are not. I'm talking about the characters now, they are enclosed in single quotes, here is an example - "A", as you can see, this is not a line. The symbol can be in a specific place, assign a string, but the characters cannot be processed as a string. If you remember, arrays work as pointers, so if you pass one character to the string, it will be considered an error.

Of all the above, you had to understand that rows are arrays of characters, and string literals are words surrounded by double quotes. Here is another example of literal:

"This is a static string"

You have not yet forgotten about the specifics of the lines, which was mentioned a little higher? So, the S-rows should always end with a zero symbol, literally - "\\ 0". Therefore, to declare a string consisting of 49 letters, you must reserve an additional cell under zero symbol:

Char My String;

As can be seen from the example, the length of the array is 50 characters, 49 of which will take the string and one, the latter will take the zero symbol. It is important to remember that at the end of the SI lines should always be a zero character, just like at the end of each sentence there is a point. Although the symbol is not displayed when the string is derived, it still takes place in memory. Therefore, technically, in an array of fifty elements, you could save only 49 letters, because, the last character is needed to complete the string. In addition, pointers can also be used as a string. If you read an article about, you can do something similar:

Char * mystring; // pointer type CHAR MYSTRING \u003d Malloc (Sizeof (* MyString) * 64); // Memory allocation

In this example, we allocated 64 cells in the memory for the MYSTRING array. To release the memory, use the FREE () function.

FREE (MyString);

Using string

Rows are useful to use when you need to perform various textual information operations. For example, if you want the user to enter a program name, you must use the string. Using the SCANF () function to enter the string - works, but this can lead to buffer overflow. After all, the input string may turn out to be greater than the size of the buffer string. There are several ways to solve this problem, but the easiest way is to use it to be announced in the header file. .

When you read the input from the user, it will read all the characters except the last. After that, at the end of the read line, the zero terminator will place. The FGETS () function will read the characters until the user clicks Enter. Let's see an example of using FGETS ():

#Include. INT MAIN () (Char My String; // Long string PrintF ("Enter long string: "); FGETS (MyString, 100, Stdin); // Read the PrintF line from the input stream (" You entered the following line:% s ", mystring); getchar ();)

The first parameter for fgets () is a string, the second parameter - the size of the string and the third parameter is a pointer to input flow data.

The result of the program:

<ВВОД>...

As you can see, from the output of the program, the symbol of the new line came to the input line - "\\ n". It so happened due to the fact that Fgets () believed in the MYSTRING string pressing the ENTER button and completed the work. This means that you may need to manually delete a symbol of a new line. One of the ways to do this, the hill bust. Let's finalize the program and delete the symbol of the new line:

#Include. INT MAIN () (CHAR MYSTRING; // Long string Printf ("Enter a long string:"); FGETS (MYSTRING, 100, STDIN); // read from the input stream INT i; for (i \u003d 0; I< 100; i++) { if (myString[i] == "\n") { myString[i] = "\0"; break; } } printf("Вы ввели следующую строку: %s", myString); getchar(); }

Please note that if the input line contains less than 100 characters, the new string symbol will fall into the string. Therefore, we can delete this symbol using a simple bust. In the program, we added a loop in which we swore string characters, rows 12-19. And when we are found a symbol of a new line, we replace it with a zero symbol, row 16.. The result of the program:

Enter a long string: Fate leaves his imprint You entered the following line: Fate leaves his imprint to close this window, press<ВВОД>...

That's all. In the next article, I will tell you about special features To work with rows.

P.S.: We all love to watch different video records, but sometimes it happens that it is not always possible to reproduce some formats of video files. So, you can solve this problem using the program - Xilisoft Converter Ultimate. You can easily quickly convert video from one format to another. In addition, this program can also convert audio files, and animated images.

It is no coincidence that the topic about the string I placed in the section "Arrays". Since the string is, in fact, the array of characters. Here is an example:

char str \u003d "This is just a string";

The same line for greater understanding can be written like this:

char Str \u003d ("E", "T", "O", "", "P", "R", "O", "C", "T", "O", "", "C", "T", "R", "O", "K", "A");

Those. All the same array, only consisting of characters. Therefore, it is possible to work with it, as well as with integer arrays.

And now let's try work with rows in c. In the introductory lessons, we disassembled that the characters relate to integer types, i.e. Each symbol has its own numeric value. Here is an example and his decision:

  1. it is required to translate the entered word into the upper case:
  2. #Include.
    #Include.

    INT MAIN ()
    {
    Char str \u003d "Sergey";

    STR [i] - \u003d 32;
    }
    For (int i \u003d 0; Str [i]! \u003d "\\ 0"; I ++) (
    PrintF ("% C", STR [I]);
    }
    getch ();

    Return 0;
    }

    to get the code code, simply use the PRINTF function by the% d parameter. Yes, and one more important moment: ending any strings is a zero-terminator that is indicated special symbol - "\0".

Another way to specify a string is to declare it through char *. Here is an example:

char * str \u003d "provod";

Those. A pointer is created on the string, which is located somewhere in memory.

But how can I enter lines through, we already have a native, Scanf operator:

char str; scanf ("% s", str);

There are two subtleties:

  1. the address of the address of the address is not needed here, since the name of the array, as we already know, is the address
  2. The length of the entered string should not exceed 15 characters, since the latter must be a zero-terminator. Moreover, the compiler will fill this symbol after the last introduced your character.

Since the SI language is a structural language, there are already built-in functions for works with strings and with symbols. To process strings you need to connect the file: CType.h. The file contains the function definition functions, symbol format. In principle, all you may need to learn about the symbol can be performed using the CTYPE.H file functions

Sometimes you may need to translate the string to another data type. To transfer rows to other types, there is a STDLIB library. Here is its functions:

  1. iNT ATOI (Char * Str)
  2. long Atol (Char * Str)
  3. double ATOF (Char * Str)

Sometimes these functions help, for example, when you need to remove a year or digital value from the line. Work with rows in C (SI) It is a very important topic, so try to delve into this lesson.

Announcement of string

The string in the C language is a one-dimensional array of characters, the last element of which is the end of the string symbol - zero (a string ending with zero, that is, NULL Terminated String).

An declaration of a variable type string in the SI language is possible in three ways, two of which initialize the string during the announcement.

The first way:

Classifieds array characters (do not forget to add a place for the final zero):

Char s;

The second way:

Assign a string variable the initial value (while the length of the string, the compiler can calculate himself):

Char S \u003d "An example of the initialization of the string";

Rowing constant is recorded to the right of the assignment sign. At the end of the line, zero ('\\ 0') is automatically added. Symbolic lines constants are placed in a static memory class.

Third way:

An implicit indication that an array is used. On the left side of the assignment sign indicates the pointer to the symbol:

Char * s \u003d "second initialization option";

The variable s will be a pointer to that place in random access memorywhere the string constant is located. In such a form of recording, a potential error lies in the fact that the pointer to the symbol is often called a string. The entry below is only a pointer to the character, since it is not provided for placement a string:

Char * s;

Enter a row from a standard input device (keyboard)

To work with rows there is a set of functions. To enter from a standard input device (keyboard), librarians from the standard I / O module are most often used: scanf. and gets..

To enter a string using the function scanf., uses format « % S.» , and pay attention to the fact that the address sign is not used before the line identifier « & » Since the one-dimensional array is already represented by the pointer at its beginning:

Scanf ("% s", s);

Function gETS () reads symbols until it reaches the transition symbol to new string. The function takes all the characters down to the row translation symbol, but does not include it. By the end of the line, the final zero ('\\ 0') is added. Function gETS () Places a sequence of characters in a string type parameter read from the keyboard and returns a pointer to this string (if the operation has been completed successfully), or NULL (in case of an error). In the example below, when successfully completing the operation, two identical lines will be displayed on the screen:

#Include. int Main () (char s; char * p; p \u003d gets (s); printf ("\\ n A string% s.", S); if (p) printf ("\\ n The string% s is entered.", P); Return 0;)

In general, we note that the GETS feature is often used to enter any data from the keyboard in the form of a string in order to further convert the SSCANF function to the desired format or for pre-analyzing entered data, for example:

#Include. #Include. #Include. INT MAIN () (char s; int x, err; do (printf ("\\ n Enter an integer -\u003e"); gets (s); er \u003d sscanf (s, "% d", & x); if (ERR ! \u003d 1) Printf ("\\ N Input Error.");) While (Err! \u003d 1); PrintF ("\\ n An integer is introduced -\u003e% d", x); Return 0;)

Row output to standard output device (monitor screen)

To display rows to a standard output device (monitor screen), you can use two functions. printf. and puts.. In the PrintF function, "% s" is transmitted as a format. Ease of using this feature is that in addition to the row, you can immediately display these types of data. Feature function puts. It is that after the line output, the next line is automatically moving.

Functions for working with rows

To convert rows in the Si language, the String library is provided. Each of the functions has its own recording format (prototype).

The most used features are considered in this article. - to read

Sample Programs (Listing) Working With Line

The program strings can be determined as follows:

  • as string constants;
  • as arrays characters;
  • through a pointer to the symbol type;
  • like lines arrays.

In addition, memory allocation should be provided for storing a string.

Any sequence of symbols concluded in double quotes "" is considered as string constant.

For correct output, any string must end with zero-symbol "\\ 0", the integer value of which is equal to 0. When declaring a string constant, the zero character is added to it automatically. Thus, the sequence of characters, which is a string constant, will be posted in the memory of the computer, including the zero byte.

Under the storage of the string highlights sequentially running cells of RAM. Thus, the string is an array of characters. To store the code of each string symbol 1 byte is given.

For placing in a string constant, some service characters are used symbolic combinations. So, if you need to turn on the double quotation symbol in the string, it must be preceded by the "Reverse Slash" symbol: '\\ "'.

String constants are placed in static memory. The initial address of the symbol sequence in double quotes is interpreted as the line address. String constants are often used to implement a user dialogue in features such as PrintF ().

When determining array of symbols You must tell the compiler required memory size.

char m;

The compiler can also independently determine the size of the symbol array if the array initialization is set when the string constant is declared:

char m2 \u003d;
char m3 \u003d ( "T", "and", "x", "and", "e", "", "d", "o", "l", "and", "n", "s", "", "P", "O", "L", "N", "Y", "", "C", "V", "E", "F", "E", "Ya", "", "M", "G", "L", "O", "Y", "\\ 0"};

In this case, the names M2 and M3 are pointers to the first elements of the arrays:

  • m2 Equivalent & M2
  • m2 equivalent to 'g'
  • m2 equivalent to 'o'
  • m3 Equivalent & M3
  • m3 is equivalent to 'x'

When declaring the array of characters and initialization, its string constant can explicitly specify the size of the array, but specified The array should be greater than the size of the initializing string constant:

char M2 \u003d. "The mountain peaks sleep in the darkness of the night.";

To specify a string you can use symbol type pointer.

char * m4;

In this case, the declaration of the M4 variable array can be assigned the address of the array:

m4 \u003d M3;
* M4 is equivalent to M3 \u003d "T"
* (M4 + 1) is equivalent to M3 \u003d "and"

Here M3 is a constant pointer. You can not change M3, as it would mean changing the position (address) of the array in memory, unlike M4.

For the pointer, you can use an increase operation (moving to the next character):

Arrays of symbol string

Sometimes there is a need to describe an array of symbol string. In this case, you can use the line index to access multiple different rows.

char * poet \u003d ( "The poet died!", "- Slap of honor -",
"Pal,", "slandered Molva ..."};

In this case, POET is an array consisting of four pointers to character strings. Each string of characters is a symbolic array, so there are four pointer to arrays. POET pointer refers to the first string:
* Poet. Equivalent "P",
* Poet [L] Equivalent "-" .

Initialization is performed according to the rules defined for arrays.
Texts in quotes are equivalent to the initialization of each row in the array. The comma separates neighboring
Sequences.
In addition, you can explicitly set the size of the strings of characters using a description similar to this:

char Poet;

The difference lies in the fact that such a form sets the "rectangular" array in which all lines have the same length.

Free massival

Description

char * Poet;


Determines the free array, where the length of each row is determined by the pointer that initializes this string. Free massif does not spend memory in vain.

Operations with strings

Most si language operations dealing with rows work with pointers. For accommodation, the strings of the characters must be:

  • select the RAM unit under an array;
  • initialize the string.

To secure memory for storing the string, the dynamic memory allocation functions can be used. At the same time, it is necessary to take into account the required string size:

char * Name;
name \u003d (char *) malloc (10);
scanf ("% 9S", Name);

To enter the string, the SCANF () function is used, and the entered string cannot exceed 9 characters. The last character will contain "\\ 0".

Functions input row

To enter a string, the SCANF () function can be used. However, the SCANF () function is designed rather to get the word, not the string. If you apply the "% s" format for input, the string is entered to (but not including) the next empty character, which can be a space, tab, or a row translation.

To enter a string, including spaces, a function is used.

char * Gets (char *);


Or its equivalent

char * gets_s (char *);

As an argument of the function, a pointer is transmitted to the string into which the input is carried out. The function asks the user to enter a string that it places into the array until the user clicks ENTER.

Row output functions

To display the lines, you can use the previously discussed function.

printF ("% s", str); // STR - pointer to the string

or in abbreviated format

printF (STR);

Function can also be used to output strings

iNT PUTS (char * s);

which prints the string S and translates the cursor to a new line (unlike printf ()). The PUTS () function can also be used to output string constants enclosed in quotes.

Symbol entry function

Function can be used to enter characters

cHAR GETCHAR ();


Which returns the value of the symbol entered from the keyboard. The specified function was used in previously reviewed examples for delaying the console window after the program is executed before pressing the key.

Symbol output function

Function can be used to output characters

char Putchar (Char);


Which returns the value of the output symbol and displays the symbol transmitted as an argument.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#Include.
#Include.
#Include.
iNT MAIN () (
char s, sym;
iNT COUNT, I;
System ("CHCP 1251");
System ("CLS");
PrintF ( "Enter the string: ");
gets_s (s);
PrintF ( "Enter the symbol:");
sym \u003d getchar ();
Count \u003d 0;
for (i \u003d 0; s [i]! \u003d "\\ 0"; i ++)
{
if (s [i] \u003d\u003d SYM)
COUNT ++;
}
PrintF ("in line \\ n");
Puts (s); // Row output
PrintF ("symbol");
Putchar (SYM); // Symbol output
PrintF ( "Meet% d times", Count);
getchar (); getchar ();
return 0;
}

Result of execution

The main functions of the standard String.h library

Main functions standard Library String.H are shown in the table.

Function Description

char * Strcat (Char * S1, Char * S2)

joins S2 to S1, returns S1

char * StrnCat (char * s1, char * s2, int n)

joins no more than N characters S2 to S1, completes the string with a "\\ 0" symbol, returns S1

cHAR * STRCY (char * s1, char * s2)

copies the S2 string in the string S1, including "\\ 0", returns S1
);
STRNCPY (M3, M1, 6); // does not add "\\ 0" at the end of the line
Puts ( "Result StrNCPY (M3, M1, 6)");
Puts (M3);
STRCPY (M3, M1);
Puts ( "STRCPY Result (M3, M1)");
Puts (M3);
Puts ( "Result STRCMP (M3, M1) is equal to");
PrintF ("% d", STRCMP (M3, M1));
STRNCAT (M3, M2, 5);
Puts ( "Result StrnCat (M3, M2, 5)");
Puts (M3);
STRCAT (M3, M2);
Puts ( "Result Strcat (M3, M2)");
Puts (M3);
Puts ( "The number of characters in a string M1 is Strlen (M1):");
PrintF ("% d \\ n", strlen (m1));
_strnset (M3, "F", 7);
Puts ( "Strnset Result (M3," F ", 7)");
Puts (M3);
_strset (M3, "K");
Puts ( "Result STRNSET (M3," K ")");
Puts (M3);
getchar ();
return 0;
}

Result of execution

Rows in C ++

The string is a sequence (array) of characters. If a single symbol is found in expression, it must be enclosed in single quotes. When used in expressions, the string lies in double quotes.A sign of the end of the line is a zero symbol \0 . C ++ lines can be described using characters (array of type elements char.), in which you should provide a place to store a feature of the end of the line.

For example, a description of a string of 25 characters should look like this:

You can describe the lines array:

The array of 3 lines of 25 bytes in each one is determined.

To work with pointers can be used ( char *). The address of the first symbol will be the initial value of the pointer.

Consider an example of declaring and output strings.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include "stdafx.h"
#Include.
using Namespace STD;
iNT MAIN ()
{
SETLOCALE (LC_ALL, "RUS");
// Describe 3 lines, S3 index
char S2 [20], * S3, S4 [30];
cout<< «s2=» ; cin >\u003e S2; // Enter Row S2
cout<< «s2=» << s2<< endl;
// Record in S3 address of the string where S4 is stored. Now in variables
// (pointers) S3 and S4 stored the value of the same address
S3 \u003d S4;
cout<< «s3=» ; cin >\u003e S3; // Entering string S3
// Conclusion on the screen of the strings S3 and S4, although as a result of the assignment S3 \u003d S4;
// Now S3 and S4 is the same thing.
cout<< «s3=» << s3<< endl;
cout<< «s4=» << s4<< endl;
sYSTEM ("PAUSE");
return 0;
}

The result of the program:

But it should be noted that if the user enters into one word variable separated by a space, the program will work otherwise:

The thing is that the function cin. Enters the strings to the gap met. More versatile function is getLine.

cIN.GetLine (char * s, int n);

Designed to enter the row keyboard s. with spaces, in the row should not be more n. Symbols. Consequently, for the correct input of lines containing a space, it is necessary to replace in our program cIN \u003e\u003e S. on the cIN.GetLine (S, 80).

Operations on strings

The string can be processed as an array of characters using arrays algorithms or using special row processing functions, some of which are shown below. To work with these rows, you must connect the library cString.

To convert a number to a string, you can use the function sprintf. from the library stdio.h..

Some functions of working with rows:

Prototype function Description of the function
size_T Strlen (Const Char * S) calculates the length of the string S in bytes.
char * Strcat (Char * Dest, Const Char * SCR) joins the SRC string at the end of the DEST line, the resulting period is returned as a result
char * Strcpy (Char * Dest, Const Char * SCR) copies the SCR string to the memory location that DEST indicates
char Strncat (Char * Dest, Const Char * Dest, Size_t Maxlen) joins the Maxlen string of the SRC string symbols at the end of the DEST line
char * Strncpy (char * dest, const char * scr, size_t maxlen) copies Maxlen SRC string symbols into memory location that DEST indicates
iNT CTRCMP (Const Char * S1, Const Char * S2) compare two lines in lexicographic order, taking into account the differences in the uppercase and lowercase letters, the function returns 0 if the strings match, returns - 1, if S1 is located in an alphabetic order earlier than S2, and 1 - in the opposite case.
iNT STRNCMP (Const Char * S1, Const Char * S2, Size_t Maxlen) compare Maxlen characters of two lines in a lexicographic order, the function returns 0 if the lines match, returns - 1 if S1 is located in the order ordered by alphabetically earlier than S2, and 1 - in the opposite case.
double ATOF (Const Char * S) converts a string into a real number, in case of unsuccessful conversion, the number 0 is returned
lONG ATOL (Const char * s) converts a string into a long integer, in case of unsuccessful conversion 0
char * Strchr (Const Char * S, Int C); returns the pointer to the first occurrence of the symbol c. in the string on which indicates s.. If symbol c. not found returning null
char * Strupr (char * s) converts the string symbols to which s, in the symbols of the top register, after which returns it

String Data Type

In addition to working with rows, as with an array of characters, in C ++ there is a special data type string. To enter variables of this type can be used. cin., or special function getLine.

getLine (CIN, S);

Here s. - the name of the type entered variable string.

When describing a variable of this type, you can immediately assign the value of this variable.

sTRING VAR (S);

Here var. - variable name, s. - String constant. As a result of this operator, a variable is created var. Type stringand it records the value of the string constant s.. For example,

string V (Hello);

Created string v.in which the value is recorded Hello..

Access to i-MU Element string s. Type string carried out in a standard way s [i]. Over lines of type string The following operations are defined:

  • assignments, for example s1 \u003d s2;
  • rowing strings (S1 + \u003d S2 or S1 \u003d S1 + S2) - adds to string S1 string S2, the result is stored in the string S1, an example of rowing row:
  • comparisons of rows based on lexicographic order: S1 \u003d S2, S1! \u003d S2, S1 s2, S1.<=s2, s1>\u003d S2 - the result will be a logical value;

When processing type strings string You can use the following functions:

  • s.SUBSTR (POS, LENGTH) - Returns a substring from the line s.starting with the number pOS. Long length. symbols;
  • s.Empty () - Returns True if the string s. empty, false - otherwise;
  • s.INSERT (POS, S1) - Inserts a string s1. In a string s.starting from the position pOS.;
  • s.Remove (POS, LENGTH) - removes from the line s. Constra length. Long pOS. symbols;
  • s.FIND (S1, POS) - Returns the number of the first row s1. In a string s., search starts with numbers pOS., Parameter pOS. may be absent, in this case the search comes from the beginning of the line;
  • s.FINDFIRST (S1, POS) - Returns the number of the first entry of any symbol from the string s1. In a string s., search starts with numbers pOS.which may be absent.

Russian language for strings

I think you have already noticed that when withdrawing Russian letters, "left" symbols appear in the console. In order to avoid this misunderstanding, you must use a third-party function. Chartooema.. We connect the library windows.h.It is necessary for our function to convert strings to another encoding. Also, we need an additional symbolic array. The source code of the program will look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include "stdafx.h"
#Include.
#Include.
using Namespace STD;
iNT MAIN ()
(SETLOCALE (LC_ALL, "RUS");
char S [255] \u003d ( "I need to convert"} ;
char * pre \u003d new char [255];
CHARTOOEEEMA (S, PRE); // Transform
cout<< s;
dELETE PRE;
system ("PAUSE \u003e\u003e Void");
return 0;
}

The method is just described quite not convenient. But there is a simpler solution for the "Russian" problem. As you can see, the program uses the setlocale () function, instead more convenient to enter the main function of the next design.



Did you like the article? Share it