string
string
A string is a sequence of characters enclosed within the double quotes. A string may include
letters, digits and various special characters such as +,-,*,/ and %.
Example:
1. “cnu”
2. “ashwika”
3. “1+2=3”
In C language a string is nothing but a null-terminated character array. This means that after
the last character, a null character (‘\0’) is stored to signify the end of the character array.
Example:
char str[]=”hello”;
Declaring and initializing string variables:
A string variable is declared as an array. The general form is:
Syntax: char string_name[size];
Example: char name[10];
char greeting[] = "Hello";
Actually, you do not place the null character at the end of a string constant. The C compiler
automatically places the '\0' at the end of the string when it initializes the array.
READING STRINGS:
The string can be read from the user by using three ways
1. Using scanf() function.
2. Using gets() function.
3. Using getchar(),getch() and getche() functions repeatedly.
Scanf(): The scanf() function is used to read the string. We use the % s format specification
to read string of characters.
Example: char college[10];
scanf(“%s”,college);
No ampersand is required before the variable college.
While reading a value using this function there should not be any white space. The string is
terminated once the white space is encountered.
Gets(): This function is used to read characters until enter key is pressed.
Syntax: gets(variable_name);
Example: char city[10];
gets(city)
17
UNIT-III
Getchar(): This function is used to read a single character at a time. This reading is
terminated when new line character (‘\n’) is encountered.
Example: getchar(ck);
WRITING STRINGS
The string can be displayed on the screen using three ways.
1. Using printf() function.
2. Using puts() function.
3. Using putchar() function.
Printf(): This function is used to print the string on the screen. We use %s format
specification.
Ex: printf(“%s”,city);
Puts(): The puts() functions is used to display the string on the screen.
Syntax: puts(variable_name);
Ex: puts(city)
Putchar(): This function is used to print or display one character at a time on the screen.
Example: putchar(city);
STRING OPERTIONS
18
UNIT-III
2. Converting characters of a string into upper case: Converts given small characters
into uppercase.
Example: program to convert lower case to upper case
#include<stdio.h> Ascii values of A-Z is 65-90
#include<conio.h>
Ascii values of a-z is 97-122
main()
{
char str[40],str1[40];
int i=0,j=0;
clrscr();
printf("\n Enter the string upto 40 characters \n");
gets(str);
while(str[i]!='\0') Output:
{
if(str[i]>='a'&& str[i]<='z') Enter the string upto 40 characters
str1[j]=str[i]-32;
else ashwika reddy
str1[j]=str[i];
i++; the string converted into upppercase is =ASHWIKA
j++; REDDY
}
str1[j]='\0';
printf("\n the string converted into upppercase is =%s ",str1);
getch();
}
3. Converting characters of a string to lower case: Converts given upper case
characters into lower characters.
Example: program to convert uppercase to lowercase
#include<stdio.h>
#include<conio.h>
main()
{
char str[40],str1[40];
int i=0,j=0;
clrscr();
printf("\n Enter the string upto 40 characters \n");
gets(str);
while(str[i]!='\0') OUTPUT:
{
if(str[i]>='A'&& str[i]<='Z') Enter the string upto 40 characters
str1[j]=str[i]+32;
else ashwika reddy
str1[j]=str[i];
i++; the string converted into upppercase is
j++; =ASHWIKA REDDY
}
str1[j]='\0';
printf("\n the string converted into upppercase is =%s ",str1);
getch();
}
19
UNIT-III
4. Concatenation: If str1 and str2 are two strings, then on concatenation str3 produces a
string which contains characters of str1 followed by the characters of str2.
Example: Concatenating two strings
#include<stdio.h>
Output:
#include<conio.h>
main() Enter the string one
{
char str[40],str1[40],str2[40]; sbvr degree
int i,j;
printf("\n Enter the string one \n"); Enter the string two
gets(str);
printf(" \n Enter the string two \n"); college
gets(str1);
for(i=0,j=0;str[i]!='\0';i++,j++) After concatenation the string is :sbvr degreecollege
str2[j]=str[i];
for(i=0;str1[i]!='\0';i++,j++)
str2[j]=str1[i];
str2[j]='\0';
printf("\n After concatenation the string is :");
printf("%s",str2);
getch();
}
5. Appending: Appending one string to another string involves copying the contents of
source string at the end of a destination string.
Example:
#include<stdio.h>
#include<conio.h>
Output:
main()
{ Enter the string1
char str[30],str1[30];
int i,j; i love
printf("\n Enter the string1 \n");
gets(str); Enter the string2
printf("\n Enter the string2 \n");
gets(str1); my mother
for(i=0;str[i]!='\0';i++);
for(j=0;str1[j]!='\0';j++,i++) The appended string is :i love my mother
{
str[i]=str1[j]; String2 is :my mother
}
str[i]='\0';
printf("\n The appended string is :%s",str);
printf("\n String2 is :");
printf("%s",str1);
getch();
}
20
UNIT-III
21
UNIT-III
22
UNIT-III
}
}
if(flag==1)
printf("\n The given string %s is palindrome ",str);
else
printf(" \n The given string %s is not palindrome ",str);
getch();
}
23