0% found this document useful (0 votes)
38 views9 pages

PDF On String New

The document provides an introduction to C strings including their definition, declaration, initialization, and common string handling functions. It also gives examples of replacing characters in a string, concatenating two strings, and using functions like strlen(), strcpy(), and strcmp(). The document is intended as an e-learning resource on C strings for students at Kaliachak Government Polytechnic.

Uploaded by

Suprakash Lal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views9 pages

PDF On String New

The document provides an introduction to C strings including their definition, declaration, initialization, and common string handling functions. It also gives examples of replacing characters in a string, concatenating two strings, and using functions like strlen(), strcpy(), and strcmp(). The document is intended as an e-learning resource on C strings for students at Kaliachak Government Polytechnic.

Uploaded by

Suprakash Lal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Kaliachak Government Polytechnic

Kalikapur Bandh, P.O: Bahadurpur,


P.S: Kaliachak , Malda- 732201

DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY

2nd Year-3rd semester


E-CONTENTS : C-Programming
language
UNIT : C strings
Developed By :
Shahnawaz Shams, Lecturer in CST
Kaliachak Government Polytechnic
Kalikapur Bandh, P.O:Bahadurpur,
P.S:Kaliachak, Malda-732201
Contents :
1. Definition & Declaration
2. Initialization of string variables
3. String handling functions from standard library
3.1 Example of (strlen(), strcpy(), strcmp())
4. Replacement of string characters
5. Concatenation of two strings
1. Definition & Declaration :
String: A character type array is known as string.A string is a one –
dimentional array of charecters terminted by a null character(‘\0’).The
ASCII value of \0 is 0.

Declaration syntax : char array_name[size] or char string_name[size];


Example:(compile time value insert)
(1)char name={‘r’,’a’,’m’,’\0’};
(2)char name=”ram”;

*Point to be noted in the secound example ‘\0’ is not necessary,because c


inserts the null character autometically.The \0 do not use the size of string
it can stored at out of bound.
2. Initialization of string variables
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
If you follow the rule of array initialization then you can write the above
statement as follows −
char greeting[] = "Hello";
Following is the memory presentation of the above defined string in
C/C++ −
3. String handling functions from standard
library
Sr.No
. Function & Purpose
1 strcpy(s1, s2);
Copies string s2 into string s1.
2 strcat(s1, s2);
Concatenates string s2 onto the end of string s1.
3 strlen(s1);
Returns the length of string s1.
4 strcmp(s1, s2);
Returns 0 if s1 and s2 are the same; less than 0 if s1<s2; greater than 0 if
s1>s2.
5 strchr(s1, ch);
Returns a pointer to the first occurrence of character ch in string s1.
6 strstr(s1, s2);
Returns a pointer to the first occurrence of string s2 in string s1.
3.1. String handling functions
(strlen(), strcpy(), strcmp())
The following example uses some of the above-mentioned functions −
#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;
}
When the above code is compiled and executed, it produces the following result −
strcpy( str3, str1) : Hello
strcat( str1, str2): HelloWorld
strlen(str1) : 10
3.1 Replacement of string characters
#include<stdio.h>
int main()
{
int i=0;
char s[50];
printf("Enter String : ");
gets(s);
while(s[i]!='\0')
{
if(s[i]=='a')
{
s[i]='*';
}
i++;
}
printf("-------------------------------------");
printf("\nString After Replacing 'a' by '*'");
printf("\n-------------------------------------\n");
printf("%s",s);
return 0;
}
4. Concatenation of two strings

#include <stdio.h>
#include <string.h>
int main()
{
char a[100], b[100];
printf("Enter the first string\n");
gets(a);
printf("Enter the second string\n");
gets(b);
strcat(a,b);
printf("String obtained on concatenation is %s\n",a);
return 0;
}
QUESTIONS OR ASSIGNMENTS

You might also like