How to set up smartphones and PCs. Informational portal

String data type in C. C strings

Declaring strings

A C string is a one-dimensional array of characters, the last element of which is a null terminated string (null terminated string).

Declaring a variable of type string in the C language is possible in three ways, two of which initialize the string during the declaration.

First way:

Character array declarations (remember to add space for the trailing null):

Char s;

Second way:

Assign an initial value to a string variable (in this case, the compiler can calculate the length of the string itself):

Char s = "Example of string initialization";

A string constant is written to the right of the assignment sign. A zero ('\ 0') is automatically appended to the end of the line. Character string constants are placed in a static memory class.

Third way:

An implicit indication that an array is being used. To the left of the assignment sign is a pointer to a symbol:

Char * s = "Second initialization option";

The s variable will be a pointer to the location in RAM where the string constant is located. There is a potential mistake in this form of notation, which is that a pointer to a character is often called a string. The entry below is only a pointer to a character, since there is no space for the string to be placed:

Char * s;

Entering a string from a standard input device (keyboard)

There is a set of functions for working with strings. For input from a standard input device (keyboard), library functions from the standard input-output module are most often used: scanf and gets.

To enter a string using a function scanf, uses the format « % s» , and note that no address sign is used in front of the string identifier « & » , since the one-dimensional array is already represented by a pointer to its beginning:

Scanf ("% s", s);

Function gets () reads characters until it reaches a newline character. The function accepts all characters up to the line feed character, but does not include it. A trailing zero ('\ 0') is appended to the end of the line. Function gets () puts a sequence of characters read from the keyboard into a parameter of type string and returns a pointer to this string (if the operation was successful), or NULL (in case of an error). In the example below, upon successful completion of the operation, two identical lines will be displayed on the screen:

#include int main () (char s; char * p; p = gets (s); printf ("\ n String% s entered.", s); if (p) printf ("\ n String% s entered.", p); return 0;)

In passing, we note that the gets function is often used to enter any data from the keyboard as a string for the purpose of further converting the sscanf function to the desired format or for preliminary analysis of the input data, for example:

#include #include #include int main () (char s; int x, err; do (printf ("\ n Enter an integer ->"); gets (s); err = sscanf (s, "% d", & x); if (err ! = 1) printf ("\ n Input error.");) While (err! = 1); printf ("\ n Integer entered ->% d", x); return 0;)

Outputting lines to standard output (monitor screen)

Two functions can be used to output strings to standard output (monitor screen) printf and puts... In the printf function, "% s" is passed as the format. The convenience of using this function lies in the fact that in addition to a string, you can immediately output data of other types. Feature Feature puts lies in the fact that after the output of the line, the transition to the next line occurs automatically.

Functions for working with strings

The string library is provided for converting strings in the C language. Each of the functions has its own recording format (prototype).

The most used functions are covered in this article. - read

An example of programs (listing) working with strings

The programmer says:

Hello! I read your article. I was very sad and funny at the same time. This phrase of yours is especially killing: "Since a variable of the char type is often used as an array, the number of possible values ​​is determined." 😆 😆 😆
I am not laughing at you. Building a website is truly a feat. I just want to support you with advice and point out a few mistakes.

1. The value of a variable of type char is assigned as follows:

Here:

Char a = * "A";

The pointer to the array is unaddressed and, as a result, the value of the first element of the array is returned, i.e. ‘A’

2. Zeroing is done as follows:

Char a = NULL;
char b = ();

// And this is how the line in the body of the program is cleared

"" - this character is called a null terminator. It is placed at the end of the line. You yourself, without knowing it, filled the s1 array from your article with this symbol. But it was possible to assign this symbol only to the zero element of the array.

3. Feel free to use terminology.
The = sign is an assignment operation.
The * sign is a callout operation.
I mean this fragment of the article: "Everything turned out to be so simple, before the = sign you had to put a * sign and you had to declare the element number (zero corresponds to the first)"

Do not misunderstand me, the article as it stands cannot exist. Do not be lazy, rewrite it.
You have a great responsibility! I'm serious. The pages of your site were included in the first page of Yandex search results. A lot of people have already started repeating mistakes after you.

Good luck! You can handle it!

:
I have known this for a long time, it's just hard to re-read 200 articles all the time to fix something. And some rude types write in such a way that even knowing what is better to fix, there is no desire to fix it at all.

I will be happy to fix other bugs as well. correct inaccuracies if they pop up. I appreciate your help. Thank you. I have known this for a long time, it's just hard to re-read 200 articles all the time to fix something. And some rude types write in such a way that even knowing what is better to fix, there is no desire to fix it at all.
With your char b = (); This is not zeroing at all. would have checked at least b.
if we talk about the null character ""; I knew very well when I filled out the line with them and the purpose was to show real cleansing, and not visible to the eye, because the line includes garbage, which sometimes gets in the way. You would be more careful with the terms yourself, "null-termination character" or simply "null character", not a terminator))) And the terminator character sounds just cool.

I am modernizing the article, but I will not go over to someone else's style. If I think that it is clearer for a beginner this way, and not the way he wants, then I will leave it that way. You, too, do not misunderstand me. The word "sign" is much easier for a weak beginner to understand and remember than the definition and name of each sign. There is absolutely no mistake in this, the sign is - the sign. Less emphasis on one thing gives more emphasis on the other.

I will be happy to fix other bugs as well. correct inaccuracies if they pop up. I appreciate your help. Thank you.

Hello again!
I want to clarify. The term "zero-terminator" (terminator) was used by my teacher at the university. Apparently this is old school!
As for nulling lines.
char b = (); This is really zeroing. The entire array is filled with zeros. Do not believe it - check it out!
If we consider a string in its natural, everyday sense, then the "empty" will be the string in which there is not a single character. Therefore, in 99.9% of cases, it is sufficient to prepend a null character. Usually, the processing of a string goes up to the first null character, and which characters follow it is no longer important. I understand that you wanted to zero out the string. I just decided to offer a time-tested classic version.

:
When "Usually the processing of the string goes up to the first null character, and what characters follow it is no longer important" - yes, the string is reset to zero
If we consider the "real zeroing of all cells in the string (which I wrote about)" - no, not zeroing, and even the first character is not zero. I checked this option. MinGW (CodeBlock) - the whole array gives the character "a"
I do not think that this is a reason for controversy.

In this tutorial we will be discussing C-style strings, you may have already seen these lines on our website or in any other tutorial. In fact, C strings are just arrays of characters, but with their own specifics, so we always know where the end of the string is. In this article we will look at several functions for working with strings, for example, you - copy, concatenate, get the length of a string.

What are strings?

Note that along with C-style strings, which are essentially simple arrays, there are also string literals such as this "literal". In reality, both strings and literals are simply sets of characters located side by side in the computer's memory. But there is still a difference between arrays and literals, literals cannot be changed and strings - you can.

Any function that accepts a C-style string can also take a literal as a parameter. There are also some entities in B that may look like strings, when in fact they are not. I'm talking about characters now, they are enclosed in single quotes, here's an example - "a", as you can see, this is not a string. A character can be assigned to a string at a specific location, but characters cannot be processed as a string. If you remember, arrays work like pointers, so if you pass one character per string, it will be considered an error.

From all of the above, you should understand that strings are arrays of characters and string literals are words surrounded by double quotes. Here's another example of a literal:

"This is a static string"

Have you forgotten about the specifics of strings that were mentioned a little above? So, C strings must always be terminated with a null character, literally "\ 0". Therefore, to declare a string consisting of 49 letters, you need to reserve an additional cell for a null character:

Char myString;

As you can see from the example, the length of the array is 50 characters, 49 of which will take a string and one, the last will take a null character. It is important to remember that there must always be a null character at the end of C strings, just like there is a period at the end of every sentence. Although the null character is not displayed on line output, it still takes up memory space. So, technically, you could only store 49 letters in an array of fifty elements, because the last character is needed to terminate the string. In addition, pointers can also be used as strings. If you read the article on, you can do something like this:

Char * myString; // pointer of type char myString = malloc (sizeof (* myString) * 64); // memory allocation

In this example, we have allocated 64 memory locations for the myString array. Use the free () function to free memory.

Free (myString);

Using strings

Strings are useful when you need to perform various operations with text information. For example, if you want the user to enter a name into the program, you must use a string. Using scanf () to input a string works, but it can cause a buffer overflow. After all, the input string may be larger than the size of the buffer string. There are several ways to solve this problem, but the easiest way is to use, which is declared in the header file .

When it reads input from the user, it will read all but the last character. After that, at the end of the read line, it will place a null terminator. The fgets () function will read characters until the user hits Enter. Let's see an example using fgets ():

#include int main () (char myString; // long string printf ("Enter a long string:"); fgets (myString, 100, stdin); // read a string from the input stream printf ("You entered the following string:% s", myString); getchar ();)

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

The result of the program:

<ВВОД>...

As you can see, from the output of the program, a newline character "\ n" got into the input line. This happened due to the fact that fgets () read the press of the Enter button into the string myString and exited. This means that you may need to manually remove the newline character. One way to do this is by character iteration. Let's modify the program and remove the newline character:

#include int main () (char myString; // long string printf ("Enter a long string:"); fgets (myString, 100, stdin); // read a string from the input stream int i; for (i = 0; i< 100; i++) { if (myString[i] == "\n") { myString[i] = "\0"; break; } } printf("Вы ввели следующую строку: %s", myString); getchar(); }

Please note that if the input string contains less than 100 characters, then the newline character will also be included in the string. Therefore, we can remove this character using simple iteration. We added a loop to the program in which we iterate over the characters of the string, lines 12-19... And when we encounter a newline character, we replace it with a null character, line 16... The result of the program:

Enter the long line: Fate leaves its mark You have entered the following line: Fate leaves its mark Press to close this window<ВВОД>...

That's all for now. In the next article I will tell you about special functions for working with strings.

PS: We all love to watch different video recordings, but sometimes it happens that it is not always possible to play some video file formats. 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.

34

--- C # Manual --- Strings

In terms of regular programming, the string string data type is one of the most important in C #. This type defines and supports character strings. In a number of other programming languages, a string is an array of characters. And in C #, strings are objects. Therefore, the string type is a reference type.

Constructing strings

The easiest way to build a character string is to use a string literal. For example, the following line of code assigns the string reference variable str to the string literal reference:

String str = "Sample string";

In this case, the str variable is initialized with the "Example string" character sequence. You can also create an object of type string from an array of type char. For instance:

Char chararray = ("e", "x", "a", "m", "p", "l", "e"); string str = new string (chararray);

Once an object of type string is created, it can be used wherever a string of quoted text is required.

String persistence

Oddly enough, the contents of an object of type string cannot be changed. This means that once created, the character sequence cannot be changed. But this limitation contributes to a more efficient implementation of character strings. Therefore, this, at first glance, obvious disadvantage actually turns into an advantage. So, if a string is required as a variation of an existing string, then for this purpose a new string should be created containing all the necessary changes. And since unused string objects are automatically garbage collected, you don't even have to worry about the fate of unnecessary strings.

It should be emphasized, however, that variable string references (that is, objects of type string) are subject to change, and therefore they can refer to another object. But the content of the string object itself does not change after its creation.

Let's consider an example:

Static void addNewString () (string s = "This is my stroke"; s = "This is new stroke";)

Let's compile the application and load the resulting assembly into the ildasm.exe utility. The figure shows the CIL code that will be generated for the void addNewString () method:

Note that there are multiple calls to the ldstr (line load) opcode. This CIL ldstr opcode instructs to load a new string object into the managed heap. As a result, the previous object that contained the value "This is my stroke" will eventually be garbage collected.

Working with strings

In class System.String a set of methods is provided for determining the length of character data, finding a substring in the current string, converting characters from uppercase to lowercase and vice versa, etc. We'll look at this class in more detail next.

String Field, Indexer, and Property

The String class has a single field defined:

Public static readonly string Empty;

The Empty field denotes an empty string, i.e. a string that does not contain characters. This is in contrast to an empty String reference, which is simply made to a non-existent object.

In addition, the String class defines a single read-only indexer:

Public char this (get;)

This indexer allows you to get the character at the specified index. Indexing of strings, like arrays, starts at zero. String objects are persistent and immutable, so it makes sense that the String class supports a read-only indexer.

Finally, the String class defines a single read-only property:

Public int Length (get;)

The Length property returns the number of characters in a string. The example below demonstrates the use of an indexer and the Length property:

Using System; class Example (static void Main () (string str = "Simple string"; // Get the length of the string and the 6th character in the string using the indexer Console.WriteLine ("The length of the string is (0), the 6th character is" (1) "" , str.Length, str);))

String class operators

The String class overloads the following two operators: == and! =. The == operator is used to test two character strings for equality. When the == operator is applied to object references, it usually checks to see if both references are made to the same object. And when the == operator is applied to references to objects of type String, then the contents of the strings themselves are compared for equality. The same applies to the! = Operator. When applied to references to objects of type String, the contents of the strings themselves are compared for inequality. At the same time, other relational operators, including =, compare references to objects of type String in the same way as to objects of other types. And in order to check if one string is larger than another, you should call the Compare () method, which is defined in the String class.

As will become clear, many character string comparisons use cultural information. But this does not apply to the == and! = Operators. After all, they simply compare the ordinal values ​​of characters in strings. (In other words, they compare binary character values ​​that are not culturally modified, ie, cultured.) Therefore, these operators perform string comparisons in a case-insensitive and culture-insensitive manner.

String class methods

The following table lists some of the more interesting methods of this class, grouped by purpose:

Methods for working with strings
Method Structure and overloads Appointment
Compare strings
Compare () public static int Compare (string strA, string strB)

Public static int Compare (string strA, string strB, bool ignoreCase)

Public static int Compare (string strA, string strB, StringComparison comparisonType)

Public static int Compare (string strA, string strB, bool ignoreCase, CultureInfo culture)

Static method, compares string strA with string strB. Returns a positive value if strA is greater than strB; negative value if string strA is less than string strB; and zero if strA and strB are equal. The comparison is case-sensitive and culturally sensitive.

If ignoreCase is the Boolean value true, the comparison ignores the difference between uppercase and lowercase letters. Otherwise, these differences are taken into account.

The comparisonType parameter specifies a specific way to compare strings. The CultureInfo class is defined in the System.Globalization namespace.

public static int Compare (string strA, int indexA, string strB, int indexB, int length)

Public static int Compare (string strA, int indexA, string strB, int indexB, int length, bool ignoreCase)

Public static int Compare (string strA, int indexA, string strB, int indexB, int length, StringComparison comparisonType)

Public static int Compare (string strA, int indexA, string strB, int indexB, int length, bool ignoreCase, CultureInfo culture)

Compares parts of strA and strB strings. The comparison starts with the string elements strA and strB and includes the number of characters specified by the length parameter. The method returns a positive value if the part of the strA string is greater than the part of the strB string; negative value if part of string strA is less than part of string strB; and zero if the compared parts of strA and strB are equal. The comparison is case-sensitive and culturally sensitive.

CompareOrdinal () public static int CompareOrdinal (string strA, string strB)

Public static int CompareOrdinal (string strA, int indexA, string strB, int indexB, int count)

Does the same as the Compare () method, but disregards local settings

CompareTo () public int CompareTo (object value)

Compares the calling string to the string representation of the value object. Returns a positive value if the calling string is greater than the string value; negative value if the calling string is less than the string value; and zero if the compared strings are equal

public int CompareTo (string strB)

Compares the calling string with the string strB

Equals () public override bool Equals (object obj)

Returns the Boolean value true if the calling string contains the same sequence of characters as the string representation of obj. Performs a case-sensitive ordinal comparison but culture-insensitive

public bool Equals (string value)

Public bool Equals (string value, StringComparison comparisonType)

Returns the Boolean value true if the calling string contains the same sequence of characters as the string value. A case-sensitive ordinal comparison is performed, but culture-insensitive. The comparisonType parameter defines a specific way to compare strings

public static bool Equals (string a, string b)

Public static bool Equals (string a, string b, StringComparison comparisonType)

Returns the boolean value true if string a contains the same sequence of characters as string b. A case-sensitive ordinal comparison is performed, but culture-insensitive. The comparisonType parameter defines a specific way to compare strings

Concatenate (join) strings
Concat () public static string Concat (string str0, string str1);

public static string Concat (params string values);

Combines separate instances of strings into one string (concatenation)
Search in string
Contains () public bool Contains (string value) A method that allows you to determine if a string contains a specific substring (value)
StartsWith () public bool StartsWith (string value)

Public bool StartsWith (string value, StringComparison comparisonType)

Returns the boolean value true if the calling string begins with the substring value. Otherwise, the Boolean value false is returned. The comparisonType parameter specifies how the search is performed.

EndsWith () public bool EndsWith (string value)

Public bool EndsWith (string value, StringComparison comparisonType)

Returns the boolean value true if the calling string ends with the substring value. Otherwise, it returns the boolean value false. The comparisonType parameter specifies a specific search method

IndexOf () public int IndexOf (char value)

Public int IndexOf (string value)

Finds the first occurrence of a specified substring or character in a string. If the desired character or substring is not found, then the value -1 is returned

public int IndexOf (char value, int startIndex)

Public int IndexOf (string value, int startIndex)

Public int IndexOf (char value, int startIndex, int count)

Public int IndexOf (string value, int startIndex, int count)

Returns the index of the first occurrence of the character or substring value in the calling string. The search starts at the element at index startIndex and spans over the number of elements identified by count (if supplied). The method returns -1 if the search character or substring is not found

LastIndexOf () Overloaded versions are similar to IndexOf () method

Same as IndexOf, but finds the last occurrence of a character or substring, not the first

IndexOfAny () public int IndexOfAny (char anyOf)

Public int IndexOfAny (char anyOf, int startIndex)

Public int IndexOfAny (char anyOf, int startIndex, int count)

Returns the index of the first occurrence of any character in the anyOf array found in the calling string. The search starts at the element at index startIndex and spans over the number of elements identified by count (if any). The method returns -1 if no match is found with any of the characters from the anyOf array. The search is carried out in an ordinal way.

LastIndexOfAny Overloaded versions are similar to IndexOfAny () method

Returns the index of the last occurrence of any character from the anyOf array found in the calling string

Splitting and concatenating strings
Split public string Split (params char separator)

Public string Split (params char separator, int count)

A method that returns a string array with substrings present in this instance, separated from each other by elements from the specified char or string array.

In the first form of the Split () method, the calling string is split into its component parts. As a result, an array is returned containing the substrings obtained from the calling string. The characters that delimit these substrings are passed in the separator array. If the separator array is empty or refers to an empty string, then a space is used as the substring separator. And in the second form of this method, the number of substrings specified by the count parameter is returned.

public string Split (params char separator, StringSplitOptions options)

Public string Split (string separator, StringSplitOptions options)

Public string Split (params char separator, int count, StringSplitOptions options)

Public string Split (string separator, int count, StringSplitOptions options)

In the first two forms of the Split () method, the calling string is split into parts and an array is returned containing the substrings obtained from the calling string. The characters separating these substrings are passed in the separator array. If the separator array is empty, then a space is used as the separator. And in the third and fourth forms of this method, the number of rows, limited by the count parameter, is returned.

But in all forms, the options parameter denotes a specific way to handle empty lines that are generated when two delimiters are side by side. Only two values ​​are defined in the StringSplitOptions enumeration: None and RemoveEmptyEntries... If options is None, empty strings are included in the end result of splitting the original string. And if the options parameter is set to RemoveEmptyEntries, then empty strings are excluded from the final result of splitting the original string.

Join () public static string Join (string separator, string value)

Public static string Join (string separator, string value, int startIndex, int count)

Constructs a new string by combining the contents of an array of strings.

The first form of the Join () method returns a string consisting of the concatenated substrings passed in the value array. The second form also returns a string consisting of the substrings passed in the value array, but they are concatenated in a certain number of counts, starting at the value array element. In both forms, each subsequent line is separated from the previous line by a separator specified by the separator parameter.

Padding and trimming lines
Trim () public string Trim ()

Public string Trim (params char trimChars)

A method that allows you to remove all occurrences of a specific character set from the beginning and end of the current line.

In the first form of the Trim () method, leading and trailing spaces are removed from the calling string. And the second form of this method removes the leading and trailing occurrences in the calling character string from the trimChars array. In both forms, the resulting string is returned.

PadLeft () public string PadLeft (int totalWidth)

Public string PadLeft (int totalWidth, char paddingChar)

Allows you to pad the string with characters to the left.

The first form of the PadLeft () method inserts spaces on the left side of the calling line so that its total length is equal to the value of the totalWidth parameter. And in the second form of this method, the characters denoted by the paddingChar parameter are inserted from the left side of the calling line so that its total length is equal to the value of the totalWidth parameter. In both forms, the resulting string is returned. If totalWidth is less than the length of the calling string, a copy of the unmodified calling string is returned.

PadRight () Same as PadLeft ()

Allows you to pad the string with characters to the right.

Insert, delete and replace strings
Insert () public string Insert (int startIndex, string value)

Used to insert one row into another, where value denotes the row to insert into the calling row at index startIndex. The method returns the resulting string.

Remove () public string Remove (int startIndex)

Public string Remove (int startIndex, int count)

Used to remove part of a string. In the first form of the Remove () method, removal is performed starting at the location indicated by startIndex and continuing to the end of the line. And in the second form of this method, the number of characters specified by the count parameter is removed from the string, starting at the position indicated by the startIndex index.

Replace () public string Replace (char oldChar, char newChar)

Public string Replace (string oldValue, string newValue)

Used to replace part of a string. In the first form of the Replace () method, all occurrences of oldChar in the calling string are replaced with newChar. And in the second form of this method, all occurrences of the string oldValue in the calling string are replaced with the string newValue.

Case change
ToUpper () public string ToUpper ()

Capitalizes all letters in the calling string.

ToLower () public string ToLower ()

Lowercase all letters in the calling string.

Getting a substring from a string
Substring () public string Substring (int startIndex)

Public string Substring (int startIndex, int length)

In the first form of the Substring () method, the substring is retrieved from the position indicated by the startIndex parameter to the end of the calling string. And the second form of this method extracts a substring consisting of the number of characters specified by the length parameter, starting at the location indicated by the startIndex parameter.

The following program example uses several of the above methods:

Using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication1 (class Program (static void Main (string args) (// Compare the first two lines string s1 = "this is a string"; string s2 = "this is text, and this is a string"; if (String.CompareOrdinal (s1, s2) ! = 0) Console.WriteLine ("Strings s1 and s2 are not equal"); if (String.Compare (s1, 0, s2, 13, 10, true) == 0) Console.WriteLine ("At the same time, they have same text "); // String concatenation Console.WriteLine (String.Concat (" \ n "+" One, two "," three, four ")); // Search in a string // First occurrence of a substring if (s2. IndexOf ("this")! = -1) Console.WriteLine ("The word \" this \ "was found in the line, it is" + "at: (0) position", s2.IndexOf ("this")); / / Last occurrence of the substring if (s2.LastIndexOf ("this")! = -1) Console.WriteLine ("The last occurrence of the word \" this \ "is" + "at (0) position", s2.LastIndexOf ("this" )); // Search from an array of characters char myCh = ("Y", "x", "t"); if (s2.IndexOfAny (myCh)! = -1) Console.WriteLine ("One of the characters from the array ch "+" found in the current line at position (0) ", s2.IndexOfAny (myCh)); // Determine if the string begins with a given substring if (s2.StartsWith ("this is text") == true) Console.WriteLine ("Substring found!"); // Determine if the string contains a substring // using the example of defining the user's OS string myOS = Environment.OSVersion.ToString (); if (myOS.Contains ("NT 5.1")) Console.WriteLine ("Your operating system is Windows XP"); else if (myOS.Contains ("NT 6.1")) Console.WriteLine ("Your Windows 7 operating system"); Console.ReadLine (); )))

A little about string comparison in C #

Of all character string processing operations, it is likely that one string is compared to another. Before considering any methods for comparing strings, the following should be emphasized: string comparisons can be performed in the .NET Framework in two main ways:

    First, the comparison can reflect the customs and norms of a particular cultural setting, which are often cultural settings that come into effect when the program is run. This is standard behavior for some, but not all, comparison methods.

    And secondly, the comparison can be performed regardless of the settings of the cultural environment only by the ordinal values ​​of the characters that make up the string. Generally speaking, culturally neglected string comparisons use lexicographic order (and linguistic considerations) to determine whether one string is greater than, less than, or equal to another string. In an ordinal comparison, strings are simply ordered based on the unchanged value of each character.

Because of the different cultural and ordinal comparison methods for string comparisons, and the implications of each such comparison, it is strongly recommended that you follow the best practices currently offered by Microsoft. After all, choosing the wrong way to compare strings can lead to incorrect operation of the program when it is used in an environment that differs from the one in which it was developed.

The choice of how to compare character strings is a very important decision. As a general rule and without exception, culturally appropriate string comparison should be chosen if it is for the purpose of displaying the result to the user (for example, to display a series of strings sorted in lexicographic order). However, if the strings contain fixed information that is not meant to be modified to accommodate cultural differences, such as a file name, keyword, website address, or security value, then you should choose an ordinal string comparison. Of course, the specifics of a particular application being developed will dictate the choice of an appropriate way to compare character strings.

The String class provides a variety of string comparison methods, which are listed in the table above. The most versatile of these is the Compare () method. It allows you to compare two strings in whole or in part, case sensitive or case insensitive, the way of comparison defined by the type parameter StringComparison as well as culture information provided with a type parameter CultureInfo.

Overloads of Compare () that do not contain a parameter of type StringComparison perform a case-sensitive and culture-sensitive comparison of character strings. And in those overloaded variants that do not contain a parameter of type CultureInfo, the culture information is determined by the current runtime.

The StringComparison type is an enumeration that defines the values ​​shown in the table below. Using these values, you can arrange string comparisons to suit the needs of your particular application. Therefore, adding a parameter of type StringComparison extends the Compare () method and other comparison methods such as Equals (). It also makes it possible to unambiguously indicate how the strings are supposed to be compared.

Because of the differences between culture-sensitive string comparisons and ordinal comparisons, it is very important to be as accurate as possible in this regard.

Values ​​defined in the StringComparison enumeration
Meaning Description
CurrentCulture String comparisons are made using the current culture settings
CurrentCultureIgnoreCase String comparisons are made using the current culture settings, but not case sensitive
InvariantCulture Comparison of strings is done using immutable, i.e. universal data on the cultural environment
InvariantCultureIgnoreCase Comparison of strings is done using immutable, i.e. universal data on cultural environment and case-insensitive
Ordinal String comparisons are made using the ordinal values ​​of the characters in the string. In this case, the lexicographic order may be violated, and the conventions adopted in a separate cultural environment are ignored.
OrdinalIgnoreCase Comparison of strings is performed using ordinal values ​​of characters in the string, but not case sensitive

In either case, the Compare () method returns a negative value if the first string being compared is less than the second; positive value if the first compared string is greater than the second; and finally zero if both strings being compared are equal. Although the Compare () method returns zero if the strings being compared are equal, it is generally best to use the Equals () method or the == operator to determine if character strings are equal.

The point is that the Compare () method determines the equality of the compared strings based on their sort order. For example, if you are comparing strings in a culture-sensitive manner, then both strings may be the same in the order they are sorted, but not substantially equal. By default, string equality is determined in the Equals () method based on ordinal character values ​​and is culturally insensitive. Therefore, by default, both strings are compared in this method for absolute, character-by-character equality, just like in the == operator.

Although the Compare () method is more versatile, it is easier to use the CompareOrdinal () method for simple ordinal comparisons of character strings. Finally, keep in mind that the CompareTo () method only compares culture-wise strings.

The following program demonstrates the use of the Compare (), Equals (), CompareOrdinal () methods, and the == and! = Operators to compare character strings. Note that the first two comparison examples illustrate the difference between culturally sensitive string comparisons and ordinal comparisons in English:

Using System; class Example (static void Main () (string str1 = "alpha"; string str2 = "Alpha"; string str3 = "Beta"; string str4 = "alpha"; string str5 = "alpha, beta"; int result; / / First, demonstrate the difference between culture-aware string comparison and ordinal comparison result = String.Compare (str1, str2, StringComparison.CurrentCulture); Console.Write ("Culture-aware string comparison:"); if (result 0 ) Console.WriteLine (str1 + "greater than" + str2); else Console.WriteLine (str1 + "equal to" + str2); result = String.Compare (str1, str2, StringComparison.Ordinal); Console.Write ("Ordinal comparison lines: "); if (result 0) Console.WriteLine (str1 +" greater than "+ str2); else Console.WriteLine (str1 +" equal to "+ str4); // Use the CompareOrdinal () method result = String.CompareOrdinal ( str1, str2); Console.Write ("Compare strings using the CompareOrdinal () method: \ n"); if (result 0) Console.WriteLine (str1 + "more" + str2); else Console.WriteLine (str 1 + "equals" + str4); Console.WriteLine (); // Determine string equality using the == operator // This is an ordinal comparison of character strings if (str1 == str4) Console.WriteLine (str1 + "==" + str4); // Determine string inequality using the operator! = If (str1! = Str3) Console.WriteLine (str1 + "! =" + Str3); if (str1! = str2) Console.WriteLine (str1 + "! =" + str2); Console.WriteLine (); // Perform a case-insensitive ordinal comparison of strings // using the Equals () method if (String.Equals (str1, str2, StringComparison.OrdinalIgnoreCase)) Console.WriteLine ("Comparing strings using the Equals () method with the" + "OrdinalIgnoreCase parameter: \ n "+ str1 +" is equal to "+ str2); Console.WriteLine (); // Compare parts of strings if (String.Compare (str2, 0, str5, 0, 3, StringComparison.CurrentCulture)> 0) (Console.WriteLine ("Compare strings based on the current culture:" + "\ n3 first characters of the line "+ str2 +" is greater than the first 3 characters of the string "+ str5);)))

Executing this program produces the following result:

Please suspend AdBlock on this site.

So, strings in C. There is no separate data type for them, as is done in many other programming languages. In C, a string is an array of characters. To mark the end of a line, the "\ 0" character is used, which we talked about in the last part of this lesson. It is not displayed on the screen in any way, so you won't be able to look at it.

String creation and initialization

Since a string is an array of characters, declaring and initializing a string is the same as for one-dimensional arrays.

The following code illustrates various ways to initialize strings.

Listing 1.

Char str; char str1 = ("Y", "o", "n", "g", "C", "o", "d", "e", "r", "\ 0"); char str2 = "Hello!"; char str3 = "Hello!";

Fig. 1 Declaring and initializing strings

On the first line, we just declare a ten character array. It's not even quite a string, since it does not have the null character \ 0 as long as it is just a character set.

Second line. The simplest way to initialize in the forehead. We declare each character separately. The main thing here is not to forget to add the null character \ 0.

The third line is analogous to the second line. Pay attention to the picture. Because there are fewer characters in the string on the right than the elements in the array, the rest of the elements will be filled with \ 0.

Fourth line. As you can see, there is no size specified here. The program will calculate it automatically and create an array of characters of the required length. This will insert the null character \ 0 last.

How to output a string

Let's add the code above to a full-fledged program that will display the created lines on the screen.

Listing 2.

#include int main (void) (char str; char str1 = ("Y", "o", "n", "g", "C", "o", "d", "e", "r", " \ 0 "); char str2 =" Hello! "; Char str3 =" Hello! "; For (int i = 0; i< 10; i = i + 1) printf("%c\t",str[i]); printf("\n"); puts(str1); printf("%s\n",str2); puts(str3); return 0; }


Fig. 2 Different ways of displaying a string on the screen

As you can see, there are several basic ways to display a string on the screen.

  • use printf function with% s specifier
  • use puts function
  • use the fputs function, specifying stdout as the second parameter.

The only caveat with the puts and fputs functions. Note that the puts function wraps the output to the next line, but the fputs function does not.

As you can see, the conclusion is quite simple.

Input strings

Inputting strings is a little more complicated than outputting. The simplest way would be the following:

Listing 3.

#include int main (void) (char str; gets (str); puts (str); return 0;)

The gets function pauses the program, reads a string of characters entered from the keyboard, and places it in a character array, the name of which is passed to the function as a parameter.
The completion of the gets function will be the character corresponding to the enter key and written to the string as a null character.
Have you noticed the danger? If not, the compiler will kindly warn you about it. The point is that the gets function exits only when the user presses the enter key. This is fraught with the fact that we can go beyond the array, in our case - if more than 20 characters are entered.
By the way, buffer overflow errors were previously considered the most common type of vulnerability. They are still found today, but it has become much more difficult to use them to hack programs.

So what do we have. We have a task: to write a string into an array of limited size. That is, we must somehow control the number of characters entered by the user. This is where the fgets function comes to our rescue:

Listing 4.

#include int main (void) (char str; fgets (str, 10, stdin); puts (str); return 0;)

The fgets function takes three arguments as input: a variable to write the string, the size of the string to write, and the name of the stream from where to get the data to write to the string, in this case, stdin. As you already know from Lesson 3, stdin is standard input, usually associated with a keyboard. It is not at all necessary that the data should come from the stdin stream; in the future, we will also use this function to read data from files.

If during the execution of this program we enter a string longer than 10 characters, only 9 characters from the beginning and a line break will still be written to the array, fgets will "cut" the string to the required length.

Note that fgets does not read 10 characters, but 9! As we recall, in strings, the last character is reserved for a null character.

Let's check it out. Let's start the program from the last listing. And enter the line 1234567890. The screen displays the string 123456789.


Fig. 3 An example of how the fgets function works

The question arises. Where did the tenth character go? And I will answer. It hasn't gone anywhere, it has remained in the input stream. Run the following program.

Listing 5.

#include int main (void) (char str; fgets (str, 10, stdin); puts (str); int h = 99; printf ("do% d \ n", h); scanf ("% d", & h) ; printf ("posle% d \ n", h); return 0;)

Here is the result of her work.


Figure 4 Non-empty stdin buffer

Let me explain what happened. We called the fgets function. She opened an input stream and waited for us to enter data. We entered 1234567890 from the keyboard \ n (\ n I mean pressing the Enter key). This went to the stdin input stream. The fgets function, as expected, took the first 9 characters 123456789 from the input stream, added a null character \ 0 to them and wrote it to the string str. There are still 0 \ n left in the input stream.

Next, we declare the h variable. We display its value on the screen. Then we call the scanf function. This is where it is expected that we can enter something, but since 0 \ n hangs in the input stream, then the scanf function perceives this as our input, and 0 is written to the h variable. Next, we display it on the screen.

This, of course, is not exactly the behavior we expect. To cope with this problem, it is necessary to clear the input buffer after we have read a line entered by the user from it. The special function fflush is used for this. It has only one parameter - the stream to be cleaned up.

Let's fix the last example so that it works predictably.

Listing 6.

#include int main (void) (char str; fgets (str, 10, stdin); fflush (stdin); // clear the input stream puts (str); int h = 99; printf ("do% d \ n", h) ; scanf ("% d", & h); printf ("posle% d \ n", h); return 0;)

The program will now work as expected.


Fig. 4 Flushing the stdin buffer with fflush

Summing up, two facts can be noted. First. It is not safe to use the gets function at the moment, so it is recommended to use the fgets function everywhere.

Second. Remember to flush the input buffer if you are using the fgets function.

This concludes the conversation about entering strings. Go ahead.

Top related articles