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

STRINGS Unit3

The document provides a comprehensive overview of strings in C programming, including their definition, declaration, initialization, and various input/output functions. It details string handling functions such as strlen, strcpy, strcat, strcmp, strrev, strlwr, and strupr, along with example code snippets for each function. Additionally, it explains the use of standard input/output functions like scanf, printf, getchar, putchar, gets, and puts.

Uploaded by

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

STRINGS Unit3

The document provides a comprehensive overview of strings in C programming, including their definition, declaration, initialization, and various input/output functions. It details string handling functions such as strlen, strcpy, strcat, strcmp, strrev, strlwr, and strupr, along with example code snippets for each function. Additionally, it explains the use of standard input/output functions like scanf, printf, getchar, putchar, gets, and puts.

Uploaded by

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

STRINGS

Definition:

 A string is defined as the sequence of characters terminated by the special character ‘\0’ (null
character).
 Strings are always enclosed in double quotes as “string constant”.

Declaring string variables:

In C, a string variable is any valid C variable name and is always declared as an array of
characters.
The general form of declaration of string variables is:

char string_name [size];

 char defines the string as array of characters.

 String-name defines the name of the string variable. It must be a valid ‘C’ identifier.

 size defines the number of characters in the string_name.

Example: char name [30];


char city [15];

Initialization of string variables (or) Initialization of arrays of char type:

The general form of initialization of string variable is:

char string_name [size] = “stringdata”;

Example: char name [30] = “ramu”;


char city [15] = { ‘g’,’u’,’n’,’t’,’u’,’r’,’\0’};
char str1 [ ] = {“jkc”};

name [0] name [1] name [2] name [3] name [4]

r a m u \0

Example:
/*Example program to illustrate the strings initialization*/

#include<stdio.h>
#include<conio.h>
void main()
{
char str1[20]="college";
char str2[20]={"college"};
char str3[20]={'c','o','l','l', 'e', 'g','e'};
1
clrscr();
printf("%s\n",str1);
printf("%s\n",str2);
printf("%s\n",str3);
getch();
}

Output: college
college
college

Q) String Input and Output functions:

1)The scanf() and printf() functions:

scanf ():

The scanf () is an input function which is used to accept a string into a string variable.

Syntax: scanf (“%s”, string variable);

Example: scanf (“%s”, str);

It is used to accept a string into str up to a white space character (Blank Space, New Line character,
Tab space). The null character ‘\0’ will be automatically appended to the string. %s is the format
specifier for strings.

printf ():

The printf () is an output function which is used to display a string.

Syntax: printf (“%s”, string variable);

Example: printf (“%s”, str);

It displays the string contained in str. %s is the format specifier for strings. str is the string variable
the contents of which are to be displayed.

Example:

/*TO illustrate string I/O functions using scanf() and printf()*/

#include<stdio.h>
#include<conio.h>
void main()
{
char str[15]; output:
clrscr(); Enter string into str: jkc
printf("Enter string into str:\n"); str=jkc
scanf("%s",str);
2
printf("str=%s\n",str);
getch();
}

2)The getchar() and putchar() functions:

getchar():

getchar() is an input function which is used to read a character through standard input device,
keyboard.

syntax: ch = getchar()

Where ch is a variable of char type. As a result of this, a character typed at the keyboard is
assigned to the variable ch.

putchar():

putchar() is an output function which is used to display the character.

synatx: putchar(ch);

Where ch represents variable of type char. It display the character stored in ch on the screen.

Example:

/*To illustrate getchar() and putchar() functions*/

#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
clrscr();
printf("Enter any character into ch:\n");
ch=getchar();
printf("The character in ch is:");
putchar(ch);
getch();
}

Output:
Enter any character into ch:
D
The charcater in ch is: D

3)The gets() and puts() Functions:


3
gets():

gets() is an input function which is used to accept a string up to a newline character into a
string variable. It automatically appends the null character ‘\0’ to the end of the string.

syntax: gets(variablename);

Example: gets(str);

It accepts a string up to a new line character into str. If str is “jkc college”, the entire string gets
accepted.

puts():

puts () is an output function which is used to display a string contained in a string variable. It
also adds the new line character ‘\n’ to the string automatically.

syntax: puts(variableaname);

example: puts(str);

It displays a string contained in a str.

Example:
/*To illustrate gets() and puts() functions*/

#include<stdio.h>
#include<conio.h>
void main()
{
char str[30];
clrscr();
printf("Enter any string into str:\n");
gets(str);
printf("The string in str is:\n"); output:
puts(str); Enter any string into str:
getch(); welcome to jkc
} The string in str is:
Welcome to jkc

Q)String handling functions or String manipulations:


4
The string handling functions that are avilable in C language is:

1) strlen()
2) strcpy()
3) strcat()
4) strcmp()
5) strrev()
6) strlwr()
7) strupr()

All these string handling built-in functions are declared in the header file string.h, this file has to be
included in all the programs which use these functions with the help of preprocessor directive
#include.

1) strlen():

This function is used to counts the number of characters excluding the null character ‘\0’
in the string.

Synatx: strlen(variablename);

Example: strlen(str);

Argument str represents a string. It can be a string constant or a string variable. The function
returns an inetger value, which is the length of the string.

Example:
/* Write a program to find the length of the string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int count;
char str[30];
clrscr();
printf("enter string into str:\n");
gets(str);
printf("The string in str is:");
puts(str);
count=strlen(str);
printf("\nThe total no of characters in str is:%d",count);
getch();
}

Output:
enter string into str:
welcome to jkc college
The string in str is:welcome to jkc college
The total no of characters in str is:21

5
2) strcpy():

This function is used to copy the content of one string into another string.

Synatx: strcpy(variablename1,variablename2);

Example: strcpy(str1,str2);

Here the string contained in str2 is copied to str1.

Example:

/* Write a program to copy one string to another string*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30],str2[30];
clrscr();
printf("enter string into str1:\n");
gets(str1);
printf("The string in str1 is:");
puts(str1);
strcpy(str2,str1);
printf("\nAfter copying string contained in str2 is:");
puts(str2);
getch();
}
Output:
enter string into str1:
jkc college
The string in str1 is:jkc college
After copying string contained in str2 is:jkc college

3) strcat():

This function is used to concatenates one string to the end of another string.

Synatx: strcat(variablename1,variablename2);

Example: strcat(str1,str2);

Here the string contained in str2 concatenates to the end of the string contained in str1.

Example:

/* Write a program to concatenate two strings*/


#include<stdio.h>
#include<conio.h>
#include<string.h>
6
void main()
{
char str1[30],str2[30];
clrscr();
printf("enter string into str1:\n");
gets(str1);
printf("enter string into str2:\n");
gets(str2);
printf("The string in str1 is:");
puts(str1);
printf("The string in str2 is:");
puts(str2);
strcat(str1,str2);
printf("\nAfter concatenate str1 and str2 string contained in str1 is:");
puts(str1);
getch();
}
Output:
enter string into str1:
jkc
enter string into str2:
college
The string in str1 is:jkc
The string in str2 is:college
After concatenate str1 and str2 string contained in str1 is:jkc college

4) strcmp():

This function is used to compare two strings.

Synatx: strcmp(variablename1,variablename2);

Example: strcmp(str1,str2);

Here the string contained in str1 is compared with the string in str2.

if str1=str2 then it returns 0


if str1>str2 then it returns positive
if str1<str2 then it returns negative

Example:

/* Write a program to compare two strings*/

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i;
char str1[30],str2[30];
7
clrscr();
printf("enter string into str1:\n");
gets(str1);
printf("enter string into str2:\n");
gets(str2);
printf("The string in str1 is:");
puts(str1);
printf("The string in str2 is:");
puts(str2);
i=strcmp(str1,str2);
printf("\ndifferene between str1 and str2 is:%d",i);
getch();
}
Output:
enter string into str1:
bsc
enter string into str2:
bca
The string in str1 is:bsc
The string in str2 is:bca
difference between str1 and str2 is:16

5) strrev():

This function is used to reverse the string.

Synatx: strrev(variablename1);

Example: strrev(str1);

Here the string contained in str1 is reversed.

Example:
/* Write a program to reverse the string*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[30];
clrscr();
printf("enter string into str1:\n");
gets(str1);
printf("The string in str1 is:");
puts(str1);
strrev(str1);
printf("\nRevresed string in str1 is:");
puts(str1);
getch();
}
Output:
8
enter string into str1:
bca
The string in str1 is:bca
Reversed string in str1 is:acb

6)strlwr():
The STRLWR() Function used to converts given string to lowercase.

Syntax:
strlwr(str1)

#include<string.h>
int main()
{
char str[100];
printf("Please, Enter the String = ");
gets(str);
printf(" \n The Lower case String is = %s",strlwr(str));
return 0;
}

7.strupr()
The STRUPR() Function used to converts given string to uppercase.

Syntax:
strupr(str1)

#include<string.h>

int main()
{

char str[100];

printf("Please, Enter the String = ");

gets(str);

printf(" \n The Upper case String is = %s",strupr(str));

return 0;

You might also like