Parse String and Display it Alphabetically

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • laurie1388
    New Member
    • Feb 2008
    • 3

    Parse String and Display it Alphabetically

    I'm in an Intro to Software Development course at FIT and I have a homework assignment that I have no idea what to do about. Can anyone help? Do I need to use ASCII? If so, how?

    Here's the assignment:

    Write a program to accept a string as input from the user.
    The program should verify that the input string contains nothing but alphabets.
    If the string is not valid, the program should ask the user to input it again.
    The program should automatically sort the characters in the string in ascending order (A-Za-z), and display it.


    Thanks in advance!

    -Lauren
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    Okay, so breaking down your requirements you need a few things:

    1) a string input by the user

    2) parsing functionality to break the string into individual characters

    3) arrangement of the characters into alphabetical order

    4) displaying the characters

    Does that sound like a complete list to you? If so, how many of those do you know how to do? Which seem like the easiest?

    Comment

    • laurie1388
      New Member
      • Feb 2008
      • 3

      #3
      This is what I've got so far (minus the stuff that goes at the end to end the program):


      [CODE=cpp]#include <iostream>
      #include <string>
      using namespace std;
      int main ()

      {

      string s;

      cout <<"Please enter some text.";
      cin >>s;[/CODE]
      Last edited by Ganon11; Feb 8 '08, 03:16 AM. Reason: Please use the [CODE] tags provided.

      Comment

      • Ganon11
        Recognized Expert Specialist
        • Oct 2006
        • 3651

        #4
        OK, so you've got the string. Now how are you going to make sure all the characters are alphabetical characters? If there is a non-alphabetic character, how will you keep asking the user for input until it is correct?

        As for sorting, check the Howtos section for some very simple sorting methods.

        Comment

        • laurie1388
          New Member
          • Feb 2008
          • 3

          #5
          That's pretty much my problem. I know how to do an if statement and a do while loop to ask for a different input, but I don't know how to get it to distinguish between letters and numbers or how to define them. Someone told me to use ASCII. I know what that is and which numbers equal the letters I need, but I don't know how to incorporate that into my code at all.

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Check out the isalpha(char) function.

            Comment

            Working...