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 | Work of Function |
---|---|
strlen() | computes string’s length |
strcpy() | copies a string to another |
strcat() | concatenates(joins) two strings |
strcmp() | compares two strings |
strlwr() | converts string to lowercase |
strupr() | converts string to uppercase |
Strings handling functions are defined under "string.h"
header file.
#include <string.h>
Note: You have to include the code below to run string handling functions.
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;
}
Note: Though, gets()
and puts()
function handle strings, both these functions are defined in "stdio.h"
header file.
Way cool! Some extremely valid points! I appreciate
you penning this article and the rest of the site is really good.
I will immediately snatch your rss as I can’t find your
e-mail subscription link or newsletter service. Do you’ve any?
Please let me know in order that I may just subscribe.
Thanks. Hi there just wanted to give you a quick heads
up. The text in your article seem to be running
off the screen in Internet explorer. I’m not sure if this is
a format issue or something to do with internet browser compatibility but I figured I’d post to let you know.
The layout look great though! Hope you get the problem fixed soon.
Thanks http://porsche.com/
my website: Jimmy
LikeLike
Thank you Jimmy……👍👍
LikeLike