User Tools

Site Tools


user:kkrauss1:portfolio:elementcounter

Project: Element Counter

A project for C/C++, Data Structures, and System Programming by Karl Krauss for fall 2011.

Approximately 3 hours including taking the time to full grasp all concepts that were used.

Objectives

The purpose of this project is to count the elements in a string.

Prerequisites

In order to successfully accomplish/perform this project, the listed resources/experiences need to be consulted/achieved:

  • *argv[]
  • argc
  • interation
  • prototyping

Background

This was something I started for Joe to count elements in a string.

Scope

Counting the characters in a string is a fairly simple task. This project was designed to allow for user interaction. It allows the user to enter a string or several strings on the command line, or a single string if the user choses to run the program without any initial string(s). If multiple strings are entered on the command line each string will have its characters counted and displayed. Otherwise the single string entered after the program starts will be counted and displayed.

Attributes

Cprog attributes

  • variables
  • pointers
  • selection
  • i/o
  • repetition
  • functions
  • arrays

Data Structures

  • Pointers
  • Malloc/new

Systems Programming

  • Command line argument

Procedure

The actual steps taken to accomplish the project. Include images, code snippets, command-line excerpts; whatever is useful for intuitively communicating important information for accomplishing the project.

Code

#include <stdio.h>
 
int counter(char *str);
 
int main(int argc, char *argv[])
{
        char *str;
        int characterCount;
        if (argc == 1) 
        {
                printf("Please enter string: ");
                scanf("%s", str);
                characterCount = counter(str);
                printf("\n%d characters\n", characterCount);
        }
        else 
        {
                // This is for counting multiple 
                for (int x=1; x < argc; x++)
                {
                        ++argv;
                        characterCount = counter(*argv);
                        printf("\n%d characters\n", characterCount);
                }
        }
 
 
        return 0;
}
 
int counter(char *str)
{
 
        int elementCounter = 0;
 
        while (*str++ != '\0')
        {
                elementCounter++;
        }
 
        return elementCounter;
}

Execution

Again, if there is associated code with the project, and you haven't already indicated how to run it, provide a sample run of your code:

lab46:~/public_html/discrete$ ./a.out karl john matt

4 characters

4 characters

4 characters
lab46:~/public_html/discrete$ ./a.out 
Please enter string: karl

4 characters
lab46:~/public_html/discrete$ 

Reflection

Comments/thoughts generated through performing the project, observations made, analysis rendered, conclusions wrought. What did you learn from doing this project?

This project will look very simple to more advanced users, but it was quite enjoyable and a good learning experience. *arg[] helped me understand pointers even better and how to allow for command line arguments. Understanding these things are an important basis for any good programming.

References

In performing this project, the following resources were referenced:

  • URL1
  • URL2
  • URL3 (provides useful information on topic)
  • URL4

Generally, state where you got informative and useful information to help you accomplish this project when you originally worked on it (from Google, other wiki documents on the Lab46 wiki, etc.)

user/kkrauss1/portfolio/elementcounter.txt · Last modified: 2011/12/12 14:07 by kkrauss1