0% found this document useful (0 votes)
3 views

STRINGS

The document provides an overview of strings in C, explaining their structure as arrays of characters terminated by a NULL character. It covers string declaration, initialization, printing, reading, and various string manipulation functions such as strcpy(), strlen(), strcmp(), and strcat(). Additionally, it includes examples of implementing string functions without using built-in functions.

Uploaded by

zakriya918
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

STRINGS

The document provides an overview of strings in C, explaining their structure as arrays of characters terminated by a NULL character. It covers string declaration, initialization, printing, reading, and various string manipulation functions such as strcpy(), strlen(), strcmp(), and strcat(). Additionally, it includes examples of implementing string functions without using built-in functions.

Uploaded by

zakriya918
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

STRINGS

A string is an array of characters terminated by NULL character which is denoted by the


escape sequence ‘\0’.

A string is stored in a memory as a sequence of characters in an array terminated by ‘\0’. For


example, consider the string “GNDEC”. It is stored as:

G N D E C \0

0 1 2 3 4 5

The first element of a string is stored at 0 th position, the second element is stored at 1st
position and so on. In addition, the last character of the sting is stored at n-1 position, where n
is the total number of characters. Now, the difference between a numerical array and a
character array or string is that a string stores a NULL(‘\0’) character at the nth position,
while a numerical array does not store such a character. This means that, a character array or
string stores n+1 element, because the last character of the array is always NULL.

Declaring a string

As similar to a numerical array, a character array or a string is declared s follows:

Syntax:
char
Where, string_name[size];
string_name-> ame given to a string.
size-> size of the string.

Example:
char college[10];

Initializing a string

A character array or string variable is initialized by either assigning a character constant


individually or assigning all characters collectively.

When you initialize a string variable by assigning character constants individually, you use
single quotes.

Example: char college[6]={‘G’,’N’,’D’,’E’,’C’,’\0’};

In above example, an array of characters or a string variable, month is declared and


initialized. The size of this string variable is 6. All the character constants are assigned
individually.

Now, when you initialize a string variable by assigning all the characters collectively, you use
double quotes.

Example: char month[6]={“GNDEC”};


In the above example, an array of characters or a string variable, month is declared and
initialize. The size of this string variable is 6. All the character constants are assigned to the
individual elements of the array automatically. In addition, the preceding declaration adds the
NULL(\0) character at the end of the string.

Printing a string

It is possible to print a string using printf(). As long as the string is terminated by a NULL
character, the printf() function prints all the characters upto, but not including, the NULL
character. The printf() conversion specification for a string variable is %s.

You can use the printf() function to display the string data in two ways. Either you can pass
the string data directly within the printf() function or you can store the string data in a
character array and pass that array as a parameter in the printf() function.

Example:

1. printf(“I study in GNDEC”);


2. char college=”GNDEC”;
printf(“%s”,college);

Reading strings using scanf()

It is possible to read a string using scanf() function. The conversion specification for reading
a string using scanf() is %s.

Note that the ‘&’ symbol is not used with the name of a string variable getting a value from
scanf().

Example: char name[16];


printf(“enter your name”);
scanf(“%s”,name);

String manipulation functions

The standard library string.h contains many functions for string manipulation. Among them
are the following.

strcpy() – allows us to give a string an initial value as a unit rather than character by
character.

strncpy() – allows us to copy a block of n characters from one string to another.

strlen() – allows us to determine the length of a string.

strcmp() – allows us to compare one string with another.

strncmp() – allows us to compare a block of n characters from one string with those in
another.
strcat() – allows us to combine two strings.

Assigning a value to a string variable : strcpy()

The strcpy() function is used to copy the content of first string to second string or to assign a
value to a string. You have to pass two strings as an argument to the strcpy() function to copy
first string to second string. The strcpy() function will copy the entire string, including the
NULL character to the new location.

Syntax:
strcpy(dest,source);
Where,

dest -> a string variable whose value is going to be changed.


source -> a string variable which is going to be copied to dest.

Example:
char first[20];
char last[20];
strcpy(first,”Sourav Ganguly”);
strcpy(last,first);

strncpy() function

The strncpy() function is used to copy a specified number of characters from one string to
another string. This function takes three parameters, where two parameters are strings, which
have to be copied, and the third parameter is number of characters upto which the second
string is to be copied to the first string.

Syntax: strncpy(dest,source,
Where,

dest -> a string variable whose value is going to be changed.


source -> a string variable which is going to be copied to dest.
n -> number of characters upto which the second string is to be copied to the first
string.

Example:
char first[20]=” Sourav Ganguly”;
char last[20];
strncpy(last,first,6);

Determining the length and size of string: strlen() and sizeof()

The strlen() function can be used to find the length of the string in bytes upto NULL
character and excluding NULL character.

Syntax: length=strlen(str);
Where,

str -> it is a string.


length -> return value. Integer representing length in bytes excluding NULL
character.

Example:
char college[10]=”GNDEC”;
len=strlen(college);

The sizeof() operator can be used to determine the declared size of a string. It accepts one
operand, a variable or a type, and returns the number of bytes allocated to it.

Syntax:
size=sizeof(str);
or
size=sizeof(typen
ame);
Where,
str -> it is a string.
typename -> any basic data type.
size -> return value. Number of bytes allocated to that type.

Example:
char college[10]=”GNDEC”;
size=sizeof(college);
size2=sizeof(char);

Comparing strings: strcmp()

The strcmp() function is used to compare two string data. Strcmp() function takes two
parameter of strings for comparing. It returns an integer value indicating the relationship
between the strings. The returned value can be zero, greater than zero, or less than zero. A
zero value indicates that both strings are equal. A value greater than zero indicates that first
character that does not match has a greater value in string1 than in string2. Finally a value
less than zero indicate that the first character that does not match has lesser value in string1
than in string2.

Syntax:
result=strcmp(string1,string2);
Where,

string1, string2 -> any wo strings which are to be compared.


result -> a return value.

Example:
1. char str1=”GNDEC”;
char str2=”GNDEC”;
int res;
res=strcmp(str1,str2); //res==0
2. res=strcmp(“cat”,”car”); //res>0
3. res=strcmp(“big”,”little”); //res<0

strncmp() function

The strncmp() function is used to compare two strings upto a specified number of characters.
This fuction takes three parameters, where two parameters are the strings, which have to be
compared, and the third parameter is the number of characters upto which the two strings are
compared.

Syntax:
result=strncmp(string1,string2,
Where,

string1, string2 -> any wo strings which are to be compared.


result -> a return value.
n -> number of characters upto which the two strings are to be compared.

Example:
char str1=”GNDEC”;
char str2=”GND”;
int res;
res=strncmp(str1,str2,3);

Concatenation: joining two strings together strcat()

The strcat() function is used to concatenate two strings. The resulting string has only one
NULL character at the end. The strcat() function takes two parameters to add second strig to
the first string. This means that the second string is appended to the fist string and
concatenated string is stored in first string.

Syntax:
strcat(first,second);
Where,

first -> a string, where the second string is appended.


second -> a string to be appended to first string.

Example:
char str1[20]=”GND”;
char str2[20]=”EC”;
strcat(str1,str2);

String input and output functions: gets(), puts()

Printing a string: puts()


Although we can print a string using printf() function, it is better to use the puts function,
which is dedicated to printing strings.

Syntax: puts(string);
puts() function takes one parameter string, tha is to be printed. After printing the
string, puts() prints the newline character.

Example:

1. puts(“Welcome to GNDEC”);
2. char str[20]=”GNDEC”;
puts(str);

Reading a value into a character array: gets()

The gets() function allows reading in an entire line of input, including whitespace characters.
It reads everything upto a newline character.

Syntax:
n=gets(string)
Where,

str -> is an array of characters.


n -> Is a pointer to a string.

Example:
gets(str);

/*1. C-Program to implement strlen() without using inbuilt function.*/


#include<stdio.h>
#include<string.h>
void main()
{
char str[20];
int i=0;
printf("Enter a string\n");
scanf("%s",str);
while(str[i]!='\0')
{
i++;
}
printf("length of string=%d",i);

}
/*2. C-Pogram to implement strcpy() without using inbuilt function.*/
#include<stdio.h>
Void main()
{
char str1[20]={0},str2[20];
int i=0;
printf("enter a string2\n");
scanf(“%s”,str2);
while(str2[i]!='\0')
{
str1[i]=str2[i];
i++;
}
puts(str1);
puts(str2);
}

/*3. C-Program to implement strcat() function without using inbuilt function.*/


#include<stdio.h>
Void main()
{
char str1[20]="Fri",str2[20]="day";
int i,j;
i=0;
while(str1[i]!='\0')
{
i++;
}
j=0;
while(str2[j]!='\0')
{
str1[i]=str2[j];
i++;
j++;
}
puts(str1);
puts(str2);
}

You might also like