Tags » C Program

C Program to Abbreviate a String

C Program to Abbreviate a String . Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
void string_abbreviation(char [],char []);
main()
{
      char text,target;
      printf("\nProgram to Abbreviate a String \n");
      printf("\n--------------------------------\n\n\n");
      printf("Enter Text : ");                                                   
      gets(text);
      
      string_abbreviation(text,target);
      
      printf("\nAbbreviation of string '%s' is : %s",text,target);
      
      getch();
      
}


          
void string_abbreviation(char a[],char b[])
     {
       char *p;
       int length,i,k=0;
       length=strlen(a);
       
       b[k]=a[0];
       k++;
      
       
       for(i=0;i<length;i++)
       {
            p=&a[i];
            
          
            
            if(*p==' ')
            {
                
                b[k]=*(p+1);  
                k++;         
            }            
           
           
                              
        }  
        
        b[k]='\0';
        strupr(b);
          
     }

Output

WBUT Assignment

C Program to Find first occurrence of char with starting position

C Program to Find first occurrence of char with starting position.Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int First_Occurrence_Char(char [],char ,int);
main()
{
      char text,searchChar;
      int position,start;
      
       printf("\nProgram to Find first occurrence of char with starting position \n");
       printf("\n------------------------------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);
       
       printf("\nSearched Char : ");
       scanf("%c",&searchChar);
       
       printf("\nStart From : ");
       scanf("%d",&start);
       
       
       position=First_Occurrence_Char(text,searchChar,start);
      
       printf("\nCharecter '%c'  in String '%s' starting at %d found at position %d ",searchChar,text,start,position);
       
       
       getch();
       
} 
      
           
int First_Occurrence_Char(char a[],char b,int start)

{
     
     int i;
     char *p;
     
     for(i=start;i<strlen(a);i++) 
     {
         p=&a[i];       
      
        
         
         if(*p==b)
         {
                  
                    break;
                 
         }
               
                              
     }    
     if(i==strlen(a))
     {
       return -1;                
     }
     else
     {
         return i;
     }
     
}

Output

WBUT Assignment

C Program to Find First occurrence of String from a String

C Program to Find First occurrence of String from a String.Compiled in Dev C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int compare_string(char * , char *);
int First_Occurrence_String(char [],char []);
int string_length(char *);
main()
{

      char text,sreachString;
      int position;

      printf("\nProgram to Find First occurrence of String from a String \n");
      printf("\n-----------------------------------------------------------\n\n\n");
      printf("Enter Text : ");
      gets(text);

      printf("\nSearched String : ");
      gets(sreachString);

      position=First_Occurrence_String(text,sreachString);

      printf("\nString '%s' starts in String '%s' from Position %d",sreachString,text,position);

      getch();

      }



int First_Occurrence_String(char a[],char b[])
{
 int i,k;
 char *p,temp;

 for(i=0;i<=(string_length(a)-string_length(b));i++)
 {
    p=&a[i];

    k=0;

    while(k!=string_length(b))
    {
      temp[k]=*(p+k);
      k++;

    }
    temp[k]='\0';

    if(compare_string(temp,b)==1)
    {
     break;
    }

 }
 if(i==(string_length(a)-string_length(b))+1)
 return -1;
 else
 return i;
}     





int compare_string(char *first , char *second)
     {
       while(*first!='\0')
       {
           if(*first==*second)
              {
                  first++;
                  second++;
               }
                else
              {
               return 0;
              }

       }

       return 1;

     }




 int string_length(char *a)
     {
          int count=0;
            while(*a!='\0')
            {
                           count++;
                           a++;
                           }

          return count;

     }     

Output

WBUT Assignment

C Program to Find Last occurrence of char from a String

C Program to Find Last occurrence of char from a String.Compiled in DEV C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int Last_Occurrence_Char(char [],char );
main()
{
      char text,searchChar;
      int position;
      
       printf("\nProgram to Find Last occurrence of char from a String \n");
       printf("\n-----------------------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);
       
       printf("\nSearched Char : ");
       scanf("%c",&searchChar);
       
       position=Last_Occurrence_Char(text,searchChar);
      
       printf("\nCharecter '%c' Found in String '%s' at Position %d",searchChar,text,position);
       
       
       getch();
       
} 
      

     
int Last_Occurrence_Char(char a[],char b)

{
     
     int i;
     char *p;
     
     for(i=strlen(a);i>=0;i--) 
     {
         p=&a[i];       
      
        
         
         if(*p==b)
         {
                  
                    break;
                 
         }
               
                              
     }    
     if(i==strlen(a))
     {
       return -1;                
     }
     else
     {
         return i;
     }
     
}  

Output

WBUT Assignment

C Program to Find first occurrence of char from a String.

C Program to Find first occurrence of char from a String.Compiled in DEV C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int First_Occurrence_Char(char [],char );
main()
{
      char text,searchChar;
      int position;
      
       printf("\nProgram to Find first occurrence of char from a String \n");
       printf("\n-----------------------------------------------------------\n\n\n");
       printf("Enter Text : ");                                                   
       gets(text);
       
       printf("\nSearched Char : ");
       scanf("%c",&searchChar);
       
       position=First_Occurrence_Char(text,searchChar);
      
       printf("\nCharecter '%c' Found in String '%s' at Position %d",searchChar,text,position);
       
       
       getch();
       
} 
      
           
int First_Occurrence_Char(char a[],char b)

{
     
     int i;
     char *p;
     
     for(i=0;i<strlen(a);i++) 
     {
         p=&a[i];       
      
        
         
         if(*p==b)
         {
                  
                    break;
                 
         }
               
                              
     }    
     if(i==strlen(a))
     {
       return -1;                
     }
     else
     {
         return i;
     }
     
}

Output

WBUT Assignment

C Program to Find two Successive Vowels from a String

C Program to FindĀ  two Successive Vowels from a String.Compiled in DEV C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
int FindSuccessiveVowel(char [],char []);
int CheckIfVowel(char );
int string_length(char *);
main()
{
        char a,b;
        int count;
        printf("\n Program to Find two Successive Vowels from a String \n");
        printf("\n-----------------------------------------------------------\n\n\n");
        printf("Enter Text : ");
        gets(a);

        strlwr(a);

        count=FindSuccessiveVowel(a,b);

        printf("\n\nNo of Successive Vowels : %d ",count);

        printf("\n\n\nSuccessive Vowels : %s",b);

        getch();

}

int FindSuccessiveVowel(char source[],char target[])
{
    char *p;
    int i,k=0,count=0;

    for(i=0;i<string_length(source)-1;i++)
    {
       p=&source[i];

       if((CheckIfVowel(*p)==1)&&(CheckIfVowel(*(p+1))==1))
       {
           count++;
           target[k]=*p;
           k++;
           target[k]=*(p+1);
           k++;
           target[k]=' ';
           k++;
       }

     } 

      target[k]='\0';

      return count;

}                           

int CheckIfVowel(char a)
{
    if((a=='a')||(a=='e')||(a=='i')||(a=='o')||(a=='e')||(a=='u'))
          {
              return 1;                                                           

          }

    return 0;

}        

int string_length(char *a)
     {
          int count=0;
            while(*a!='\0')
            {
                  count++;
                   a++;
            }

        return count;

     }

Output

WBUT Assignment

C Program to Find no of Vowels Consonants and Space from a String

C Program to Find no of Vowels , Consonants and Space from a String. Compiled in DEV C++

#include<stdio.h>
#include<string.h>
#include<conio.h>
void Find_Vowels(char *,int *,int *,int *);
main()
{
        char a;
        int vowel,Consonants,Space;
        printf("\n Program to Find no of Vowels , Consonants and Space from a String \n");
        printf("\n-----------------------------------------------------------------------\n\n\n");
        printf("Enter Text : ");                                                   
        gets(a);
        
        strlwr(a);
        
        Find_Vowels(a,&vowel,&Consonants,&Space);
        
        printf("\nNo of Vowels : %d ",vowel);
        
        printf("\nNo of Consonants : %d ",Consonants);
        
        printf("\nNo of Space : %d ",Space);
        
        getch();
      
      
}

 void Find_Vowels(char *a,int *vowels,int *consonants,int *space)
     {
        int length=0;
        int v=0,c=0,s=0;
        
        while(*a!='\0')
        {
          if(*a==' ')
          {
              s++;  
          }
          
          if((*a=='a')||(*a=='e')||(*a=='i')||(*a=='o')||(*a=='e')||(*a=='u'))
          {
              v++;                                                           
                                                                                
          }
            
          length++;
          a++;              
        }    
        
        c=length-(v+s);
        
        *vowels=v;
        *consonants=c;
        *space=s;
        
          
     }

Output

WBUT Assignment