Header Ads

Strings:C Programming Language - Part 10

Strings


String is the curator set. A word, a sentence, a prearrag, all the string. Like Hello World, a string. Again hello and a string. World and a string When there's only one color, then it's the cursor.




As told before, the string is the curator set. Yes, the array of a one-dimensional curctor is the string. Before reading the text, there should be an idea about arrays / arrays. The array can be known from this text. A string is declared as follows

Char string [50] = "This is a static string";

And to write to print is:
Printf ("% sn", string);

We declare a string and print a program to print it

1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include <string.h>
Int main ()
{
    Char string [50] = "Hello";
    Printf ("% s \ n", string);
    Return 0;
}
To print strings in Hello we have declared a 50-bit curator array, but our color is just 5. So we do not need to declare such a big array. For that, we must declare an array of size 5. It is good to say that when we declare a string, then all the letters will be automatically added to a null character "". See the picture below.

String_representation

For our Hello string we need a 6-size array. Hello's five letters and a null curator. Again we know the indexing of arrays starts from 0. Then we can declare our Hello string as follows

Char string [5] = "Hello";

Again, we can use the size of the array, we can leave it on run time. The program will automatically sign a size. Write for him

Char string [] = "Hello";

Now write a program, where one will take a input from the user, and then print it out

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <string.h>
Int main ()
{
    Char string [100];
    Printf ("Enter a sentance:");
    Scanf ("% [^ \ n] s", & string);
    Printf ("% s \ n", string);
    Return 0;
}
In the above program, we are writing in scanf% [^ n] s. The scan input can not be taken with scanf. He has to use special modifiers. That's how it's written. And without scanf we can take string input with get. The above program will be written with go:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <string.h>
Int main ()
{
    Char string [100];
    Printf ("Enter a sentance:");
    Gets (string);
    Printf ("% s \ n", string);
    Return 0;
}
Simple, is not it?


Strings Concatenation


Concatenation means being paired. Strings concatenation is used to support two different strings. There is a library function for this. Strcat (), which can be used to connect two strings together. Strcat () takes two strings as input. Thus: strcat (string1, string2) then adds string string inside string2 to string1, see the following program:

1
2
3
4
5
6
7
8
9
10
11
12
#include <stdio.h>
#include <string.h>
Int main ()
{
    Char string1 [] = "Hello";
    Char string2 [] = "World";
    Strcat (string1, string2);
    Printf ("% s \ n", string1);
    Return 0;
}

Strcmp ()

Strcmp (string1, string2) is used to compress two strings. If you pass the function on two strings, then return three values

Return value will be smaller than 0 if string1 is greater than string2.
Return value will be greater than 0 if string1 is less than string2.
The return value will be 0, if the two strings are the same.
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
27
28
#include <stdio.h>
#include <string.h>
Int main () {
    Int ret;
    Char string1 [50], string2 [50];
    Printf ("Enter string 1:");
    Gets (string1);
    Printf ("Enter string 2:");
    Gets (string2);
    Ret = strcmp (string1, string2);
    If (ret <0) {
          Printf ("string1 is less than string2");
       }
   Else if (ret> 0) {
          Printf ("string2 is less than string1");
       }
   Else {
          Printf ("string1 is equal to string2");
       }
   Return 0;
}
Strcpy ()


When strcpy () passes two strings, the value of the second string will be copied to the first string. As strcpy (string1, string2) passes, strings inside string2 will be signed to string1. See the following program

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>
Int main ()
{
    Char string1 [] = "Hello";
    Char string2 [] = "World";
    Strcpy (string1, string2);
    Printf ("% s \ n", string1);
    Printf ("% s \ n", string2);
    Return 0;
}
Now we will write a program, where we can find out how many times a letter has a sentence. Take a sentence as input. Then we will find out the number of letters, that is. And then print out the number of times it's printed:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <stdio.h>
#include <string.h>
Int main () {
   Char sentence [1000];
   Char character;
   Int i, count = 0;
   Printf ("Enter a sentence:");
   Gets (sentence);
   Printf ("Enter a character to find frequency:");
   Scanf ("% c", & character);
   For (i = 0; sentence [i]! = '\ 0'; ++ i)
   {
       If (character == sentence [i])
           ++ count;
   }
   Printf ("Frequency of% c =% d", character, count);
   Return 0;
}
In the above program, the first one is taking a sentence input from the wires. Then, how many times have the cursor / letter taking the input. Then we have counted the character by going for the for loop. After that, we have printed the letters over and over. For loop we used sentence [i]! = '\ 0'. This means that the for loop will continue until it gets the null curator. And we know that at the end of each string array is a null curator.



Gratitude:Online.

Our YouTube Channel:Clik Here



No comments

Theme images by sebastian-julian. Powered by Blogger.