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 :)

April 9, 2011 3 comments

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++

July 28, 2010 3 comments

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!

April 21, 2010 Leave a comment

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!

C++ Program to tweet

September 20, 2009 16 comments

It’s very interesting to play with C++ and if it is in GNU/Linux, lots of tools like ‘wget’, ‘grep’,etc. are there to help us in time. Here is a simple C++ snippet which tweets directly to your time line. Feel free to add more features and would be great if you could share it with me too :)

/*
 *      shell-tweet.cpp
 *
 *      Copyright 2009 Ershad K  ershad92@gmail.com
 *      Licensed under GPL Version 3
 *
 *      Please change USERNAME, PASSWORD and compile with g++
 *      To compile - g++ shell-tweet.cpp
 *      To execute - ./a.out
 */

#include <iostream>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main()
{
char name[] = “username”, password[] = “password”, status[500], cmd[500];

char sym[] = {char(34),”};

again: cout << “\n Status: “;
fgets (status,140,stdin);

cout << “\n Length = ” << strlen(status);

if (strlen(status) > 139)
{
cout << ” Exceeds 140 character limit, Type again \n”;
goto again;
}
else
{

strcpy(cmd,”wget -nv –keep-session-cookies –http-user=”);

strcat(cmd,name);
strcat(cmd,” –http-password=”);
strcat(cmd,password);
strcat(cmd,” –post-data=”);
strcat(cmd,sym);
strcat(cmd,”status=”);
strcat(cmd,status);
strcat(cmd,sym);
strcat(cmd,” http://twitter.com:80/statuses/update.xml&#8221;);

system(cmd);

cout << “\n Done”;
}
return 0;
}

Easy Clarity – Connect Easily!

September 5, 2009 2 comments

‘Disconnection’ is a routine process in dial up internet. Here is an C++ snippet which dials BSNL WLL Clarity without asking for password again and again. It automatically closes on disconnection with a beep sound.

/*
 *      easyclarity.cpp
 *
 *      Copyright 2009 Ershad K  ershad92@gmail.com
 *      Licensed under GPL Version 3
 *
 *      Please change USERNAME, PASSWORD and compile with g++
 *      To compile - g++ easyclarity.cpp
 *      To execute - ./a.out
 */

#include <iostream>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

int main ()
{
	char username[] = "USERNAME";
	char password[] = "PASSWORD";
	puts("\n EasyClarity - Connect wisely!  www.ershadk.wordpress.com\n\n");
	ofstream myfile;
	system("sudo wvdialconf");
	myfile.open ("/etc/widial.conf");
        myfile << "[Dialer Defaults]\nInit1 = ATZ\nInit2 = ATQ0 V1 E1 S0=0 "
               << "&C1 &D2 +FCLASS=0\nInit3 = at+crm=1;+cmux=1;+cps=33;+cta=0"
               << "\nModem Type = Analog Modem\nISDN = 0\nPhone = #777\nModem"
               << " = /dev/ttyUSB0\nUsername = "<<username<<"\nPassword = "
               <<password<<"\nBaud = 460800\nStupid Mode = 1\nAuto DNS\nCheck"
                <<" Def Route";
	myfile.close();
	system("sudo wvdial");
	cout << "\a";
	return 0;
}

Checknet V1.0 Alpha (GNU/Linux only)

June 8, 2009 3 comments

Checknet is an application developed for slow Internet connection(especially Dial-up) users who likes to turn off monitor while downloading some files from the web. Since the monitor is switched off or else if you are working with some other application, you wouldn’t know whether the download is interrupted by connection erros. This application gives you beep-beep alerts when the connection gets dissconnected or the download is interrupted. You even need not turn your speakers ON for alerts, the alerts are from CPU :) .

This programs is working based on the amount of received-data. It wil update the record every 2 seconds, if there is no change in the total recieved data, it just starts giving you alerts.

Checknet was initially created just for my personal use, hence it doesn’t feature a good interface or other extra elements. Feel free to edit it to suit your style.

Thanks to Ragsagar ettan and Sarath ettan :)

Click here for the executable fileClick here for the source code

Follow

Get every new post delivered to your Inbox.