Monday 4 November 2013

Convert String from upper case to lower case

#include<stdio.h>
#include<string.h>
void main()
{
  char a[20];
  int x;
  printf("enter the string:");
  scanf("%s",a);
  for(x=0;x<=strlen(a);x++)
  {
       if(a[x]>=65&&a[x]<=90)
       a[x]=a[x]+32;
  }
  printf("%s",str);
}

Convert String from lower case to upper case

#include<stdio.h>
#include<string.h>
void main()
{
  char a[20];
  int x;
  printf("enter the string:");
  scanf("%s",a);
  for(x=0;x<=strlen(a);x++)
    {
            if(a[x]>=97&&a[x]<=122)
            a[x]=a[x]-32;
    }
  printf("%s",str);
}

Saturday 26 October 2013

Reverse of a String

#include<stdio.h>
void main()
{
    char a[20];
    char b[20];
    int i=-1,j=0;// i =-1 because index starts from zero.
    printf("enter the string :- ");
   gets(a);  
    while(a[++i]!='\0'); // i denotes the last character of the string through this loop
    while(i>=0)
     b[j++] = a[--i];//copy last character of "a" char array string as the first character into the "b" char array. 
    b[j]='\0'; //insert null to terminate the string
    printf("Reverse  string is : %s",b);

}

Frequency of the character in a String

frequency of a character means that how many times the character occurs in a string.
Ex:- Ctutorials
frequency of C = 1
frequency of t =2
frequency of o , a , l , s , u =1
frequency of t is 2 because only t comes 2 times


#include<stdio.h>
void main()
{
 char a[20],character; 
int i,freq=0; 
printf("enter the string:- ");
 gets(a); 
printf("enter the character:- ");
 scanf("%c",&character); 
for(i=0;a[i]!='\0';++i) 
{
 if(character==a[i])
 ++freq; 
}
 printf("Frequency of %c = %d", character, freq); 
}

Copy String

#include <stdio.h>
void main()
 { 
  char a[20], b[20], i;
  printf("enter the string:- "); 
  gets(a);
  for(i=0; a[i]!='\0'; ++i) 
  b[i]=a[i];
  b[i]='\0';
  printf("String : %s",b); 
  } 

simply it copy one string to another char array..