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';
}

printing spaced letters in a string like ABC to A B C

#include<stdio.h>
#include<conio.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 a[30]={0};
    int i;
    header();
    system("pause");
    system("cls");
    printf("Enter String : ");
    scanf("%s",a);
    printf("\n");
    for(i=0;a[i]!='\0';i++)
    {
        printf(" %c ",a[i]);
    }
    printf("\n\n\n\n");
    getch();

}

Distinguish between vowels and consonants in strings

#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]={0};
    printf("Enter String : ");
    scanf("%[^aeiou]",name);
    printf("\n%s",name);
}

Search vowels in a string

#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]={0};
    header();
    system("pause");
    system("cls");
    printf("Enter String : ");
    scanf("%[aeiou]",name);
    printf("\n%s",name);
}

Reversing String

#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[30];
    int i,j;
    header();
    system("pause");
    system("cls");
    printf("Enter String : ");
    gets(name);
    printf("You have Entered : ");
    for(i=0;name[i]!='\0';i++)
    printf("%c",name[i]);
    printf("\n\n\n");
    for(i=0;name[i]!='\0';i++);
    printf("String Length : %d ",i);
    printf("\n\n\n");
    printf("Reversed String is : ");
    for(j=i;j>=0;j--)
    printf("%c",name[j]);
    printf("\n\n\n");
}

gets() and puts() function in String

#include<stdio.h>
int main()
{
    char c1[100];
    puts("Enter String : ");
    gets(c1);
    printf("You Have Entered : %s",c1);

}

Get String and Print string

#include<stdio.h>
int main()
{
  char c1[100],c2[100],c3[100],c4[100];
  puts("Enter Name of 1st City : ");
  gets(c1);
  puts("Enter Name of 2nd City : ");
  gets(c2);
  puts("Enter Name of 3rd City : ");
  gets(c3);
  puts("Enter Name of 4th City : ");
  gets(c4);
  printf("\n\n\n\n\n");
  puts(c1);
  puts(c2);
  puts(c3);
  puts(c4);

}
//Exercise 1: Write a C Program to create the data for some students
//(roll, name, mark1, mark2, mark3, term mark) and then find the total
//marks for each student and average mark of each student.
#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");
}
struct student
{
    int rollno;
    char name[500];
    float mark1;
    float mark2;
    float mark3;
    float term_mark;
    float total;
    float average;
}st;
int main()
{
    header();
    system("pause");
    system("cls");
    printf("Enter Roll No.   : ");
    scanf("%d",&st.rollno);
    fflush(stdin);
    printf("\nEnter Name       : ");
    gets(st.name);
    printf("\nEnter Marks 1    : ");
    scanf("%f",&st.mark1);
    printf("\nEnter Marks 2    : ");
    scanf("%f",&st.mark2);
    printf("\nEnter Marks 3    : ");
    scanf("%f",&st.mark3);
    printf("\nEnter Term Marks : ");
    scanf("%f",&st.term_mark);
    system("cls");
    fflush(stdin);
    printf("Name         : %s ",st.name);
    printf("\nMarks 1      : %.2f",st.mark1);
    printf("\nMarks 2      : %.2f",st.mark2);
    printf("\nMarks 3      : %.2f",st.mark3);
    printf("\nTerm Marks   : %.2f",st.term_mark);
    st.total=0;
    st.total=st.mark1+st.mark2+st.mark3+st.term_mark;
    st.average=0;
    st.average=st.total/4;
    printf("\nAverage      : %.2f",st.average);
    printf("\nTotal        : %.2f ",st.total);
    printf("\n\n\n");
    return 0;
}

Getting and Printing Data using structs and pointers together

#include<stdio.h>
struct student
{
    int rollno;
    char name[100];
    float marks[5];
    float average;
}s[20];
void get(struct student *a)
{
    int i;
    float j=0.0;
    printf("Enter Roll No. of Student : ");
    scanf("%d",&a->rollno);
    printf("Enter Name of Student : ");
    fflush(stdin);
    scanf("%s",&a->name);
    for(i=0;i<5;i++)
    {
       printf("Enter Marks of %d Subject : ",i+1);
       scanf("%f",&a->marks[i]);
    }
    for(i=0;i<5;i++)
    {
       j+=a->marks[i];
    }
    a->average=j/5;

}
void print(struct student *a)
{
    int i;
    printf("\nRoll No : %d",a->rollno);
    printf("\nName    : %s",a->name);
    for(i=0;i<5;i++)
    {
       printf("\nMarks of Subject %d : %.2f ",i+1,a->marks[i]);
    }

    printf("\nAverage : %.2f ",a->average);

}
int main()
{
    int i;
    for(i=0;i<2;i++)
    {
       get(&s[i]);
    }
    for(i=0;i<2;i++)
    {
        if(s[i].average>70)
        print(&s[i]);
    }
}

Getting and Printing Data using structs

#include<stdio.h>
struct account
{
    char name[100];
    int balance;
}ac;
void get(struct account *a)
{
    printf("Enter Name : ");
    fflush(stdin);
    scanf("%s",&a->name);
    printf("\nEnter Account Balance : ");
    scanf("%d",&a->balance);
}
void print(struct account *b)
{
    printf("Name : %s",b->name);
    printf("\nBalance : %d",b->balance);
}
int main()
{
    get(&ac);
    print(&ac);
}

Changes 'a' & 'b' to '-' in file using file handling

#include<stdio.h>

int main()
{
FILE *p1, *p2;
char f1[20], f2[20];
char ch;

printf("Enter File name which has to be copied: ");
gets(f1);
p1=fopen(f1,"r");

if(p1==NULL)
{
    printf("\nUnable to open %s",f1);
    return 1;
}

printf("Enter Destination file");
gets(f2);
p2=fopen(f2,"w");

if(p2==NULL)
{
    printf("Cannot open %s",f2);
    return 1;
}

while((ch=fgetc(p1))!=EOF)
{
    if(' ' || 'a' || 'b')
        ch='-';
    fputc(ch,p2);
}
printf("Completed");
fclose(p1);
fclose(p2);
return 0;
}

Calculate 'A' , 'B' & ' space ' in created file

#include<stdio.h>

int main()
{
FILE *p1,*p2,*p3;
char f1[20], f2[20],f[30];
char ch;
int count1=0,count2=0,count3=0;
printf("Enter File name which has to be copied:  ");
gets(f1);
p1=fopen(f1,"r");

if(p1==NULL)
{
    printf("\nUnable to open %s",f1);
    return 1;
}

printf("\nEnter Name of File in which you want to copy Data :   ");
gets(f2);
p2=fopen(f2,"w");

if(p2==NULL)
{
    printf("Cannot open %s",f2);
    return 1;
}
rewind(p1);
while((ch=fgetc(p1))!=EOF)
{
    if(ch=='A' || ch=='a')
    {
        count1++;
    }

    if(ch=='b' || ch=='B')
    {
        count2++;
    }
    if(ch==' ')
    {
       count3++;
    }
    fputc(ch,p2);
}
printf("a = %d  ,b = %d , space = %d",count1,count2,count3);
printf("\n\nData of File 1 Has been copied to File 2\n\n\n");
fclose(p1);
fclose(p2);
return 0;
}

Creating File & Writing data in file

//Exercise 1: Write a C Program to open a file named “DATA” and write a line of text in it by reading the text from the keyboard.
#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()

{
    FILE *f;
    char a[500];
   header();
   system("pause");
   system("cls");
    f=fopen("DATA.txt" , "w");
    if (f==NULL)
    {
        printf("\nUnable to Open File ....!!");
        return 1;
    }
    printf("\nEnter any Line to Write in File : ");
    gets(a);
    while(!feof(stdin))
    {
        fprintf(f,"%s",a);
        printf("\nEnter any Line to Write in File Ctrl + Z to exit the program : ");
        gets(a);
    }
    fclose(f);
    printf("\nFile Has been created Successfully....!!!\n\n\n\n");

}

Key Search in File Handling

#include<stdio.h>
#include<string.h>
int main()
{
    FILE *f,*e;
    char a[100];
    int roll;
    char key[100];
    f=fopen("abc.dat" , "w");
    if (f==NULL)
    {
        printf("\nUnable to Open File....!!!");
        return 1;
    }
    printf("\nEnter any Text to Write in File : ");
    gets(a);
    fflush(stdin);
    printf("\nEnter Roll No. : ");
    scanf("%d",&roll);
    while(!feof(stdin))
    {
        fprintf(f,"%s %d\n",a,roll);
        printf("\nEnter any Text to Write in File : ");
        fflush(stdin);
        gets(a);
        printf("\nEnter Roll No. : ");
        scanf("%d",&roll);
    }
    fclose(f);
    e=fopen("abc.dat" , "r");
    if (e==NULL)
    {
        printf("\nUnable to Open File....!!!!");
        return 1;
    }
    printf("\n\nEnter key : ");
    gets(key);
    fscanf(e,"%s%d",a,&roll);
    while(!feof(e))
    {
       if (strcmp(key,a)==0)
       {
            printf("%s %d",a,roll);
       }
       fscanf(e,"%s%d",a,&roll);
    }
    fclose(e);
}
//Exercise 2: Write a C Program to read the contents of file ‘File1’ and paste the contents at the beginning of another file ‘File2’ without taking help of any extra file.
#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()
{
    FILE *a,*b;
    char f_1[100],f_2[100],ch;
    header();
    system("pause");
    system("cls");
    a=fopen("source.txt" , "w");
    if(a==NULL)
    {
        printf("\nOooppsss Unable to Open.....!!!");
        return 1;
    }
    printf("Write any text in File or Ctrl+Z to Exit the Program : ");
    gets(f_1);
    while(!feof(stdin))
    {
        fprintf(a,"%s",f_1);
        gets(f_1);
    }
    fclose(a);
    printf("\n\nEnter the name of Source File : ");
    gets(f_1);
    a=fopen(f_1 , "r");
    if (a==NULL)
    {
        printf("\nOooppsss .... Unable to Open the File....!!!");
        return 1;
    }
    printf("\n\nEnter the name of Target File in which you want to copy the Data :  ");
    gets(f_2);
    b=fopen(f_2,"w");
    if(b==NULL)
    {
        printf("\nOoooOOoopsss... Unable to Open the File....!!!");
        return 1;
    }
    while(!feof(a))
    {
        ch=fgetc(a);
        putc(ch,b);
    }
    fclose(a);
    fclose(b);
    printf("\n\n\t\tCongratulations....!!!!");
    printf("\n\n\t\tFile Has Been Copied Successfully...!!!\n\n");

}

Calculating Spaces Lines Words Letters

//Exercise 3: Write a C Program to count the number of characters, words, lines, spaces and tabs in the input supplied from a file.
#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()
{
    FILE *f,*e;
    char a[500],ch;
    int chr=0,sp=0,words=0,lines=0,tabs=0;
    header();
    system("pause");
    system("cls");
     f=fopen("abc.txt" , "w");
    if(f==NULL)
    {
        printf("\nUnable to Open File...!!");
        return 1;
    }
    printf("\nWrite on File or CTRL+Z to exit : ");
    gets(a);
    while(!feof(stdin))
    {
        fprintf(f,"%s\n",a);
        gets(a);
    }

    fclose(f);
    e=fopen("abc.txt" , "r");
    if(e==NULL)
    {
        printf("\nUnable to Open File...!!");
        return 1;
    }
    rewind(e);
    while((ch=fgetc(e))!=EOF)
    {
         if (ch>='A' || ch>='a' && ch<='z' || ch<='Z')
         {
             chr++;
         }
         if(ch==' ')
         {
             sp++;
             words++;
         }
         if(ch=='\t')
         {
             tabs++;
             words++;
         }
         if(ch=='\n')
         {
             lines++;
             words++;
         }
    }
    printf("\nCharacters : %d\nSpaces : %d \nWords : %d\nLines : %d\nTabs : %d ",chr,sp,words,lines,tabs);
}