Adding details to strcmp (#24491)

* Adding details to strcmp

* Reworded statement
This commit is contained in:
Dauli Pamale Alexis Ange
2018-12-11 12:55:30 +02:00
committed by Manish Giri
parent ba11ff267a
commit 24d1276f9c

View File

@ -170,6 +170,12 @@ void string_concatenate(char [] s1, char [] s2)
}
```
#### Concatenate number of characters to a string: `strncat`
`strncat` (from 'string number concatenate') concatenates a certain number of characters from the beginning of the second string to the end of first string. In this example, strncat will concatenate some characters from the second to the first string:
```C
strncat(char s1[], char s2[], int n);
```
#### Get length: `strlen`
`strlen` (from 'string length') will return an integer value corresponding to the length of the string. In this example, an integer called `string_length` will be assigned the length of `my_string`:
@ -200,21 +206,22 @@ if(!strcmp(first, second)){
```
Notice the `!`, which is needed because this function returns 0 if they are the same. Placing the exclamation point here will make that comparison return true.
#### Concatenate number of characters to a string: `strncat`
`strncat` (from 'string number concatenate') concatenates a certain number of characters from the beginning of the second string to the end of first string. In this example, strncat will concatenate some characters from the second to the first string:
***Tip*** - If you found the `!` strange, you can also compare the result of `strcmp()` with 0, like so -
```C
strncat(char s1[], char s2[], int n);
if(strcmp(first, second) == 0){
```
#### Compare 'n' bytes: `strncmp`
`strncmp` compares two strings for 'n' characters. The integer value it returns is 0 if they are the same, but it will also return negative if the value of the first (by adding up characters) is less than the value of the second, and positive if the first is greater than the second. Take a look at an example of how this might be used:
```C
if(!strncmp(first, second, 4)){
printf("These strings are the same!\n");
} else {
printf("These strings are not the same!\n");
}
```
If the first string is `wires` and the second string is `wired`, the above snippet would still print `These strings are the same!` comparing first 4 characters.
Notice the `!`, which is needed because this function returns 0 if they are the same. Placing the exclamation point here will make that comparison return true.