Strings
Strings
Strings
Introduction
4. strlwr()
This function is used to convert the given string into the lower case string.
Syntax: strlwr(string);
Example: strlwr(“Hello World”);
Output: hello world
5. strupr()
This function is used to convert the given string into the upper case string.
Syntax: strupr(string);
Example: strupr(hello world”)
Output: HELLO WORLD
6. strcat()
This function is used to concatenate (appending the second string at the end of
the first string) two given strings.
Syntax: strcat(string1,string2);
Example: strcat(“Hello” “World”);
Output: HelloWorld
The following program illustrate the use of strcat() function.
Aim: C-program to demonstrate the use of strcat() function.
Program: /*strcat.c*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
clrscr();
printf("\n Enter the first string:");
gets(s1);
printf("\n Enter the second string:");
gets(s2);
/* Performing concatenation*/
strcat(s1,s2);
printf("\n The result string is: %s",s1);
getch();
}
Output:
7. strchr()
This function determines the first occurrence of the given character with in the
given string.
Syntax: strchr(string, character)
Example: strchr(“abcbcc”,’b’);
Output: 1
Programs
1. Write a C-Program to check whether the given string is palindrome or not.
Aim: C-Program to check whether the given string is palindrome or not.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
clrscr();
printf("\n Enter a string:");
gets(s1);
strcpy(s2,s1);
strrev(s1);
if(strcmp(s1,s2)==0)
printf("\n The given string is palindrome");
else
printf("\n The given string is not a palindrome");
getch();
}
Output:
#include <stdio.h>
int main()
{
char ch;
printf("Enter the alphabet:");
scanf("%c",&ch);
if(ch>=65 && ch<=90)
printf("upper case");
printf("lower case");
else
printf("Invalid input");
return 0;
}
Output
Enter the alphabet:g
lower case
The above represents book is a string variable capable of storing two strings and each
string size is 10.
The following program will explain how to use array of strings in C-Language.
Aim: C-Program to demonstrate array of strings.
Program: /*strarray.c*/
#include<stdio.h>
#include<conio.h>
void main()
{
char book[20][20];
int n,i;
clrscr();
printf("\n Enter the number of books:");
scanf("%d",&n);
printf("\n Enter the books tittles\n");
for(i=0;i<=n-1;i++)
{
scanf("%s",book[i]);
}
printf("\n The book tittles are....\n");
for(i=0;i<n;i++)
{
puts(book[i]);
}
getch();
}
Output:
Important Questions
1. Define string and explain about declaration and initialization of strings in C.
2. Define string and explain about string handling functions in C with suitable examples.
3. Define string and explain about character array in C with example program.