Archive

Posts Tagged ‘C++’

Anagram Solver in C

September 18, 2011 Leave a comment

Hi, Here’s a small anagram solver written in C. It works based on generating permutations of given string and comparing them using a dictionary. For fast search in dictionary, the program first creates an index of the starting location of each alphabet. It takes a few minutes to process long words.

Here’s the source code. Suggestions are always welcome, Greetings πŸ™‚

Update: There’s a little problem in using the term ‘anagram solver’ here. An anagram solver finds words from another valid word – But this program finds words from the characters of given valid/invalid word.Β  Thanks to technikhil for the info πŸ™‚

C program to extract email IDs from a file

Here’s a small program to extract email IDs from an input file. It could have done using regex (in Python orΒ  in any other high level language), but doing it in C is more fun, eh? πŸ™‚

The code is here!

Usage: parseEmail [FILE]

Suggestions and improvements are always welcome πŸ™‚ Thank you!

A sudoku checking program in C :)

It just started when my younger bro, Shafeeq who reads hacker news everyday, found a little interesting blog post. It was about the authors experience in applying for an internship at Google. The candidate was asked to write a program to check a completed sudoku during the telephone interviewΒ  for errors, in his favorite programming language. I asked bro, “Why don’t you give it a try?!”. He’s now in 11th standard and started coding in C++ which he learns in Computer Science subject.Β  He came up with the code in one day, and then it was my turn to do that πŸ™‚ Here’s the way I solved the problem and it’s in pure C.

You can find the code here, in github. The input is given as a plain text file that contains 9×9 numbers separated by space. A sample input is given below:

7 2 3 8 4 6 1 5 9
6 1 5 3 9 2 4 7 8
8 4 9 7 1 5 6 3 2
3 7 8 6 5 4 9 2 1
1 9 4 2 8 7 3 6 5
2 5 6 9 3 1 8 4 7
5 6 1 4 7 9 2 8 3
4 8 7 1 2 3 5 9 6
9 3 2 5 6 8 7 1 4

And to check for errors, please call the program with the file as argument, like this –

$checksudoku <data_file>

Hope you enjoyed it, Please go through the code and feel free to post your valuable comments and suggestions here. Thank you πŸ™‚

A simple square pattern in C++

Here’s a small C++ program which outputs a square pattern according to the size entered. Code is given below :


/*
* sqpattern.cpp
*
* Copyright 2010 Ershad K ershad92@gmail.com
* Licensed under GPL Version 3
*
* To compile - g++ -o sqpattern sqpattern.cpp
* To execute - ./sqpattern
*/

#include <iostream>
using namespace std;

int main()
{
  int size;
  cout << "Enter size: ";
  cin >> size;

  int count = 1;
  cout << " ";
  while (count++ < size) cout << " _";

  for (int i = 1; i < size; i++)
    {
      int numOfCharsPerLine = 0;
      cout << "\n";
      for (int j = 0; j < i; j++)
        {
          cout << " |";
          numOfCharsPerLine += 2;
        }

      cout << "_";
      numOfCharsPerLine += 1;

      int k = 1;
      while (k++ < (size*2) - numOfCharsPerLine )
      cout << " ";
      cout << "|";
    }

  count = 1;
  cout << "\n ";
  while (count++ <= (size)-1 ) cout << "|_";

  cout << "|\n";

return 0;
}

Output:

The programs drawsΒ  squares of any size entered. The code needs improvement, please feel free to give suggestions. Click here for ‘experiments’ git repo. Thank you πŸ™‚

Categories: C++ Tags: , ,

Configure Gmail in Mutt easily!

It’s always wonderful to play with terminal. Last week, I tried to configure my Gmail account in mutt, a powerful console based mail client, using this tutorial. I had to create some configuration files for various related applications and executed some commands. Hence I thought it would be nice if there is any script/program to configure mutt easily by simply entering the Gmail ID and password. Here is such an application, written in C++, which will help you to configure mutt very easily. All that you need to do is install mutt and follow these steps :

1. Download this file
2. Exctract it to your home directory
3. Execute the following commands :

$cd muttgconf
$./muttgconf

VERY IMPORTANT: THE PROGRAM CREATES / MODIFIES MANY CONFIGURATION FILES. I WILL NOT BE RESPONSIBLE FOR ANY DAMAGE OR LOSS CAUSED BY THIS APPLICATION. USE IT UNDER YOUR RISK.

Get its source from git://github.com/ershad/Mutt-Gmail-Conf.git Β Β  Good luck πŸ™‚

Salt Analysis Simulator V1.0 – Released!

January 16, 2010 7 comments

For the past few weeks, I have been coding to bring out a program to do salt analysis lab work virtually. We have salt Analysis as a part of plus two academics and it is a process of finding the anion and cation which the salt contain through a systematic procedure. We add many reagents / acids throughout the labwork and analyse the result to identify the anion and cation. I hope this program would help students to practice salt analysis at their home and would feel confident!

The program is completely written in C++ and is intended to work on M$ DO$ platform, becuase more students are using M$ Platform including Biology-science group students. Another techincal reason is that, I have used clrscr(), gotoxy() and getchar() many times which is a troublesome work to implement in ANSI standard.

The program works based on a new algorithm which I name it as “IntMatchReact” since the reaction process is done through a matching of integer values. While developing this software, I had a notion to implement anti-piracy for fun which was left on the way. The software is licensed under GPL and I would like you to go through the code and help in improving.

Please post your valuable comments and improvement tips. Thank you πŸ™‚

Get SAS V1.0 (here)

Bug: i forgot to add exit function for ‘e’ key stroke, have exams now – will upload SAS with minor corrections soon πŸ™‚

Categories: C++ Tags: , , , ,

The pleasure of solving!

December 14, 2009 7 comments

It’s been a quite long since I blogged something πŸ™‚ May be a writer’s block* (*acronym for pure laziness,here);) Well, Last Saturday, I went to dxmk9 program conducted by NSS collage of engineering at Kottamaidhanam, Palakkad. I had registered my name for ‘Geek’s Paradise’ competition which is basically a C programming contest. The first task was to answer some objective questions, mainly to predict the output, was not much tough. The second task was to print a number pattern like this:

1
1 2 1
1 2 3 2 1
1 2 3 4 3 2 1
1 2 3 2 1
1 2 1
1

whooo! the problem was not easy for a newbie like me (‘m still in school,sir πŸ˜‰ ), I simply wrote some printf() statements with some ++i,etc. and escaped! Also, I had no much time to think, I had promised to return home before 1 PM.

When I reached home, a sudden thought flashed across my mind “Why shouldn’t I give a try ?”. I booted my ubuntu system ( ‘m eagerly waiting to get Fedora in hands), took newly installed Netbeans IDE and started trying. After some time –Β  LOL πŸ™‚ !! It worked!! The program asked for an integer input and printed the pattern exactly the way it’s meant to be! I know, this is a small problem and has nothing of great speciality or something, but don’t know why – The output gave me some moments of rejoice! A small pleasure of coding πŸ™‚ Ha ha, I would like to share that code with you πŸ™‚

/*
 *      121.c
 *
 *      Copyright 2009 Ershad K <ershad92@gmail.com>
 *
 *      Licensed under GPL Version 3
 */

#include <stdio.h>

main() {

    int n;
    int i = 1;
    int j = 1;
    printf("Enter a number: ");
    scanf("%d",&n);
    
    // Increment....
    for (;i <= n; i++ )
    {
        for(j = 1; j <= i; j++)
        {
            printf(" %d",j);
        }
        --j;

        for (--j; j >=1; j--)
        {
            printf(" %d",j);
        }
        printf("\n");
    }

    // Decrement....

     for (i = n-1;i >= 1; i-- )
    {
        for(j = 1; j <= i; j++)
        {
            printf(" %d",j);
        }
        --j;

        for (--j; j >=1; j--)
        {
            printf(" %d",j);
        }
        printf("\n");
    }
    
}

Patches are always welcome :) All the best, nice day!