Вы находитесь на странице: 1из 22

Poornima College of

Engineering
Sting, string functions

Vishal Choudhary
Deptt. Of CS
Session outline

 String

 String manipulation function

 String program
What is string
 Sequence of character is called string
 In C “the sequence of character enclosed in

double quotes is called string”


e.g—
”hello string”
”45”,
“23welcome”
 In C programming, a string is a sequence of
characters terminated with a null
character \0. For example:
char c[] = "c string";
When the compiler encounters a sequence of
characters enclosed in the double quotation
marks, it appends a null character \0 at the
end by default.
How to declare a string?

 char s[5];

Here, we have declared a string of 5


characters.
How to initialize strings?

 You can initialize strings in a number of


ways.

char c[] = "abcd";


char c[50] = "abcd";
char c[] = {'a', 'b', 'c', 'd', '\0'};
char c[5] = {'a', 'b', 'c', 'd', '\0'};
 char greeting[] = "Hello";

Actually, you do not place the null character at the end of a string


constant. The C compiler automatically places the '\0' at the end of
the string when it initializes the array.
String Function & Purpose
 strcpy(s1, s2);
Copies string s2 into string s1.
 strcat(s1, s2);

Concatenates string s2 onto the end of string


s1.
 strlen(s1);

Returns the length of string s1.


 strcmp(s1, s2);

Returns 0 if s1 and s2 are the same; less than 0


if s1<s2; greater than 0 if s1>s2.
#include <stdio.h>
#include <string.h>
int main ()
{
char str1[12] = "Hello";
char str2[12] = "World";
char str3[12];
int len ; /* copy str1 into str3 */
strcpy(str3, str1);
printf("strcpy( str3, str1) : %s\n", str3 );
/* concatenates str1 and str2 */
strcat( str1, str2);
printf("strcat( str1, str2): %s\n", str1 );
/* total lenghth of str1 after concatenation
*/ len = strlen(str1);
printf("strlen(str1) : %d\n", len ); return
0;
}
Output
strcpy( str3, str1) : Hello
 strcat( str1, str2):

HelloWorld
strlen(str1) : 10
Read String from the user

 the scanf() function reads the sequence of


characters until it
encounters whitespace (space, newline, tab,
etc.).
 The gets() function read  string contains

spaces, we use the gets() function. Gets


ignores the whitespaces. It stops reading
when a newline is reached (the Enter key is
pressed).For example:
#include <stdio.h>
int main()
{
char full_name[25];
printf("Enter your full name: ");
gets(full_name);
printf("My full name is %s ",full_name);
return 0;
}
puts function
 The puts function prints the string on an
output device and moves the cursor back to
the first position. A puts function can be used
in the following way,

#include <stdio.h>
int main()
{
char name[15];
gets(name); //reads a string
puts(name); //displays a string
return 0;
}
1. Which among the following is Copying function?
a) memcpy()
b) strcopy()
c) memcopy()
d) strxcpy()

2. Which function will you choose to join two words?


a) strcpy()
b) strcat()
c) strncon()
d) memcon()
3. What will strcmp() function do?
a) compares the first n characters of the object
b) compares the string
c) undefined function
d) copies the string
If the two strings are identical, then strcmp() function
returns

A
-1
.
B
1
.
C
0
.
D
Yes
.
 How will you print \n on the screen?
printf("\n");
echo "\\n";
printf('\n');
printf("\\n");
What is output?
#include <stdio.h>
int main()
{
char *str = "hello, world";
char *str1 = "hello, world";
if (strcmp(str, str1))
printf("equal");
else
printf("unequal");
}
important library functions are:
 strncmp(str1, str2, n) :
It returns 0 if the first n characters of str1 is equal to
the first n characters of str2, less than 0 if str1 <
str2, and greater than 0 if str1 > str2.

 strncpy(str1, str2, n)
This function is used to copy a string from another
string. Copies the first n characters of str2 to str1
 strchr(str1, c):

it returns a pointer to the first occurrence of


char c in str1, or NULL if character not found.
 strstr(str1, str2):
it returns a pointer to the first occurrence of
str2 in str1, or NULL if str2 not found.
 strncat(str1, str2, n)

Appends (concatenates) first n characters of


str2 to the end of str1 and returns a pointer
to str1.
 strlwr() :to convert string to lower case
 strupr() :to convert string to upper case
 strrev() : to reverse string
strrchr(str1, c): it searches str1 in reverse and returns a pointer to
the position of char c in str1, or NULL if character not found.
Summary
 A string is a sequence of characters stored in a character array.

 A string is a text enclosed in double quotation marks.

 A character such as 'd' is not a string and it is indicated by single quotation marks.

 'C' provides standard library functions to manipulate strings in a program. String manipulators

are stored in <string.h> header file.

 A string must be declared or initialized before using into a program.

 There are different input and output string functions, each one among them has its features.

 Don't forget to include the string library to work with its functions

 We can manipulate different strings by defining a string array.

Вам также может понравиться