Friday, April 9, 2010

how to chk if a no. is integer or not.

Source : http://stackoverflow.com/questions/784563/c-check-whether-is-number-is-int-float/784825

#include
using namespace std;
int main(){
 double num1;
 cin>>num1;
 if(static_cast(num1) == num1)//casts num1 to type int
  cout<<"Integer"<
 else
  cout<<"Not Integer"<
  return 0;
  }


Another method is
char *test = "asdf1234";
int i;
for (i = 0; i < strlen (test); i++)
{
   
if (isdigit (test[i]))
        fprintf
(stdout, "test[%d] is a digit!\n", i);
}

print 1-100 no.swithout loop in c

Basically the Question generally is to Write a C function to print all numbers from 1 to m (m is the input no) without any loop specifically for, while, switch, do and recursion
#include
#include
char globalStr[1000000];
void nprint(int n)
{

  char strn[7], *cp;

  sprintf(strn,"%d\n", n);
  cp = strstr(globalStr, strn);
  *(cp + strlen(strn)) = 0;

  printf("%s",globalStr);
}
int main(){
  int n,i;
  globalStr[0] = 0;
  /* Lopp is not allowed only in the function nprint()......still if not convinced, you are free to form the string manually :P*/
  for(i = 1;i<32768;i++)
    sprintf(globalStr,"%s%d\n", globalStr,i);

  scanf("%d",&n);
  nprint(n);
  return 0;
}

For CPP, there is a better method,
#include

class a{
public:
a(){std::cout<<++i<}
private:
static int i;
};

int a::i;

int main(){
int n=0;
std::cout<<"Enter the maximum number that you want to print: ";
std::cin>>n;
a* array=new a[n];
//system("pause");
return 0;
}

print 1-100 no.swithout loop in c

globalStr,i);

  scanf("%d",&n);
  nprint(n);
  return 0;
}

For CPP, there is a better method,
#include

class a{
public:
a(){std::cout<<++i<}
private:
static int i;
};

int a::i;

int main(){
int n=0;
std::cout<<"Enter the maximum number that you want to print: ";
std::cin>>n;
a* array=new a[n];
//system("pause");
return 0;
}

Write a C program which when compiled and run, prints out a message indicating whether the compiler that it is compiled with, allows /* */ comments to nest.

#include
int allowed(){
     int a=1;
     /* /* */ a=0; // */
    return  a;
}

int main(){
   if (allowed())
       printf("Nested comments Allowed\n");
   else
       printf("Nested comments not Allowed\n");
   return 0;
}


In case of nested comments not allowed, we have it as /*/* */ a=0;// */
Bigger font show commented area........


What if the single line comment style is not supported?
After all, single line style comments were introduced with C++...........
#include
int allowed(){
    int i=0, k=1;
    int *j = &k;
    return  /* /* */ i */*j;/* */1;
}

int main(){
   if (allowed())
       printf("Nested comments Allowed\n");
   else
       printf("Nested comments not Allowed\n");
   return 0;
}


So in case of nested comments allowed we have /*/* *\ *\.....
but in case its allowed it is : /*/* *\ *\

Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)

Syntax of strstr 
const char * strstr ( const char * str1, const char * str2 );

 Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
The matching process does not include the terminating null-characters.

Approach1
Append str1 to itself and do a strstr for str2. That should work.
(I am assuming strstr looks for string x within string y).
But this fails sometime...for eg. s1 = ABCDA, s2 = BCDA, s2s2 = BCDABCDA
s2s2 now contains s1, but it is not a rotation of s1.
probably other corner cases exist. 

So we can improve it...by first checking whether s1 and s2 have equal number of characters or not.

Reverse a singly linked listeverse a singly linked list

//Iterative reverse 
 
Void ReverseList(node* head)
{
        node
*temp,*current,*result;
        temp
=null;
        result
=null;
        current
=head;
       
while(current!=null)
       
{
                temp
=current->next;//point to next element
                current
->next=result;//point current element's next to prev element
                result
=current;//point prev element to current element
                current
=temp;
       
}
   head
=result;
 
} 

Thursday, April 8, 2010

Remove duplicates from a sorted linked list

As the linked list is sorted, we can start from the beginning of the list and compare adjacent nodes. When adjacent nodes are the same, remove the second one. There's a tricky case where the node after the next node needs to be noted before the deletion.


// Remove duplicates from a sorted list
void RemoveDuplicates(struct node* head) 
{
  struct node* current = head;
  if (current == NULL) return; // do nothing if the list is empty

  // Compare current node with next node
  while(current->next!=NULL)
  {
      if (current->data == current->next->data)
      {
         struct node* nextNext = current->next->next;
         free(current->next);
         current->next = nextNext;
      }
      else
      {
         current = current->next; // only advance if no deletion
      }
   }
}