string
string
string
strlen() function:-
Example:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
getch();
Concatenate Strings
To concatenate (combine) two strings, you can use the strcat() function:
Syntax
char* strcat(char* dest, const char* src);
The terminating character at the end of dest is replaced by the first
character of src.
Parameters
dest: Destination string
src: Source string
Return value
The strcat() function returns a pointer to the dest string.
Example:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
// Print str1
printf("%s", str1);
getch();
strcmp() Function
The strcmp() is a built-in library function in C. This function takes two
strings as arguments and compares these two strings lexicographically.
Syntax
int strcmp(const char *str1, const char *str2);
Parameters
str1: This is the first string to be compared.
str2: This is the second string to be compared.
Return Value
If str1 is less than str2, the return value is less than 0.
If str1 is greater than str2, the return value is greater than 0.
If str1 is equal to str2, the return value is 0.
Example:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
getch();
Copy Strings
To copy the value of one string to another, you can use the strcpy() function
Example:-
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[20] = "Hello World!";
char str2[20];
// Print str2
printf("%s", str2)
getch();
strstr()
The strstr() function in C is used to search the first occurrence of a
substring in another string.
Syntax
char *strstr (const char *s1, const char *s2);
Parameters
s1: This is the main string to be examined.
s2: This is the sub-string to be searched in the s1 string.
Return Value
If the s2 is found in s1, this function returns a pointer to the first
character of the s2 in s1, otherwise, it returns a null pointer.
It returns s1 if s2 points to an empty string.
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
if (myPtr != NULL) {
printf("%s", myPtr);
getch();
}
C Pointers
What is a Pointer in C?
A pointer is defined as a derived data type that can store the address of
other C variables or a memory location. We can access and manipulate
the data stored in that memory location using pointers.
Syntax of C Pointers
The syntax of pointers is similar to the variable declaration in C, but we
use the ( * ) dereferencing operator in the pointer declaration.
datatype * ptr;
where
ptr is the name of the pointer.
datatype is the type of data it is pointing to.
1. Pointer Declaration
In pointer declaration, we only declare the pointer but do not initialize it.
To declare a pointer, we use the ( * ) dereference operator before its
name.
Example
int *ptr;
The pointer declared here will point to some random memory address as it
is not initialized. Such pointers are called wild pointers.
2. Pointer Initialization
Pointer initialization is the process where we assign some initial value to
the pointer variable. We generally use the ( &: ampersand ) addressof
operator to get the memory address of a variable and then store it in the
pointer variable.
Example
int var = 10;
int * ptr;
ptr = &var;
We can also declare and initialize the pointer in a single step. This method
is called pointer definition as the pointer is declared and initialized at the
same time.
Example
int *ptr = &var;
Note: It is recommended that the pointers should always be initialized to
some value before starting using it. Otherwise, it may lead to number of
errors.
Pointer Dereferencing
Dereferencing a pointer is the process of accessing the value stored in the
memory address specified in the pointer. We use the same ( * )
dereferencing operator that we used in the pointer declaration.
C Structures
Last Updated : 11 Oct, 2024
C Structure Declaration
We have to declare structure in C before using it in our program. In
structure declaration, we specify its member variables along with their
datatype. We can use the struct keyword to declare the structure in C
using the following syntax:
Syntax
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
};
C Structure Definition
To use structure in our program, we have to define its instance. We can
do that by creating variables of the structure type. We can define structure
variables using two methods:
1. Structure Variable Declaration with Structure Template
struct structure_name {
data_type member_name1;
data_type member_name1;
....
....
}variable1, varaible2, ...;
2. Structure Variable Declaration after Structure Template
// structure declared beforehand
struct structure_name variable1, variable2, .......;
Access Structure Members
We can access structure members by using the ( . ) dot operator.
Syntax
structure_name.member1;
strcuture_name.member2;