0% found this document useful (0 votes)
197 views

String Functions / Operations in C

The document summarizes common string functions in C including strlen(), strcpy(), strncpy(), strcmp(), stricmp(), strncmp(), strnicmp(), strlwr(), and provides examples of how to use each function with sample code. Each function is described, its syntax is provided, and a sample program is included to demonstrate how it works with explanations of the program logic. The functions allow manipulating, comparing, and converting strings in C programs.

Uploaded by

Mohammed Jeelan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
197 views

String Functions / Operations in C

The document summarizes common string functions in C including strlen(), strcpy(), strncpy(), strcmp(), stricmp(), strncmp(), strnicmp(), strlwr(), and provides examples of how to use each function with sample code. Each function is described, its syntax is provided, and a sample program is included to demonstrate how it works with explanations of the program logic. The functions allow manipulating, comparing, and converting strings in C programs.

Uploaded by

Mohammed Jeelan
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

String functions / Operations in C

1) strlen() in C :
This function is used to determine the length of a given string.

Syntax :

variable=strlen(string);

The string length will be returned to the variable.

Sample Program

str_length.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
int l;
printf("Enter a string : ");
scanf("%s",&a);
l=strlen(a);
printf("\n Length of the Given String is : %d",l);

getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strlen() comes under
it.
4. main() function is the place where C program execution begins.
5. A char type array a[30] is declared.
6. User input for array a is received using scanf.
7. C in-built function strlen() is used to determine the length of the given string in
array a and the result is stored in variable l.
8. When variable l is printed the string length is displayed.

Output :

2) strcpy() in C :
This function copies the string from one variable to another variable.

Syntax :

strcpy(source_variable, destination_variable);

Sample Program

str_cpy.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
printf("Enter a string : ");
scanf("%s",&a);
strcpy(b,a);
printf("Value of a : %s",a);
printf("\nValue of b : %s",b);
getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strcpy() comes under
it.
4. main() function is the place where C program execution begins.
5. Two array of type char a[30] and b[30] are declared.
6. User input is received and stored in the arrays a using scanf.
7. C in-built function strcpy() is used to copy the datas from array a to array b.
8. Now when the arrays a & b are displayed both displays identical values.

Output :

3) strncpy() in C :
This function also copies the string from one variable to another variable, but only
upto the specified length.

Syntax :

strncpy(destination, source, length);

Sample program :

strn_cpy.c
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
printf("Enter a string : ");
scanf("%s",&a);
strncpy(b,a,3);
printf("Value of a : %s",a);
printf("\nValue of b : %s",b);
getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strncpy() comes under
it.
4. main() function is the place where C program execution begins.
5. Two array of type char, a[30] and b[30] are declared.
6. User input is received and stored in the arrays a using scanf.
7. C in-built function strncpy() is used copy the datas from array a to
array bupto a specified length. In this case it is 3.
8. Now when the array a & b are displayed array b will have data identical to
array a upto the length of 3.

Output :

4) strcmp() in C :
This function is used to compare two strings. They are case sensitive.

Syntax :

strcmp(string1,string2);

If both the strings are equal return value will be 0, else non_zero value will be
returned.

Sample program :

str_cmp.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int n;
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
n=strcmp(a,b);
if(n==0)
{
printf("Both the Strings are Equal");
}
else
{
printf("Strings are not Equal");
}
getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strcmp() comes under
it.
4. main() function is the place where C program execution begins.
5. Two array of type char a[30] and b[30] are declared.
6. User input is received and stored in the arrays a and b using scanf.
7. C in-built function strcmp() is used to compare the values in a & b and the
comparison result is returned to variable n.
8. A if condition is used to check whether the returned value in variable n is 0.
9. If it is 0 printf is used to display that both the strings are equal. If it is a non-
zero value again printf is used to display that the given strings are not
identical.

Output :

5) stricmp() in C :
This function is also used to compare two strings but they are not case sensitive.

Syntax :

stricmp(string1,string2);

Sample program :

stri_cmp.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int n;
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
n=stricmp(a,b);
if(n==0)
{
printf("Both the Strings are Equal");
}
else
{
printf("Strings are not Equal");
}
getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function stricmp() comes under
it.
4. main() function is the place where C program execution begins.
5. Two array of type char a[30] and b[30] are declared.
6. User input is received and stored in the arrays a and b using scanf.
7. C in-built function stricmp is used to compare the value in a & b without any
match case and the comparison result is returned to variable n.

Output :
6) strncmp() in C :
This function compares two strings only upto a specified length. They are case-
sensitive.

Syntax :

strncmp(string1,string2,length);

Sample program :

strn_cmp.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int n;
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
n=strncmp(a,b,3);
if(n==0)
{
printf("Both the Strings are Equal upto the first 3 characters");
}
else
{
printf("Strings are not Equal");
}
getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strncmp() comes
under it.
4. main() function is the place where C program execution begins.
5. Two array of type char a[30] and b[30] are declared.
6. User input is received and stored in the arrays a and b using scanf.
7. C in-built function strncamp() is used to compare the values of a & b upto a
specified length (i.e. 3) and the comparison result is returned to variable n.
8. A if condition is used to check whether the returned value in variable n is 0.
9. If it is 0, printf is used to display that both the strings are equal. If it is a non-
zero value again printf is used to display that the given strings are not
identical.

Output :

7) strnicmp() in C :
This function also compares two strings upto a specified length, but not case-sensitive.

Syntax :

strnicmp(string1,stringn,length);

Sample program :

strni_cmp.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30];
int n;
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
n=strnicmp(a,b,3);
if(n==0)
{
printf("Both the Strings are Equal upto the first 3 characters");
}
else
{
printf("Strings are not Equal");
}
getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strnicmp() comes
under it.
4. main() function is the place where C program execution begins.
5. Two array of type char a[30] and b[30] are declared.
6. User input is received and stored in the arrays a and b using scanf.
7. C in-built function strnicmp() is used compare the values of a & b upto a
specified length without any match case and the comparison result is returned
to variable n.
8. A if condition is used to check whether the returned value in variable n is 0.
9. If it is 0, printf is used to display that both the strings are equal. If it is a non-
zero value again printf is used to display that the given strings are not
identical.

Output :
8) strlwr() in C :
This function converts uppercase characters to lowercase.

Syntax :

strlwr(string);

Sample program :

str_lwr.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
printf("Enter a string in uppercase : ");
scanf("%s",&a);

printf("RESULT : %s",strlwr(a));

getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strlwr() comes under
it.
4. main() function is the place where C program execution begins.
5. A char type array a[30] is declared.
6. User input for array a is received using scanf.
7. C in-built function strlwr() is used to display the given string in lower case.

Output :

9) strupr() in C :
This function converts lowercase characters to uppercase.

Syntax :

strupr(string);

Sample program :

str_upr.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
printf("Enter a string in lowercase : ");
scanf("%s",&a);

printf("RESULT : %s",strupr(a));

getch();
}
Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strupr() comes under
it.
4. main() function is the place where C program execution begins.
5. A char type array a[30] is declared.
6. User input for array a is received using scanf.
7. C in-built function strupr() is used to display the given string in upper case.

Output :

10) strdup() in C :
This function is used to duplicate a string to a pointer variable by allocating the
memory location of the string.

Syntax :

pointer=strdup(string);

Sample program :

str_dup.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],*b;
printf("Enter a string : ");
scanf("%s",&a);
b=strdup(a);
printf("Entered String in a : %s",a);
printf("\nDuplicated string : %s",b);

getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strdup() comes under
it.
4. main() function is the place where C program execution begins.
5. A char type array a and char type pointer variable *b are declared.
6. User Input for array a is received using scanf.
7. C in-built function strdup() is used to duplicate the datas in a to b.
8. Now when array and pointer variable *b are displayed, they both haveidentical
datas.

Output :

11) strcat() in C :
This function is used to join (concatenate) two strings.

Syntax :

strcat(string1,string2);
Sample program :

str_cat.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30],b[30],c[30];
printf("Enter string one : ");
scanf("%s",&a);
printf("Enter string two : ");
scanf("%s",&b);
strcat(a,b);

printf("Concatinated String : %s",a);

getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strcat() comes under
it.
4. main() function is the place where C program execution begins.
5. Two array of type char a[30] and b[30] are declared.
6. User Input is received and stored in the arrays a and b using scanf.
7. C in-built function strcat() is used to join the two strings(data) from array
variable a & b.
8. When the array a is printed the concatenated string is displayed.

Output :
13) strrev() in C :
This function is sued to reverse the characters in a given string.

Syntax :

strrev(string);

Sample program :

str_rev.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
printf("Enter string : ");
scanf("%s",&a);
strrev(a);

printf("Reversed String : %s",a);

getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strrev() comes under
it.
4. main() function is the place where C program execution begins.
5. A char type array a[30] is declared.
6. scanf is used to receive user input to array a.
7. Then C in-built function strrev() is used to receive the string in array a and the
same is displayed using printf.

Output :

14) strset() in C :
This function replaces all the characters of a string with a given symbol or character.

Syntax :

strset(string,symbol);

Sample program :

str_set.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
char b;

printf("Enter a string: ");


gets(a);
printf("Enter a symbol to replace the string : ");
scanf("%c",&b);

strset(a,b);
printf("After strset : %s",a);

getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strset() comes under
it.
4. main() function is the place where C program execution begins.
5. A char type array a[30] is declared.
6. A variable b of type char is declared.
7. gets() is used to receive user input for array a.
8. scanf is used to receive a character symbol to variable b.
9. C in-built function strset() is used to replace all the characters in array a with
symbol received in variable b.

Output :

15) strnset() in C :
This function also replaces the characters of a string with a given symbol but only to a
specified length.

strnset(string,symbol,n);
Sample program :

strn_set.c

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[30];
char b;

printf("Enter a string: ");


gets(a);
printf("Enter a symbol to replace the string : ");
scanf("%c",&b);

strnset(a,b,2);

printf("After strset : %s",a);

getch();
}

Program Algorithm / Explanation :

1. #include<stdio.h> header file is included because, the C in-built


statementprintf we used in this program comes under stdio.h header files.
2. #include<conio.h> is used because the C in-built function getch() comes under
conio.h header files.
3. string.h header file included because c in-built function strnset() comes under
it.
4. main() function is the place where C program execution begins.
5. A char type array a[30] is declared.
6. A variable b of type char is declared.
7. gets() is used to receive user input for array a.
8. scanf is used to receive a character symbol to variable b.
9. C in-built function strnset() is used to replace the character in array a with
symbol received in variable b upto a specified length. In this case it is 2.

Output :

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy