1. !!!!
""""
Strings/Character Array: in C language the group of characters,
digits and symbols enclosed within quotation marks are called
string.
- The string always declared as character array.
- In other word character array are called string.
- Every string is terminated with ‘0’ (NULL) character.
- The null character is a byte with all bits at logic zero.
- For example: char name[ ]={‘S’,’A’,’N’,’T’,’T’,’O’,’S’,’H’};
- Each character of the string occupies 1 byte of memory.
- The last character is always‘0’.
- It is not compulsory to write, because the compiler
automatically put’0’ at the end of character array or string.
- The character array stored in contiguous memory locations
- For example Memory map of string
1001 1001 1002 1003 1004 1005 1006 1007
Declaration and initialization of string:
Char name [ ] =”SANTOSH”;
Q. Write a program to display string
Void main ()
{
Char name [6] = {‘J’,’A’,’C’,’K’,’Y’};
Clrscr ();
Printf (“Nam=%s”, name);
}
Output: JACKY
S A N T O S H 0
2. !!!!
""""
Important Note: it character array no need of write & in scanf
statement because as we know that string is a collection of
character, which is also called character array. The character are
arranged or stored in contiguous memory location, so no need of
address to store the string.
How to read a character
- To read a character use the following function
- char getch()
- char getche()
Difference between getch() and getche():
- In case of getch() the input data does not visible on screen.
- In case of getche() the input data visible on screen.
How to display a character:
- To display a character use following function.
- putch() void putch(data)
How to read a string:
- To read a string use following function
- gets(): whole string is accepted
How to display a string:
- puts ( ): it is used to display a string.
Example:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[10],i;
clrscr();
printf("Enter a string");
gets(a);// to read a string
3. !!!!
""""
puts(a);// it is used to display a string
printf("%s",a);//display the string
for(i=0;a[i]!='0';i++)
printf("%c",a[i]);
getch();
}
Output: Enter a string: JACKY
JACKY
JACKY JACKY
Explanation: ill print three times because for gets (),puts(),and
printf();
Standard String Functions:
Function Description
Strlen() Determine length of string
Strcpy() copies a string from source to destination
Strcmp() a) compare character of two string (function
determine between small and big)
b) if the two string equal it return zero otherwise it
return nonzero.
c) it the ASCII value of two string.
d) it stop when either end of string is reached or the
corresponding character are not same.
Strcat() append source string to destination string.
Strrev() reverse all character of a string.
Strlwr() it is used to convert a string to lower case.
Tolower() it is used to covert a character to lower case
Strupr() it is used to convert string to upper case.
Touppper() it is used to convert a character to upper case
4. !!!!
""""
Program: WAP to find the length of string Strlen()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char p[50];
int len;
clrscr();
printf("Enter a Text:n");
gets(p);
len=strlen(p);
printf("length of string=%d",len);
getch();
}
Program: WAP to find the length of string without using Strlen()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char p[50];
int len=0,i;
clrscr();
printf("Enter a string:n");
gets(p);
for(i=0;p[i]!='0';i++)
len++;
printf("length of string %s=%d",p,len);
getch();
}
5. !!!!
""""
Program: WAP to input a string then copy one string to another
string using strcpy ().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50];
clrscr();
printf("Enter a string:n");
gets(a);
strcpy(b,a);
printf("string %s is copy %s",a,b);
getch();
}
Program: WAP to input a string then copy one string to another
string without using strcpy ().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50];
int i;
clrscr();
printf("Enter a string:n");
gets(a);
for(i=0;a[i]!='0';i++)
b[i]=a[i];
b[i]='0';
printf("string %s is copy %s",a,b);
getch();
}
6. !!!!
""""
Program: WAP to input two string then joined it using strcat()
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50];
int i;
clrscr();
printf("Enter ist string:n");
gets(a);
printf("Enter 2nd string:n");
gets(b);
strcat(a,b);
printf("%s",a);
getch();
}
Program: WAP to input two string then compare it using
strcmp().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50];
clrscr();
printf("n Enter 1st string");
gets(a);
printf("Enter 2nd string");
gets(b);
if(strcmp(a,b)==0)
printf("equal");
else
7. !!!!
""""
printf("not equal");
getch();
}
Program: WAP to input two string then compare it without
using strcmp().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50];
int i=0;
clrscr();
printf("n Enter 1st string");
gets(a);
printf("Enter 2nd string");
gets(b);
while(a[i]!='0'&&b[i]!='0'&&a[i]==b[i])
i++;
if(a[i]==b[i])
printf("equal");
else
printf("not equal");
getch();
}
Program: WAP to input a string then reverse it using strrev().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
clrscr();
8. !!!!
""""
printf("n Enter 1st string");
gets(a);
strrev(a);
printf("n %s",a);
getch();
}
Program: WAP to input a string then reverse it without using
strrev().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50],b[50];
int i,k=0;
clrscr();
printf("n Enter a string");
gets(a);
for(i=strlen(a)-1;i>=0;i--)
b[k++]=a[i];
b[k]='0';
printf("n %s",b);
getch();
}
Program: WAP to input a string then convert it into lower case
using strrev().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
clrscr();
9. !!!!
""""
printf("n Enter a string");
gets(a);
strlwr(a);
printf("n %s",a);
getch();
}
Program: WAP to input a string then convert it into lower case
without using strlwr().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
int i;
clrscr();
printf("n Enter a string");
gets(a);
for(i=0;a[i]!='0';i++)
{
if(a[i]>=65&&a[i]<=90)
a[i]=a[i]+32;
}
printf("n %s",a);
getch();
}
Program: WAP to input a string then convert it into lower case
using tolower().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
10. !!!!
""""
char a[50];
int i;
clrscr();
printf("n Enter a string");
gets(a);
for(i=0;a[i]!='0';i++)
{
if(a[i]>=65&&a[i]<=90)
a[i]=tolower a[i];
}
printf("n %s",a);
getch();
}
Program: WAP to input a string then convert it into lower case
using strupr ().
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
clrscr();
printf("n Enter a string");
gets(a);
strupr(a);
printf("n %s",a);
getch();
}
Program: WAP to input a string then convert it into lower case
without using strupr ().
#include<stdio.h>
#include<conio.h>
#include<string.h>
11. !!!!
""""
void main()
{
char a[50];
int i;
clrscr();
printf("n Enter a string");
gets(a);
for(i=0;a[i]!='0';i++)
{
if(a[i]>=97&&a[i]<=122)
a[i]=a[i]-32;
}
printf("n %s",a);
getch();
}
Program: WAP to input a string then find out occurrence of
each character.
#include<string.h>
#include<stdio.h>
#include<conio.h>
void main()
{
char a[50];
int i,k,j,c;
clrscr();
printf("Enter a string");
gets(a);
for(i=0;i<strlen(a);i++)
{
c=1;
for(j=i+1;j<strlen(a);j++)
{
if(a[i]==a[j])
12. !!!!
""""
{
c++;
for(k=j;k<strlen(a);k++)
a[k]=a[k+1];
j--;
}
}
printf("n%c is print %d times",a[i],c);
}
getch();
}
Program: WAP to input a string then find out how many words
are there.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
int w=0,i=0;
clrscr();
printf("n Enter a string");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]==32)
w++;
}
printf("n no of words=%d",w);
getch();
}
13. !!!!
""""
Program: WAP to input a string then find out how many digits,
space, alphabets, special symbols.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
int i,p=0,d=0,s=0,sp=0;
clrscr();
printf("n Enter a string");
gets(a);
for(i=0;i<strlen(a);i++)
{
if(a[i]>=65&&a[i]<=90||a[i]>=97&&a[i]<=122)
p++;
else if(a[i]>=48&&a[i]<257)
d++;
else if(a[i]==32)
s++;
else
sp++;
}
printf("n no of alphabets=%d",p);
printf("n no of digits=%d",d);
printf("n no of space=%d",s);
printf("n no of special symbol=%d",sp);
getch();
}
14. !!!!
""""
Program: WAP to input a string then find out how many vowels
are there.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
int i,v=0;
clrscr();
printf("n Enter a string");
gets(a);
for(i=0;a[i]!='0';i++)
{
if(a[i]=='a'||a[i]=='A'||
a[i]=='e'||a[i]=='E'||
a[i]=='i'||a[i]=='I'||
a[i]=='o'||a[i]=='O'||
a[i]=='u'||a[i]=='U')
v++;
}
printf("n Total no of vowes=%d",v);
getch();
}
Program: WAP to input a string then convert into lower case.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[50];
int i;
clrscr();
15. !!!!
""""
printf("n Enter a string");
gets(a);
for(i=0;a[i]!='0';i++)
{
if(a[i]>=65&&a[i]<=90)
a[i]=tolower a[i];
}
printf("n %s",a);
getch();
}
Program: WAP to input a string then check string is palindrome
or not.
#include<stdio.h>
#include<string.h>
main()
{
char a[100], b[100];
printf("Enter the string to check if it is a palindromen");
gets(a);
strcpy(b,a);
strrev(b);
if( strcmp(a,b) == 0 )
printf("Entered string is a palindrome.n");
else
printf("Entered string is not a palindrome.n");
return 0;
}
16. !!!!
""""
Program: WAP to input a string then construct a structure.
#include<stdio.h>
#include<string.h>
main()
{
char string[100];
int c, k, length;
printf("Enter a stringn");
gets(string);
length = strlen(string);
for ( c = 0 ; c < length ; c++ )
{
for( k = 0 ; k <= c ; k++ )
{
printf("%c", string[k]);
}
printf("n");
}
return 0;
}
Program: WAP to input a string then sorting of string .
#include<stdio.h>
int main(){
int i,j,n;
char str[20][20],temp[20];
puts("Enter the no. of string to be sorted");
scanf("%d",&n);
for(i=0;i<=n;i++)
gets(str[i]);
for(i=0;i<=n;i++)
for(j=i+1;j<=n;j++){
if(strcmp(str[i],str[j])>0){
strcpy(temp,str[i]);
18. !!!!
""""
Program: WAP to input a string then input a character and
check occurrence of character.
#include<stdio.h>
#include<string.h>
main()
{
char a[100],ch;
int i,c=0;
printf("Enter a stringn");
gets(string);
printf("Enter a stringn");
ch=getch();
for ( i=0;i<strlen(a);i++)
{
If[a[i]==ch)
C++;
}
Printf(“Total no of %c in string %d”,c);
}
getch();
}