STRING
A string is a series of characters treated as a single unit.
•A string may include letters, digits and various special characters such as +, -, *,
/ and $.
•In C language strings are stored in array of char type along with null
terminating character ‘\0’ at the end.
•String literals or string constants in C are written in double quotation marks as
follows:
“1000 Main Street”
Syntax :-
char string_name[size];
Reading strings:
Strings can be read from the user by using three ways;
1. Using scanf() function
2. Using gets() function
3. Using getchar(), getch(), or getche() function repeatedly
Using scanf() function
A string can be read using scanf() by writing scanf(“%s”,variable_name);
Although the syntax of scanf() function is well known and easy to use, the main
pitfall with this function is that it terminates as soon as it finds a blank space.
Example:
……..
char str[10];
printf(“Enter a string\n”);
scanf(“%s”,str);
………….
Using gets() function
The string can be read by writing
gets(str);
gets() is a function that overcomes the drawbacks of scanf(). The
gets() function takes the starting address of the string which will
hold the input. The string inputted using gets() is automatically
terminated with a null character.
Example:
…………..
char str[10];
printf(“Enter a string\n”);
gets(str);
…………..
Using getchar(), getch(), or getche() function repeatedly
The string can also be read by calling the getchar() repeatedly to
read a sequence of single characters (unless a terminating
character is encountered) and simultaneously storing it in a
character array as follows:
Writing string
The string can be displayed on screen using three ways:
1. Using printf() function
2. Using puts() function
3. Using putchar() function repeatedly
•The string can be displayed using pintf() by writing
printf(“%s”,str)
•The next method of writing a string is by using the puts() function.
• The string can be displayed by writing:
puts(str);
•It terminates the line with a newline character (‘\n’).
Finally the string can be written by calling the putchar( )
function repeatedly to print a sequence of single characters.
int i=0;
char str[10];
while(str[i]!=’\0’)
{
putchar(str[i]);
i++;
}
Example: Read and display a string
#include<stdio.h>
#include<conio.h>
main()
{
char str[20];
printf(“\n Enter a string:\n”);
gets(str); / scanf(“%s”, str);
printf(“The string is:\n”);
puts(str); / printf(“%s/n”,str);
getch();
}
Write a C program to input a word from keyboard and how each character followed by
a new line
Sample Input: Bangladesh
Sample Output:
B
a
n
g
l
a
d
e
s
h
Write a program using for loop to print the following output
#include<stdio.h>
main()
{
int c, d;
char string[] = "CProgramming";
printf("\n\n");
for( c = 0 ; c <= 11 ; c++ )
{
d = c + 1;
printf("|%-12.*s|\n", d, string);
}
for( c = 11 ; c >= 0 ; c-- )
{
d = c + 1;
printf("|%-12.*s|\n", d, string);
}
}
%12.*s, %.*s, and %*.1s
Character strings are often used to build meaningful
and readable programs. The common operations
performed on character strings include:
1. Combining strings together.
2. Copying one string to another.
3. Comparing strings for equality.
4. Extracting a portion of a string.
String-Handling Functions
Example: Write a program to read string and determine no. of
characters
#include<stdio.h>
#include<conio.h>
#include<string.h>
main()
{
int d;
char line[80];
gets(line);
d=strlen(line);
printf("%s length = %d ",line,d);
}
Write a program to find the number of
vowels and consonants in a text string
Algorithm
Flow-chart
Program
Example: Consider s1, s2 and s3 are three string variables. Write a
program to read two string constants into s1 and s2 and compare
whether they are equal or not. If they are not, join them together.
Then copy the contents of s1 to the variable s3. At the end the
program should print the contents of all three variables and their
length.
#include<stdio.h>
#include<string.h>
main()
{
char s1[20],s2[20],s3[20];
int x,a1,a2,a3;
printf("\n Enter two string constants\n");
scanf("%s%s",s1,s2);
x=strcmp(s1,s2);
if(x!=0)
{
printf("\n Strings are not equal\n");
strcat(s1,s2);
}
else
printf("\n Strings are equal\n");
strcpy(s3,s1);
a1=strlen(s1);
a2=strlen(s2);
a3=strlen(s3);
printf("\n length of strings :\n s1_ %s= %d\n s2_ %s= %d\n s3_
%s= %d",s1,a1,s2,a2,s3,a3);
}