0% found this document useful (0 votes)
2 views7 pages

string

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 7

String function in c

strlen() function:-

The strlen() function calculates the length of a given string. It doesn’t


count the null character ‘\0’.
Syntax
int strlen(const char *str);
Parameters
 str: It represents the string variable whose length we have to find.
Return Value
 strlen() function in C returns the length of the string.

Example:-

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

char alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";


printf("%d", strlen(alphabet));

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()

char str1[20] = "Hello ";


char str2[] = "World!";

// Concatenate str2 to str1 (result is stored in str1)


strcat(str1, str2);

// 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()
{

char str1[] = "Hello";


char str2[] = "Hello";
char str3[] = "Hi";

// Compare str1 and str2, and print the result


printf("%d\n", strcmp(str1, str2)); // Returns 0 (the strings
are equal)

// Compare str1 and str3, and print the result


printf("%d\n", strcmp(str1, str3)); // Returns -4 (the strings
are not equal)

getch();

Copy Strings
To copy the value of one string to another, you can use the strcpy() function

The strcpy() is a standard library function in C and is used to copy one


string to another. In C, it is present in <string.h> header file.
Syntax
char* strcpy(char* dest, const char* src);
Parameters
 dest: Pointer to the destination array where the content is to be
copied.
 src: string which will be copied.
Return Value
 strcpy() function returns a pointer pointing to the output string.

Example:-

#include<stdio.h>

#include<conio.h>

#include<string.h>

void main()

{
char str1[20] = "Hello World!";
char str2[20];

// Copy str1 to str2


strcpy(str2, str1);

// 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()

char myStr[] = "The rain in Spain falls mainly on the plains";

char *myPtr = strstr(myStr, "ain");

if (myPtr != NULL) {

printf("%s", myPtr);

getch();
}

C Pointers

Pointers are one of the core components of the C programming


language. A pointer can be used to store the memory
address of other variables, functions, or even other pointers. The
use of pointers allows low-level memory access, dynamic memory
allocation, and many other functionality in C

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.

How to Use Pointers?


The use of pointers in C can be divided into three steps:
1. Pointer Declaration
2. Pointer Initialization
3. Pointer Dereferencing

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



The structure in C is a user-defined data type that can be used to


group items of possibly different types into a single type.
The struct keyword is used to define the structure in the C
programming language. The items in the structure are called
its member and they can be of any valid data type. Additionally,
the values of a structure are stored in contiguous memory
locations.

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;

You might also like