Friday, November 21, 2014

C character using library function



Check Whether Entered Character is Uppercase Letter or Not without using Library Function
#include
#include 
int main()
{
   char ch;
   printf("\nEnter The Character : ");
   scanf("%c", &ch);
   if (ch >= 'A' && ch <= 'Z')
      printf("Character is Upper Case Letters");
   else
      printf("Character is Not Upper Case Letters");
    return (0);
}

Output
Enter The Character : A
Character is Uppercase Letters

Reverse String without Using Library Function


#include
#include
int main()
{
char str[100],temp;
int I,j=0;
printf("\n Enter the string: "”);
gets(str);
i=0;
j=strlen(str)-01;
while(i
{
str[i]str[i];
str[j]=temp;
i++;
j--;
}
printf("\n Reverse sting is %s",str);
return (0);
}

Output


Enter the string : Pritesh
Reverse string is : Hsetirp

With using User-defined Function Write a Program to Find Length of String


#include
#include 
int FindLength(char str[]);
int main()
{
   char str[100];
   int length;
   printf("\nEnter the String : ");
   gets(str);
   length = FindLength(str);
   printf("\nLength of the String is : %d", length);
   return(0);
}
int FindLength(char str[])
{
   int len = 0;
   while (str[len] != '\0')
      len++;
   return (len);
}

Find Length of String Using Library Function


#include
#include
int main()
{
   char str[100];
   int len;
   printf("\nEnter the String : ");
   gets(str);
   len = strlen(str);
   printf("\nLength of Given String : %d", len);
   return(0);
}

Search occurrence of Character in String

#include
#include
int main()
{
   char str[20], ch;
   int count = 0, i;
   printf("\nEnter a string : ");
   scanf("%s", &str);
   printf("\nEnter the character to be searched : ");
   scanf("%c", &ch);
   for (i = 0; str[i] != '\0'; i++)
  {
      if (str[i] == ch)
         count++;
   }
    if (count == 0)
      printf("\nCharacter '%c'is not present", ch);
   else
      printf("\nOccurence of character '%c' : %d", ch, count);

   return (0);
}

Output

First Run :
Enter a string : c4learn.blogspot.com
Enter the character to be searched : o
Occurence of character 'o' : 3

Second Run :
Enter a string : c4learn.blogspot.com
Enter the character to be searched : x
Character 'x'is not present

No comments:

Post a Comment