Sunday 23 November 2014

How to concatenate two strings?

The following function will concatenate two strings and return the concatenated strings:

char* strcat(char* str1,char* str2)
{
             int len=0,i=0;
             while(str1[len])     //until a null character founds
                       ++len;

              while(str2[i])         //until a null character founds
              {
                          str1[len]=str2[i];
                          ++i;
                          ++len;
               }
             
               str1[len]='\0';       //terminate the string
               return str1;
}
                 
for e.g.:

strcat("hello","hi");

will return :

hellohi
             

How to add file security in C#?

To add file security to any file following function can be used:
Firstly add the name space:
using System.IO;
using System.Security.AccessControl;

public static void AddFileSecurity(string fileName, string account,FileSystemRights rights, AccessControlType controlType)
        {
             // Get a FileSecurity object that represents the current security settings.
             FileSecurity fSecurity = File.GetAccessControl(fileName);

            // Add the FileSystemAccessRule to the security settings.
            fSecurity.AddAccessRule(new FileSystemAccessRule(account,rights, controlType));

            // Set the new access settings.
            File.SetAccessControl(fileName, fSecurity);

        }

Now you can use this function to add file security to any file or directory. Pass the name of file or directory as fileName. Account can be your current account or any other account. The argument rights can be any valid FileSystemRights (for e.g.: FullControl, Read, Write, Delete, Modify). The argument controlType can be any valid AccessControlType(Allow or Deny).

for e.g.:

AddFileSecurity("D:\music","Everyone",FileSystemRights.Delete,AccessControlType.Deny);

Now above file security added to D:\music ensures that no one can delete the folder. As it means 'Deny' 'Everyone' to 'Delete'

Now you can also deny current user to deny delete on this folder. For this just pass name of current user as the argument account.
To know how to get name of current user click the link below:
http://gsbprogramming.blogspot.in/2014/11/how-to-get-current-application-path.html

How to get current application path, current user's name and current application name in c#?

To get current application path:
MessageBox.Show(Application.ExecutablePath);

To get current user's name:
MessageBox.Show(System.Security.Principal.WindowsIdentity.GetCurrent ().Name);

To get current application name:
MessageBox.Show(Application.ProductName);

Thursday 20 November 2014

A function to calculate length of string

The following function will calculate the length of a given string
int strlength(char *s)
{
      int len=0;
      while(s[len])  //repeat until a null character is found
            ++len;
      return len;
}

How to sort digits of a number in C++?

To sort digits of a number in C++ the code is as follows:

//Program to sort digits of a number in C
#include<iostream.h>
using namespace std;
int main()
{
int n,temp,digit;
cout<<"Enter the number: ";
cin>>n;
for(digit=0;digit<=9;++digit)   //traverse the digits from 0 to 9
      for(temp=n;temp>0;temp/=10)  //temp/=10 means temp=temp/10
                  if(temp%10 = = digit)
                        cout<<digit;
return 0;
}

How to sort digits of a number in C?

To sort digits of a number in C the code is as follows:

//Program to sort digits of a number in C
#include<stdio.h>
int main()
{
int n,temp,digit;
printf("Enter the number: ");
scanf("%d",&n);
for(digit=0;digit<=9;++digit)   //traverse the digits from 0 to 9
      for(temp=n;temp>0;temp/=10)  //temp/=10 means temp=temp/10
                  if(temp%10 = = digit)
                        printf("%d",digit);
return 0;
}

Wednesday 19 November 2014

What is difference between C & C++?



Following are the differences between C & C++:

                              C                              C++
1. C is Procedural Language.1. C++ is non Procedural i.e Object oriented Language.
2. No virtual Functions are present in C2. The concept of virtual Functions are used in C++.
3. In C, Polymorphism is not possible.3. The concept of polymorphism is used in C++.
Polymorphism is the most Important Feature of OOPS.
4. Operator overloading is not possible in C.4. Operator overloading is one of the greatest Feature of C++.
5. Top down approach is used in Program Design.5. Bottom up approach adopted in Program Design.
6. No namespace Feature is present in C Language. 6. Namespace Feature is present in C++ for avoiding Name collision.
7. Multiple Declaration of global variables are allowed.7. Multiple Declaration of global variables are not allowed.
8. In C
  • scanf() Function used for Input.
  • printf() Function used for output.
8. In C++
  • Cin>> Function used for Input.
  • Cout<< Function used for output.
9. Mapping between Data and Function is difficult and complicated.9. Mapping between Data and Function can be used using "Objects"
10. In C, we can call main() Function through other Functions 10. In C++, we cannot call main() Function through other functions.
11. C requires all the variables to be defined at the starting of a scope.11. C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.
12. No inheritance is possible in C.12. Inheritance is possible in C++
13. In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating.13.In C++,  new and delete operators are used for Memory Allocating and Deallocating.
14. It supports built-in and primitive data types.14. It support both built-in and user define data types.
15. In C, Exception Handling is not present.15. In C++, Exception Handling is done with Try and Catch block.
16. ANSI C recognizes only the first 32  characters in identifier names.16. ANSI C++ places no limits on its length.
17.In ANSI C, we can assign a void pointer to a non-void pointer without using a cast to non-void pointer type. For example:

void *ptr1;
char *ptr2;
ptr2=ptr1;
17. In ANSI C++ we must use cast operator as shown below:

ptr2=(char *) ptr1;
18. ANSI C does not require an initializer; if none is given, it initializes the const to 018. C++ requires a const to be initialized.
19. In C character constants are stored as int, therefore,
sizeof('a')
is equivalent to
sizeof(int)

19. In C++ char is not promoted to the size of int, and therefore,
sizeof('a')
is equivalent to
sizeof(char)
20. In C, the global version of a variable cannot be accessed from within the inner block. 20. In C++, this problem is resolved by the introduction of a new operator : : called the scope resolution operator.
21. C notation for type casting is:

(type) expression
21. C++ notation for type casting is:

type (expression)
22. In C struct keyword is necessary while creating a object of structure. For example:

struct student a;
22. In C++ struct keyword is not necessary while creating a object of structure. For example:

student a;