100% found this document useful (1 vote)
104 views

Strings: Declaring and Initializing String Variables

A string is a sequence of characters that is treated as a single data item. In C, strings are implemented as character arrays that end with a null character. Common string operations include concatenation, comparison, copying and extracting portions. Functions like strcpy(), strcat(), strcmp() and strlen() are used to manipulate strings. Strings can be read from the user using scanf() and gets() and written to output using printf() and puts().

Uploaded by

ishandeep
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
104 views

Strings: Declaring and Initializing String Variables

A string is a sequence of characters that is treated as a single data item. In C, strings are implemented as character arrays that end with a null character. Common string operations include concatenation, comparison, copying and extracting portions. Functions like strcpy(), strcat(), strcmp() and strlen() are used to manipulate strings. Strings can be read from the user using scanf() and gets() and written to output using printf() and puts().

Uploaded by

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

Strings

A string is a sequence of characters that is treated as a single data item.


Any group of characters defined between double quotation marks is a
string constant.
Character strings are often used to build meaningful and readable
program. The common operation performed on character strings include
Reading and writing strings
Combining strings together
Copying one string to another
Comparing strings for equality
Extracting a portion of a string
Declaring and initializing string variables:
C does not support string as datatype. However, it allows us to represent
strings as a character array. The general form of declaration of a string
variable is:
Char string_name[size];
When the compiler assigns a character string to a character array, it
automatically supplies a null character (\0) at the end of the string.
Therefore, the size should be equal to the maximum number of
characters plus one.
Character array can be initialised in either of the following two forms:
Char city [9]=NEW YORK;
Char city [9]={N,E,W, ,Y,O,R,K,\0};
C also permits us to initialise a character array without specifying the
number of elements.
Char string [ ]={G,O,O,D,\0};
Reading strings from terminal
Using scanf function:
The familiar function scanf can be used with %s format specification to
read in a string of characters.
Char address[10];
scanf(%s, address );
The problem with scanf function is that it terminates its input on the first
white space it finds.
Note: In case of character array, the ampersand(&) is not required before
the variable name.
For reading a line of text, C supports a format specification known as the
edit set conversion code %[. .]. It can be used to read a line containing a
variety of characters including white spaces.
Using gets function:
Another convenient method of reading a string of text containing
whitespaces is to use the library function gets.
Gets(str);
Writing strings to screen
Using printf function:
Printf function with %s format can be used to print strings to screen.
The format %s can be used to display an array of characters that is
terminated by the null character.
Printf(%s, name);
Using puts function:
Another convenient way of printing string values is to use the function
puts. This is one parameter function.
Puts(str);
Arithmetic operation on characters
C allows us to manipulate characters the same way we do will numbers.
We may write character as an integer.
We can also perform arithmetic operations on characters.
We may also use character constants in relational expression
Putting strings together:
We cannot simply join two strings together by arithmetic addition.
Instead characters from one string should be copied into another string
one after the other. The process of combining two strings together is
called concatenation.
Comparison of two strings
C does not permit the comparison of two strings directly. It is therefore
necessary to compare the two strings character by character. The
comparison is done until there is a mismatch or one of the strings
terminates into a null character.
Strings-handling functions
The C library supports a large number of strings-handling functions.
Following are the most commonly used string-handling functions.
Strcat() concatenate two strings
Strcmp() compares two strings
Strcpy() copies one string over another
Strlen() finds lengths of a string
Strcat() function:
The strcat() function joins two strings together. It takes the following
form:
Strcat(string1, string2);
When the function strcat is executed, string2 is appended to string1. It
does so by removing the null character at the end of string1 and placing
string2 from there. The string at string2 remains unchanged.
Strcmp() function:
The strcmp function compares two strings identified by the arguments
and has a value of 0 if they are equal. If they are not, it has the numeric
difference between the first non-matching character. It takes the form:
Strcmp(string1, string2);
Strcpy() function:
The strcpy function works almost like a string-assignment operator. It
takes the form:
Strcpy(string1, string2);
It assigns the content of string2 in string1.
Strlen() function:
The function counts and returns the number of characters in a string. It
takes the form:
N=strlen(string);
where n is an integer variable, which receives the value of the length of
the string.
Strncpy() function:
In addition to the function strcpy, we have another function strncpy that
copies only the left most n characters of the source string to the target
string. This is a three parameter function and is invoked as follows:
Strncpy(string1, string2, n);
Strncmp() function:
This function has three parameter as illustrated below:
Strncmp(string1, string2, n);
This function compares the left most n characters and return
0 if they are equal
Negative number if string1 sub-string is less than string2
Positive number otherwise.
Strncat() function:
This is another concatenation function that takes three parameters as
shown below:
Strncat(string1, string2, n);
It will concatenate the left most ncharacters.
Strstr() function:
It is a two parameter function that is used to locate a sub-string in a
string. This takes the form:
Strstr(string1, string2);
The function strstr searches weather string2 is contained in string1. If
yes, the function returns the position of the first occurrence of the sub-
string, otherwise it returns a NULL pointer.
Table of strings
To store list of character strings we use two-dimensional character array.
Example:
Char str[ ][ ]
{
Chandigarh,
Chennai,
Mumbai,
Hyderabad
};
Programs
1. Program to extract n characters from a string
main()
{
int n,i=0;
char a[20],b[20];
printf("enter n and str a");
scanf("%s",a);
scanf("%d" ,&n);
while(a[i]!='\0' && i<n)
{
b[i]=a[i];
i++;
}
printf("%s",b);
}

2.program to demonstrate getschar


void main()
{
char ch,a[10],b[10];
int i=0;
printf("enter the strings");

do
{
ch=getchar();
a[i]=ch;
i++;
}while(ch!='\n');
printf("%s",a);
}
3.program to print upper case to lower case and vice versa
void main()
{
char str[20];
printf("enter str");
scanf("%s",str);
int i;
for(i=0;i!='\0';i++)
{
if(str[i]>='A' && str[i]<='Z')
{
str[i]+=32;
}
else
{
str[i]-=32;
}
}
printf("%s",str);
}
4.program to print the length of the string
void main()
{
char str[30];
int i,c=0;
printf("enter str");
scanf("%s",str);
for(i=0;i<=30;i++)
{
if(str[i]!=0)
{
c++;
}
else
break;
}
printf("length=%d",c);
}

5.program to extract n characters from the end of the string


main()
{
int n,i=0,j;
char a[20],b[20];
printf("enter n and string");
scanf("%d",&n);
scanf("%s",a);
while(a[i]!='\0')
{
i++;
}
j=i;
i=0;
while(j>=n && i<=n)
{
b[i]=a[j];
printf("%c",b[i]);
j--;
i++;
}
}
6. main()
{
int n,i=0,j,x;
char a[20],b[20];
printf("enter n and string");
scanf("%d",&n);
scanf("%s",a);
while(a[i]!='\0')
{
i++;
}
j=i;
i=0;
while(j>=n && i<=n)
{
b[i]=a[j];
printf("%c",b[i]);
j--;
i++;
}
for(x=i;x<=20;x++)
{
b[x]='\0';
}
printf(b);
}
7.string functions
void main()
{
char a[20],b[20],d[30],e[30];
int n,c,i=0;
printf("enter the strings");
scanf("%s %s",a,b);
n=strlen(a);
c=strcmp(a,b);
strcat(a,b);
printf(a);
strcpy(a,b);
printf(a);
printf(n,c);
}

You might also like