Vous êtes sur la page 1sur 8

String Manipulations In C Programming

Using Library Functions


In this article, you'll learn to manipulate strings in C using library functions such as gets(), puts,
strlen() and more. You'll learn to get string from the user and perform operations on the string.
You need to often manipulate strings according to the need of a problem. Most, if not all, of the
time string manipulation can be done manually but, this makes programming complex and large.
To solve this, C supports a large number of string handling functions in the standard library
"string.h".
Few commonly used string handling functions are discussed below:
Function
strlen()
strcpy()
strcat()
strcmp()
strlwr()
strupr()

Work of Function
Calculates the length of string
Copies a string to another string
Concatenates(joins) two strings
Compares two string
Converts string to lowercase
Converts string to uppercase

trings handling functions are defined under "string.h" header file, i.e, you have to include the
code below to run string handling functions.
#include <string.h>

gets() and puts()


Functions gets() and puts() are two string functions to take string input from the user and display it
respectively as mentioned in the previous chapter.
#include<stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name);
//Function to read string from user.
printf("Name: ");
puts(name);
//Function to display string.
return 0;
}

Example: Calculate Length of String without Using strlen()


Function
#include <stdio.h>

int main()
{
char s[1000], i;
printf("Enter a string: ");
scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i);

printf("Length of string: %d", i);


return 0;

Output
Enter a string: Programiz
Length of string: 9

Find Length of String Using Library Function


#include<stdio.h>
#include<string.h>
int main() {
char str[100];
int len;
printf("\nEnter the String : ");
gets(str);
len = strlen(str);
printf("\nLength of Given String : %d", len);
return(0);
}
Concat Two Strings Using Library Function
#include<stdio.h>
#include<string.h>
int main() {
char str1[100];
char str2[100];
char str3[100];
int len;

printf("\nEnter the String 1 : ");


gets(str1);
printf("\nEnter the String 2 : ");
gets(str2);
strcpy(str3, str1);
strcat(str3, str2);
printf("\nConcated String : %s", str3);
return (0);
}

Copy One String Into Other Using Library Function


#include<stdio.h>
#include<string.h>
int main() {
char str1[100];
char str2[100];
printf("\nEnter the String 1 : ");
gets(str1);
strcpy(str2, str1);
printf("\nCopied String : %s", str2);
return (0);
}
Convert Given String into Lowercase Using Library Function

#include<stdio.h>
#include<string.h>
int main() {
char *string = "Pritesh Taral";
printf("\nString before to strlwr : %s", string);
strlwr(string);
printf("\nString after strlwr : %s", string);
return (0);
}

Output :
String before to strlwr : Pritesh Taral String after strlwr : pritesh taral
1
2

String before to strlwr : Pritesh Taral


String after strlwr : pritesh taral

Program 2 :
#include<stdio.h> #include<string.h> int main() { char string[100]; printf("Enter String : "); gets(string); strlwr(string);
printf("\nString after strlwr : %s", string); return (0); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include<stdio.h>
#include<string.h>
int main() {
char string[100];
printf("Enter String : ");
gets(string);
strlwr(string);
printf("\nString after strlwr : %s", string);
return (0);
}

Output :
Enter String : Pritesh Taral String after strlwr : pritesh taral
1
2

Enter String : Pritesh Taral


String after strlwr : pritesh taral

Convert Given String into Uppercase Using Library Function


Program 1 :
#include<stdio.h> #include<string.h> int main() { char *string = "Pritesh Taral"; printf("String before to strupr : %sn",
string); strupr(string); printf("String after strupr : %sn", string); return (0); }
1
2
3
4
5
6
7

#include<stdio.h>
#include<string.h>
int main() {
char *string = "Pritesh Taral";
printf("String before to strupr : %sn", string);
strupr(string);

8
9
10
11
12

printf("String after strupr : %sn", string);


return (0);
}

Output :
String before to strupr : Pritesh Taral String after strupr : PRITESH TARAL
1
2

String before to strupr : Pritesh Taral


String after strupr : PRITESH TARAL

Program 2 :
#include<stdio.h> #include<string.h> int main() { char string[100]; printf("Enter String : "); gets(string); strupr(string);
printf("String after strupr : %s", string); return (0); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14

#include<stdio.h>
#include<string.h>
int main() {
char string[100];
printf("Enter String : ");
gets(string);
strupr(string);
printf("String after strupr : %s", string);
return (0);
}

Output :
Enter String : Pritesh Taral String after strupr : PRITESH TARAL
1
2

Enter String : Pritesh Taral


String after strupr : PRITESH TARAL

String Program : Program to Convert String to Integer


#include<stdio.h> #include<stdlib.h> int main() { int num; char marks[3]; printf("Please Enter Marks : "); scanf("%s",
marks); num = atoi(marks); printf("\nMarks : %d", num); return (0); }
1
2
3
4

#include<stdio.h>
#include<stdlib.h>
int main() {
int num;

5
6
7
8
9
10
11
12
13
14
15

char marks[3];
printf("Please Enter Marks : ");
scanf("%s", marks);
num = atoi(marks);
printf("\nMarks : %d", num);
return (0);
}

Output :
Please Enter Marks : 76 Marks : 76
1
2

Please Enter Marks : 76


Marks : 76

Program to sort set of strings in alphabetical order


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

#include<stdio.h>
#include<string.h>
void main() {
char s[5][20], t[20];
int i, j;
clrscr();
printf("\nEnter any five strings : ");
for (i = 0; i < 5; i++)
scanf("%s", s[i]);
for (i = 1; i < 5; i++) {
for (j = 1; j < 5; j++) {
if (strcmp(s[j - 1], s[j]) > 0) {
strcpy(t, s[j - 1]);
strcpy(s[j - 1], s[j]);
strcpy(s[j], t);
}
}
}
printf("\nStrings in order are : ");
for (i = 0; i < 5; i++)
printf("\n%s", s[i]);
getch();

26
27
28

Output :
Enter any five strings :
pri
pra
pru
pry
prn
Strings in order are :
pra
pri
prn
pru
pry

Vous aimerez peut-être aussi