Handling th Strings in C Programming - ProgrammerTech
Language:

Search

Handling th Strings in C Programming

  • Share this:
Handling th Strings in C Programming

Operations on Strings in C

Strings and dealing with them in the C language, in our lesson, our topic will be dealing with texts or the so-called Strings. Have you ever wondered how we can store complete sentences or phrases such as the name of a person or a specific text, based on the data types we mentioned. There is no type in which we can store an entire text, but the closest to this is char, which can only store one character or one symbol.

Recalling our previous lesson on arrays, we'll imagine strings as chars. In this way, we can store a set of symbols and treat them as one piece.

char string[]={'p', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'e', 'r'};

This is actually the principle of strings in C. They are arrays of type char, but instead of storing the text in a method. Tired as in the previous example in determining the values ​​of matrices. In C, there is an easier way, which is intended for storing texts only, and it is done. Putting the text in double quotes, these two C sentences have the same result

char string[]={"programmer"};

But here there is still a problem that we cannot modify the text in this array. Only by modifying each value separately, if we want to store the tech statement, we must write it as

char string[]="programmer";
string[0] = 't';
string[1] = 'e';
string[2] = 'c';
string[3] = 'h';

Since C does not allow us to reset the entire text of the array, another problem is that when we define the array. We have set its amplitude and it cannot be changed so the values ​​of the array cannot be reset. So that it contains more text than what was present. The solution is that we will anticipate some lessons and use pointers, which are a similar method. To some extent to arrays in terms of the way memory is reserved.

We'll get to that later, now all we have to know is that pointers are denoted by an asterisk so instead of defining the text as an array. We define it as a pointer by putting an asterisk before the variable name, the process of defining and resetting the text is very easy.

char *string = "programmer";
string = "tech";

Let's write a simple code that defines a simple text and prints it

#include <stdio.h>
#include <stdlib.h>
int main(int argc, char** argv)
{
    char *str = "I am Programmer and Designer \n";
    printf("%s", str);
    return 0;
}

We've snagged a script and I'm a programmer and designer wrote it in English and we defined char as a pointer and we put str as an abbreviation for string. Then we stored the text in it, then we printed this text on the screen and typed %s ie we want to print. Text of type text and we put str after the comma and when we run the code it will print the sentence for us as shown

I am Programmer and Designer

Instead of writing the entire text at the printf function, we store it in a variable called str, which makes it easier for us to write inside the printf function.

 

Operations on texts in C

We'll go over some of the operations we can perform on text, for example.

 

  • How can we compare between one text and another text?
  • Do we use the equal sign == as we did with the numbers in the previous lessons?
  • How can we merge two texts together?
  • How do we know the number of characters in a particular text?

 

We will answer all these questions shortly.

The C language provides us with some functions that we can use to perform various operations on texts. We can use these functions. We must insert the file containing the definition of these functions by typing.

#include <string.h>

At the top of the source file at the beginning of the code this sentence tells the compiler that we want to insert the file. named string.h in our source file. It is worth noting that it is automatically installed on your computer when you install any C compiler, and this file. Contains the definition of all custom functions with text. After inserting the string.h file, we can now make use of the functions it contains.

 

strcmp function to compare two texts in C

The strcmp function is a function that compares two strings, i.e. two char arrays. This function is called strcmp ie compare strings. This function takes two texts as parameters and compares them and returns an integer and this integer represents what. If the two texts are equal or not. If the two texts are equal, the result will be. For this function the number is 0 otherwise a value will be returned. Other than 0.

You might be wondering, why would we use a function instead of just using the == sign like we did before? This method will not work for comparing two texts at all, not even for comparing any two matrices, and we will know. The reason is when accessing the indicators lesson.

You might be wondering, why would we use a function instead of just using the == sign like we did before? This method will not work for comparing two texts at all, not even for comparing any two matrices, and we will know. The reason is when accessing the indicators lesson.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
    char *cs1, *cs2;
    cs1 = "programmer-tech.com";
    cs2 = "programmer-tech.com";
    int x = strlen(cs1);
    printf("%d", x);
    int status = strcmp(cs1, cs2);
    if(status == 0)
    {
        printf("The two texts are equal %d", x);
    }
    else{
        printf("The two texts are not equal");
    }
    return 0;
}

First we called the library for dealing with text string.h. We have defined two strings as char and the two strings are cs1 for the first string and cs2 for the second string and we defined the status variable to put the comparison result against. And we wrote the strcmp function and chose it to compare cs1 and cs2 and we wrote to it a condition if the status is 0, that is, the two texts are equal. Write to us the length of the text, and if not, print to us that they are not equal and when the program is running.

19The two texts are equal 19

When the two texts are not equal

19The two texts are not equal

 

strlen function to find the length of text in C

The strlen function is a function used to find the length of a text and it takes only one parameter which is text and returns the number of characters in that text as an integer.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
    char *cs1, *cs2;
    cs1 = "programmer-tech.com";
    cs2 = "programmer-tech.com";
    int x = strlen(cs1);
    printf("%d", x);
    return 0;
}

First we called the library for dealing with strings.h and defined two strings. as char and the two strings are cs1 for the first and cs2 for the second. And we defined two variables and put the texts in them, and we put the output in a scalar variable, which is x, and we wrote the function strlen and we wrote the name of the variable cs1 from. Yes, to calculate its length, and we printed the output via printf, and when the program is run, its result is.

19

 

strcat function to merge text together in C

The strcat function is a function used to combine two texts by moving the source text to the end of the text. destination The destination array must be large enough to contain the merging of the two texts

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char** argv)
{
    char *cs = "-tech";
    char saja[15] = "programmer";
    strcat(saja, cs);
    printf("My website = %s \n", saja);
    return 0;
}

At first we created two scripts, the first is cs of type char pointer and the script is -tech and the second script is saja was an array of char. And it contains the word programmer in order to combine them together and we put the number 15 which is the length of the array. The two texts are 15 and their original length is 14. We put an extra number and the reason is in C, we can't know. When it ends. Only through a special symbol called the cause is in C language, we cannot know when it ends except through a special symbol called null terminator and denoted by /0.

This special symbol is added at the end of the text, and this symbol needs a special place to accommodate it in the memory. It is added automatically when adding new Strings texts or using one of the text-handling functions. But all we have to make it have a place in the memory to accommodate it, so we always recommend that we add 1 to the capacity of the text array when manually specifying it to avoid any problem that occurs. And then we wrote the strcat function and we chose the two texts for it to be merged, and we used the print function and wrote it My website. Then it prints for us saja because the text is stored in saja and when we run the program we will get the following result

My website = programmer-tech

Tags:
Saja Alsadig

Saja Alsadig

من السودان مبرمجة مواقع ويب و تطبيقات أندرويد اتقن لغات "html, css, php, js" بالإضافة للتعامل مع اندرويد ستوديو و بناء التطبيقات, مهتمة بمجال التقنية منذ الصغر و كان لدي شقف كبير للدخول بالجامعه بقسم البرمجة و دخلته في عام 2013 و تعلمت كل شيئ ضمن الجامعة و خارجها من التعليم الذاتي و اتمنى أن أكون عند حسن ظن الجميع و إيصال الأمانة لكم, اعمل حاليا في إدارة موقع جامعة السودان.