Aim: 7.1 Write a program to find a character from a given string.
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char str[100],c;int i,lenf=0;
int len,f=0;
printf("Enter a string : ");
gets(str);
len=strlen(str);
printf("\nEnter a character to find its position : ");
scanf("%c",&c);
for(i=0;i<len;i++)
{
if(str[i]==c)
{
printf("\nCharacter position : %d",i+1);
f=1;
}
}
if(f==0)
{
printf("\nCharacter not found");
}
}
Output screenshot:
Aim: 7.2 Write a program to replace a character in a given string.
Code:
#include<stdio.h>
#include<string.h>
int main()
{
char s[100],c1,c2;
int i;
printf("\n Enter a String : ");
gets(s);
printf("\n Enter Character to replace from string : ");
c1=getchar();
getchar();
printf("\n Enter Character to Replace : ");
c2=getchar();
printf("\n String Before replace : %s",s);
for(i=0;s[i];i++)
{
if(s[i]==c1)
{
s[i]=c2;
}
}
printf("\n String After replace : %s",s);
return 0;
}
Output screenshot:
Aim: 7.3 Write a program to delete a character in a given string.
Code:
#include<stdio.h>
int main()
{
char str[50],ch;
int i,j;
printf("\n Enter a String : ");
gets(str);
printf("\n Enter Character to Delete : ");
ch=getchar();
for(i=0;str[i]!='\0';i++)
{
if(str[i]==ch)
{
for(j=i;j<str[j]!='\0';j++)
{
str[j]=str[j+1];
}
i--;
}
}
printf("\n New String : %s",str);
return 0;
}
Output screenshot:
Aim: 7.4 Write a program to reverse string.
Code:
#include<stdio.h>
int main()
{
char str[10];
int i,c=0;
printf("\n Enter a String : ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
c++;
}
printf("\n String Reverse is : ");
for(i=c;i>=0;i--)
{
printf("%c",str[i]);
}
return 0;
}
Output screenshot:
Aim: 7.5 Write a program to convert string into upper case.
Code:
#include<stdio.h>
int main()
{
char str[50];
int i;
printf("\n Enter a String : ");
scanf("%s",str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>='a' && str[i]<='z')
{
str[i]=str[i]-32;
}
}
printf("\n Upper Case String : %s",str);
return 0;
}
Output screenshot:
No comments:
Post a Comment
If you have any doubts or suggestions, Please let me know