Showing posts with label C Programming. Show all posts
Showing posts with label C Programming. Show all posts

Friday, June 20, 2014

Get Abbreviation

Get abbreviation of a given name entered by user in C Programming language by Project Free TV . 

#include<stdio.h>
void header()
{
    printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
    printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t  Lahore , Pakistan\n\n\n");
}
int main()
{
    char name[100];
    int i=0;
    header();
    system("pause");
    system("cls");
    printf("Enter Name : ");
    gets(name);
    system("cls");
    printf("You have entered %s ",name);
    printf("\n\n\n\t\tAbbreviation");
    printf("\n\n\t\t     %c",name[i]);
    for(i=0;name[i]!='\0';i++)
    {
        if (name[i]==' ')
        {
            i++;
            printf("%c",name[i]);

        }
    }
printf("\n\n\n\n");
}

Strings Swapping using pointers , malloc() & strcpy()

We know that to swap two numbers in c programming language is much easy but can we swap two strings? Of course you can perform swapping on strings as well. The following program swap two strings using pointers, malloc() and strcpy().

#include<stdio.h>
#include<string.h>
#include<malloc.h>
void header()
{
    printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
    printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t  Lahore , Pakistan\n\n\n");
}
int main()
{
    char s1[100],s2[100];
    char *hold;
    header();
    system("pause");
    system("cls");
while (!feof(stdin))
{
    printf("Enter String 1 : ");
    gets(s1);
    printf("\nEnter String 2 : ");
    gets(s2);
    hold=malloc(0); //is used to provide memory for pointer *hold
    strcpy(hold,s1);
    strcpy(s1,s2);
    strcpy(s2,hold);
    printf("\n\n After Swapping \n\n ");
    printf(" String 1 : %s  ; String 2 : %s \n\n\n\n",s1,s2);
}
}

Functions to calculate string length using arrays

There are many programs in which we need to calculate the length of a string. To calculate string length we can use built in function in C Programming to calculate length but we should know what is going on in back end of this function or what is the structure of this function? The following code defines the structure of function used to calculate the length of a string.

#include<stdio.h>
int strln(char a[])
{
   int i;
   for(i=0;a[i]!='\0';i++);
   return i;

}
int main()
{
   int a[100000];
   puts("Enter String : ");
   gets(a);
   system("pause");
   system("cls");
   printf("\n\n\nString Length is %d \n\n\n\n\n",strln(a));
}

Function to copy one string into another using pointers

To copy one string to another string, we have built-in function defined in <string.h> library but to be a good programmer, we should have knowledge about the algorithm of function and the story of back end code. The following code is the implementation of function to copy one string into another using pointers in C Programming Language.


#include<stdio.h>
void strcp(char *a , char *b)
{
    int i;
    for(i=0;*b!='\0';i++)
    {
        *a=*b;
         a++;
         b++;

    }
}
void header()
{
    printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
    printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t  Lahore , Pakistan\n\n\n");
}
int main()
{
    char a[100];
    char b[100];
    header();
    system("pause");
    system("cls");
    puts("Enter String 1: ");
    gets(a);
    puts("Enter String 2 : ");
    gets(b);
    strcp(a,b);
    printf("\nAfter Copying String 2 in String 1 \n\n");
    printf("String 1 is : %s \n\n\n\n\n",a);
}

String Comparison using pointers , functions , loops and if selection statements

You can comparison two strings in C Programming language by using built-in function defined in <String.h>. The back end code working behind the function in <String.h> library to comparison two strings works as follows:

#include<stdio.h>
int stcp(char *a , char *b)
{
    int i;
    while(*a==*b)
    {
        if (*a=='\0' || *b=='\0')
        break;
        a++;
        b++;

    }
    if (*a=='\0' && *b=='\0')
        return 0;
    else return -1;
}
int main()
{
    char a[20],b[20];
    printf("Enter String 1 : ");
    scanf("%s",a);
    printf("Enter String 2 : ");
    scanf("%s",b);
    if(stcp(a,b)==0)
    {
        printf("String 1 = String 2");
    }
    else if (stcp(a,b)==-1)
    {
        printf("String 1 is not matching string 2");
    }
}

String Addition using pointers, functions and loop


String concatenation is a way to add two strings. For example if we have two strings "My" and "Name" then to produce these two strings as one string that is "My Name", we will concatenate these two strings. We can do concatenation in C Programming Language by using built-in string concatenation function defined in <String.h> library but we can also make our own function to concatenate two strings. The following code do concatenation of two strings using pointers, functions and loops.

#include <stdio.h>
#include<conio.h>

void concatenate_string(char*, char*);

int main()
{
    char original[100], add[100];

    printf("Enter source string\n");
    gets(original);

    printf("Enter string to concatenate\n");
    gets(add);

    concatenate_string(original, add);

    printf("String after concatenation is \"%s\"\n", original);
 getch();
    return 0;
}

void concatenate_string(char *original, char *add)
{
   while(*original)
      original++;

   while(*add)
   {
      *original = *add;
      add++;
      original++;
   }
   *original = '\0';
}

Saturday, May 31, 2014

Write a program to declare an integer array of 50 elements. A. Write a function getArray() to get array input from the user that will be used to initialize the first thirty five (35) elements of the array by getting input from the user. The rest of the 15 entries would be set to zero (0). B. Write a function FindEven() to find the total numbers of even numbers in the given array. C. Write a function ModifyArray() to make the each array element to a multiple of four(04). D. Write a function named ‘FindMin()’ that will find the smallest element in the given array and return the smallest element.

Write a program to declare an integer array of 50 elements.  
A. Write a function getArray() to get array input from the user  that will be used to initialize the first thirty  five (35) elements of the array by getting input from the user. The rest of the 15 entries would be set to zero (0).  
B. Write a function FindEven() to find the total numbers of even numbers in the given array.
C. Write a function ModifyArray() to make the each array element to a multiple of four(04).
D. Write a function named ‘FindMin()’ that will find the smallest element in the given array and return the smallest element.

#include<stdio.h>
void header()
{
    printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
    printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t  Lahore , Pakistan\n\n\n");
}
void getArray(int a[])
{
    int i;
    for(i=0;i<35;i++)
    {
        printf("Enter %d Number : ",i+1);
        scanf("%d",&a[i]);
        printf("\n");
    }
    printf("\nYou Array's Elements are");
    for(i=0;i<50;i++)
    printf(" %d ",a[i]);
}
void even(int a[])
{
    int i;
    for (i=0;i<35;i++)
    {
        if (a[i]%2==0)
        printf("%d is Even\n",a[i]);
    }
}
int multiply(int a[])
{
    int i;
    for(i=0;i<35;i++)
    {
        printf("%d X 4 is %d\n",a[i],a[i]*4);
    }
}
int FindMin(int a[])
{
    int i,j,hold;
    for(j=0;j<35;j++)
    {
         for(i=0;i<35-1;i++)
         {
              if (a[i]>a[i+1])
              {
                  hold=a[i];
                  a[i]=a[i+1];
                  a[i+1]=a[i];
              }
         }
    }
        printf(" Minimum Number You have Entered is  %d ",a[0]);
        printf("\n\n\n\n\n");
}
int main()
{
    int a[50]={0},i;
    header();
    system("pause");
    system("cls");
    getArray(a);
    system("pause");
    system("cls");
    even(a);
    system("pause");
    system("cls");
    multiply(a);
    system("pause");
    system("cls");
    FindMin(a);
}