Simple C code for strings.

I am currently teaching a class right now (first time).  I am working on teaching students how to program with if statements and while statments.  Here is the thing.  I want to assign them a program that has them check a string of char to see outprint to the screen.  I want them to outPut the string skipping spaces and punctuation.  They have not learned pointers yet so I want to do this without using them.  Are there any teachers or experts out there that have a sample code to use.  

Thanks in advance.

Corey


LVL 2
wiggs1006Asked:
Who is Participating?
 
griesshConnect With a Mentor Commented:
#include <stdio.h>

main()
{

    char line[] = {"This is a line of text!"};
    int i = 0;

    while (line [i] != '\0')
    {
        if ((line[i]>='a' && line[i]<='z') ||
            (line[i]>='A' && line[i]<='Z') ||
            (line[i]>='0' && line[i]<='9'))
        {
            printf ("%c", line[i]);
        }
        i++;
    }
    printf ("\n");

}
0
 
jkrCommented:
If you indeed want to teach students, you might want to chek out

http://computer.howstuffworks.com/c35.htm

and

http://computer.howstuffworks.com/c22.htm
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi wiggs1006,

My first computer instructor was named Corey, though it was "several" years back.  :)

I think that I'd start by having your students do something similar to what griessh has suggested, and incrementally add functionality.

1)  Write a simple program that will print all of the alphabetic characters in a string, without using the definitions in ctype.h or string.h.
2)  Modify the program to also print digits and punctuation.
3)  Modify it again to accept both the source string and the exception characters from the command line.
4)  Modify it one more time to allow the usage of the definitions in ctype.h and string.h


Kent
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
MysidiaCommented:
#include <string.h>

/**
 * Precondition:  letter is initialized to an ASCII value ranging from 0 to 127
 *        Returns:  1 if the value associated with letter corresponds to an uppercase letter
 *                         0 if the value corresponds to anything else
 */
int is_uppercase(char letter) {
        char uppers[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        if (strchr(uppers, letter)) {
            return 1;
        }
        else {
            return 0;
        }
}

/**
 * Precondition:  buffer is initialized to a string, which is a \0 terminated character array
 *                         of length 'start' or greater.
 *
 *        Returns:   the array index of the first uppercase letter in the buffer
 *                          or -1 if it is not there
 */
int first_uppercase(char buffer[], int start) {
        int i;

        for(i = start; buffer[i] != '\0'; i++) {
              if  (is_uppercase(buffer[i]))
                   return i;
        }
         return -1;
}

int main()
{
      char text[] = "Wall Ox Random Door";
      int position = 0;

      position = first_uppercase(text,0);

      while ( position != -1 ) {
                 printf("%c", text[position]);
                 position = first_uppercase(text, position+1);
      }
      printf("\n");
}
0
 
kledbetterCommented:

char *stringToPrint = "This is the string to print to the screen.";

int main(int argc, char *argv[])
{
     for (int x = 0; stringToPrint [x] != '\0'; ++x)
    {
        if ( (! ispunct(stringToPrint [x]) &&
             (! isspace(stringToPrint [x]))
            putchar (stringToPrint [x]);
     }
      return 0;
0
 
wiggs1006Author Commented:
I want to thank you all so much for the help.  I am looking forward to teaching and I would have given you all points if I could.  I want to programming enjoyable for students, I do not want them to go through some of the horrors that I had to when I was in college, it is funny how I forget to think on their level, although it was not all that long ago that I was there.  I again thank you all with helping me to simplfy this problem.  I am sure that as time passes I will be more comfortable in my teaching and confident that the students are enjoying themselves as well as learning.  

Take care all,

Corey
0
 
amar_rCommented:
#include <stdio.h>
                                                                                                                             
main()
{
                                                                                                                             
    char line[] = "This is a line ;";
    int i = 0;
                                                                                                                             
    while (line [i] != '\0')
    {
        if ((line[i]>=65 && line[i]<=90) ||(line[i]>=48 && line[i]<=57) ||(line[i]>=97 && line[i]<=122))
        {
            printf ("%c", line[i]);
        }
        i++;
    }
    printf ("\n");
                                                                                                                             
                                                                                                                             
                                                                                                                             
                                                                                                                             
 return 0;
 }
                                                                                                                             
0
All Courses

From novice to tech pro — start learning today.