Sie sind auf Seite 1von 13

Program Based On String

1. write a program to find out the length of a given string. Then display
the same string with its length.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[25];
int i=0,len=0;
clrscr();
printf("Enter Any String : ");
scanf("%[^\n]s",&str);
while(str[i]!='\0')
{
i++;
len++;
}
clrscr();
printf("Entered String : %s.",str);
printf("\nLength Of String : %d.",len);
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String -1- Roll No :- 04MCA54


Program Based On String

2. Write a program to accomplish the followings:


- Read any two string. Say sti and st2.
- Compare sti and st2 and display the corresponding message
according to the following criteria:
If (sti =st2) then
display “Both strings are equal”
else
display “Both strings are not equal”

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str1[25],str2[25];
int i=0,flag;
clrscr();
printf("Enter String-1 : ");
scanf("%[^\n]s",&str1);
printf("Enter String-2 : ");
scanf("%[^\n]s",&str2);
while(str1[i]!='\0')
{
if(str1[i]==str2[i])
{
flag=1;
}
else
{
flag=0;
}
i++;
}
if(flag==0)
{
printf("Both Strings Are Equal.");
}
else
{
printf("Both Strings Are Not Equal.");
}
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String -2- Roll No :- 04MCA54


Program Based On String
3. Write a program to copy one string to another and count the number of
character copied.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

void main()
{
char strsrc[20],strdest[20];
int i=0;
clrscr();
printf("Enter String To Be Copied : ");
scanf("%s",strsrc);
while(strsrc[i]!='\0')
{
strdest[i]=strsrc[i];
i++;
}
printf("Copied String Is : %s.",strdest);
printf("\nLength Of String : %d",i);
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String -3- Roll No :- 04MCA54


Program Based On String
4. The names of employees are stored in three arrays, namely first
name, second name and last name. Write a program to concatenate
the three parts into one string to be called name.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char name[50]={'\0'},fname[15],mname[15],lname[15];
int i=0,j=0;
clrscr();
printf("Enter First Name : ");
scanf("%s",fname);
printf("Enter Middle Name : ");
scanf("%s",mname);
printf("Enter Last Name : ");
scanf("%s",lname);
while(fname[i]!='\0')
{
name[j]=fname[i];
i++;
j++;
}
i=0;
while(mname[i]!='\0')
{
name[j]=mname[i];
i++;
j++;
}
i=0;
while(lname[i]!='\0')
{
name[j]=lname[i];
i++;
j++;
}
printf("String Is : %s.",name);
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String -4- Roll No :- 04MCA54


Program Based On String
5. 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<conio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
char join[50]={'\0'},s1[15],s2[15],s3[15];
int i=0,j=0,flag=1;
clrscr();
printf("Enter First String : ");
scanf("%s",s1);
printf("Enter Second String : ");
scanf("%s",s2);
if(strlen(s1)!=strlen(s2))
{
flag=1;
}
else
{
while(s1[i]!='\0')
{
if(s1[i]==s2[i])
{
flag=0;
}
else
{
flag=1;
}
i++;
}
}
if(flag==1)
{
i=0;
while(s1[i]!='\0')
{
join[j]=s1[i];
i++;
j++;
}
i=0;

Path :- F:\MCA I Sem\FOP\Final\String -5- Roll No :- 04MCA54


Program Based On String
while(s2[i]!='\0')
{
join[j]=s2[i];
i++;
j++;
}
}
printf("\nFirst String Is : %s.",s1);
printf("\nSecond String Is : %s.",s2);
printf("\nConcated String Is : %s.",join);
printf("\n\nLength Of First String Is : %d.",strlen(s1));
printf("\nLength Of Second String Is : %d.",strlen(s2));
printf("\nLength Of Concated String Is : %d.",j);
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String -6- Roll No :- 04MCA54


Program Based On String
6. Write a program to extract a portion of a character string and print th
extractedstring. Assume that m characters are extracted, starting from
the nth
character.

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
char str[20];
int i=0,n,m;
printf("enter String : ");
scanf("%s",&str);
printf("Enter Value Of N : " );
scanf("%d",&n);
printf("Enter Value Of M : " );
scanf("%d",&m);
if(n<0 && m>strlen(str))
{
printf("Invalid String.");
getch();
exit(1);
}
printf("The String Is : %s.\n");
printf("The String Between %d & %d Is : \n");
for(i=n;i<m;i++)
{
printf("%c",str[i]);
}
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String -7- Roll No :- 04MCA54


Program Based On String
7. Write a program that will read a string and rewrite It In the
alphabetical order. For
example, the word “STRING” should be written as GINRST.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char str[25],temp;
int i,j;
clrscr();
printf("Enter String-1 : ");
scanf("%[^\n]s",&str);
clrscr();
printf("The Entered String Is : %s.",str);
for(i=0;i<strlen(str);i++)
{
for(j=0;j<strlen(str);j++)
{
if(str[i]<str[j])
{
temp=str[i];
str[i]=str[j];
str[j]=temp;
}
}
}
printf("\nThe Sorted String Is : %s.",str);
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String -8- Roll No :- 04MCA54


Program Based On String
8. Write a program to accomplish the following:
- Read a string. Say st = “CICAMCA”
- Generate and display the following type of pattern, using st.

C
C I
C I C
C I C A
C I C A M
C I C A M C
C I C A M C A

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str[20];
int i,j,n;
clrscr();
printf("enter String : ");
scanf("%[^\n]s",&str);
for(i=0;i<strlen(str);i++)
{
printf("\n");
for(j=0;j<=i;j++)
{
printf(" %c ",str[j]);
}
}
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String -9- Roll No :- 04MCA54


Program Based On String
9.
C C
C I I C
C I C C I C
C I C A A C I C
C I C A M M A C I C
C I C A M C C M A C I C
C I C A M C A A C M A C I C

#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str[20];
int i,j,n;
clrscr();
printf("enter String : ");
scanf("%[^\n]s",&str);
for(i=0;i<=strlen(str);i++)
{
printf("\n");
for(j=0;j<=i;j++)
{
if(j==i)
{
printf(" %c ",str[j]);
}
else if(j<i)
{
printf(" ");
}
else
{
printf(" %c ",str[j]);
}
}
for(j=0;j<strlen(str);j++)
{
if(j==i)
{
printf(" %c ",str[j]);
}

Path :- F:\MCA I Sem\FOP\Final\String - 10 - Roll No :- 04MCA54


Program Based On String
else if(j<i)
{
printf(" ");
}
else
{
printf(" %c ",str[j]);
}
}
}
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String - 11 - Roll No :- 04MCA54


Program Based On String
10. Write a program to do the following (Do not use any built in string
functions):
- read any string, say St1.
- Validate the entered string according to the following rules:
o String must contain only alphabets (both capital & small) and/or blank
spaces.
o Length of string must be less than or equal to 65.
o If string is valid then do the following operations else display an
appropriate message.
 Convert each capital character into small character and each small
character into capital. Say this new string as st2.
 Reverse both the string st1 & st2 and store them into rstl and rst2
respectively.

 Display the followings:


String st1 and st2.
Reverse string rstl and rst2.
Length of st1.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char str1[70]={'\0'},str2[70]={'\0'},rstr1[70]={'\0'},
rstr2[70]={'\0'};
int i=0,j=0,len=0;
clrscr();
fflush(stdin);
printf("Enter Any String : ");
scanf("%s",str1);
while(str1[i]!='\0')
{
if(!(int(str1[i])>=65 && int(str1[i])<=90))
{
if(!(int(str1[i])>=97 && int(str1[i])<=122))
{
printf("Invalid String.");
getch();
exit(1);
}
}
i++;
}
len=i;
if(i>66)
{
printf("Invalid Length Of String.");
getch();
exit(1);
}

Path :- F:\MCA I Sem\FOP\Final\String - 12 - Roll No :- 04MCA54


Program Based On String
clrscr();
printf("\nEntered String Is : %s.\n",str1);
i=0;
while(str1[i]!='\0')
{
if(isupper(str1[i]))
{
str2[i]=tolower(str1[i]);
}
else
{
str2[i]=toupper(str1[i]);
}
i++;
}
printf("\nConverted String Is : %s.",str2);
printf("\nLength Of String1 Is : %d.",len);
i=strlen(str1)-1;
while(str1[i]!='\0')
{
rstr1[j]=str1[i];
i--;
j++;
}
i=strlen(str2)-1;
j=0;
while(str2[i]!='\0')
{
rstr2[j]=str2[i];
i--;
j++;
}
printf("\nReverse Of String1 Is : %s.",rstr1);
printf("\nReverse Of String2 Is : %s.",rstr2);
getch();
}

Path :- F:\MCA I Sem\FOP\Final\String - 13 - Roll No :- 04MCA54

Das könnte Ihnen auch gefallen