Anda di halaman 1dari 5

STRINGS IN C

String programs for the references


/* Q1 Programme to use gets() and puts() special function */
#include<stdio.h>
void main()
{
char str[10];
printf("\n Enter your string ");
gets(str);
printf("\n Your string is ");
puts(str);

/*Q2 Programme to count no of upper case lower case digits and special characters */
#include<stdio.h>
void main()
{
char str[100];
int i,c1=0,c2=0,c3=0,c4=0;
printf("\n Enter your string ");
gets(str);
for(i=0 ; str[i]!='\0' ; i++)
{
if(str[i]>='A' && str[i]<='Z') // if(str[i]>=65 && str[i]<=90)
c1++;
else
if(str[i]>='a' && str[i]<='z') //if(str[i]>=97 && str[i]<=122)
c2++;
else
if(str[i]>='0' && str[i]<='9') //if(str[i]>=48 && str[i]<=57)
c3++;
else
c4++;
}
printf("\nUpper case is: %d ",c1);
printf("\nLower case is: %d",c2);
printf("\nDigit is: %d",c3);
printf("\nSpecial Character is: %d",c4);

/*Q3 Programme to count no of vowels and consonants in a string */


#include<stdio.h>
void main()
{
char str[100];
int i,c1=0,c2=0;
printf("\n Enter your string ");
gets(str);
for(i=0;str[i]!='\0';i++)
{

if(str[i]=='a'||str[i]=='e'||str[i]=='i'||str[i]=='o'||str[i]=='u'||str[i]=='A'||str[i]=='E'||str[i]=='I
'||str[i]=='O'||str[i]=='U')
c1++;
else
c2++;
}
printf("\n Vowels are=%d",c1);
printf("\n Conconents are=%d",c2);

/*Q4 Programme to Convert upper case to lower case lower to upper case*/
#include<stdio.h>
void main()
{
char str[100];
int i,c1=0,c2=0;
printf("\n Enter your string ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
if(str[i]>=65 && str[i]<=90)
str[i]=str[i]+32;
else
str[i]=str[i]-32;
}
printf("\n Output=%s",str);

/*Q5 Programme to count no of character and words*/


#include<stdio.h>
main()
{
char str[100];
int i,c1=0,c2=0;
printf("\nEnter a string ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
c1++;
if(str[i]==' ')
{c1--;
c2++;}
}
printf("\nCharacter are=%d\nWords are=%d",c1,c2+1);
}
/*Q6 Programme to read a string and copy into another string*/
#include<stdio.h>
void
main ()
{
char s1[20], s2[20];
int i;
printf ("\nEnter a string ");
gets (s1);
for (i = 0; s1[i] != '\0'; i++)
{
s2[i] = s1[i];
}
s2[i] = '\0';
printf ("\n Copied string is=%s", s2)
}
/*Q7 programme to pint ACSII value of each character*/
#include<stdio.h>
main()
{
char str[10];
int i;
printf("\nEnter a string ");
gets(str);
for(i=0;str[i]!='\0';i++)
{
printf("\n%c=%d",str[i],str[i]);
}
}

/* Q8 Programme to use of predefined sprintf() Function */


#include<stdio.h>
main()
{
char str[20],s1[20];
int i;
printf("\nEnter your string ");
gets(str);
sprintf(s1,"Output is =%s","hello");
puts(s1);

Anda mungkin juga menyukai