how to obtain first character of every other word

This is a discussion on how to obtain first character of every other word within the C++ Programming forums, part of the General Programming Boards category; Hi, Need help with the program No strings or arrays used in this program, only char. In a sentence how ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    Post how to obtain first character of every other word

    Hi, Need help with the program
    No strings or arrays used in this program, only char.
    In a sentence how do I check to see that word should be seperated by a single blank.
    How do I capitalize the first charcter of every other word. If the sequence starts with a vowel then the first character of the first word should be capitalised otherwise the second word should be capitalised.
    Would appreciate anyones help
    Thanks!~

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    >>No strings or arrays used in this program, only char.
    Then it can't be done. You need to put the line in an array and go through it reading spaces until you read two and then the next character gets changed to upper case.
    C code. C code run. Run code, run...please!

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    7
    I agree, but it is suppose to read each character by character and also read in spaces using cin.get(). can you tell me how to check for the first char?
    Thanks




    Originally posted by CeeCee
    >>No strings or arrays used in this program, only char.
    Then it can't be done. You need to put the line in an array and go through it reading spaces until you read two and then the next character gets changed to upper case.

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    202
    acturally a char datatype is a array. Ever notice how specifing a buffer for a char is the same as specifying an array??
    "Christ died for our sins. Dare we make his martyrdom meaningless by not committing them?"

  5. #5
    Unregistered
    Guest
    look for an ascii table somewhere then use it to compare the char.

    char test;
    cin >> test;

    if( test == ' ' ) //check that its a space
    {
    //do this bit....
    }


    when you know the ascii values it is easy to chek that its upper or lower case, and so on. But you are severly limited if you cant use arrays or strings, are you sure about that?

  6. #6
    Registered User
    Join Date
    Nov 2001
    Posts
    46
    Code:
    if(isspace(cin.get(c))){
        count++;
    }
    if(count == 2){
        c = toupper(cin.get(c));
        count = 0;
    }
    C code. C code run. Run code, run...please!

  7. #7
    Registered User
    Join Date
    Feb 2002
    Posts
    7

    wny isn't this working?

    why isn't my program working??

    char ch;
    int countspace=0;
    cout << "Please enter a line of text, ";
    cout << "followed by a return: ";
    cout << endl;
    cin.get(ch);
    while (ch == '\n')
    {
    if (ch == ' ')
    countspace++;
    if(countspace > 0 && ch != ' ')

    ch= toupper(ch);
    cout <<ch;
    countspace = 0;
    }

  8. #8
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    why isn't my program working??
    because its wrong. It shows little thought.This is your code fixed but it doesn't do what your original post wanted it to do.

    Code:
    char ch; 
    int countspace=0; 
    cout << "Please enter a line of text, "; 
    cout << "followed by a return: "; 
    cout << endl; 
    do
    {
    cin.get(ch); 
    if (ch == ' ') 
    countspace++; 
    if(countspace > 0 && ch != ' ') 
    ch= toupper(ch); 
    cout <<ch<<flush; 
    countspace = 0; 
    }
    while (ch != '\n');
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  9. #9
    Unregistered
    Guest
    Problem:

    change this:


    File A:
    an apple a day
    keeps the doctor away

    into this:

    File B:
    An apple A day
    keeps The doctor Away

    without using strings or arrays.


    Here's my first pass at an algorhythm to solve this problem. I'm sure there are going to be plenty of pitfalls for you to overcome, but it demonstrates one way to approach the problem, and a place to start from if you elect to go this way.


    declare stream to read from file A and write to file B

    declare char dummy to hold input and analyze it

    declare counter to keep track of spaces in each line, initialize to 0

    read first char into dummy

    Outer loop:
    while dummy != EOF
    assign 0 to counter

    outer IF:
    if dummy is vowel
    change dummy to upper case
    write dummy to B
    read next char into dummy

    Inner Loop:
    as long as dummy not newline or EOF

    inner IF:
    if counter equals 2
    change dummy to upper case
    write dummy to B
    assign 0 to counter
    read in next char to dummy
    end inner IF

    inner else if counter != 2 and dummy is space
    increment counter
    write dummy to B
    read in next char to dummy
    end else if

    inner else if counter != 2 and dummy is not space
    write dummy to B
    read in next char to dummy
    end else if

    end inner loop
    end outer IF

    outer else if dummy != vowel and dummy != newline char and dummy != EOF
    bool initialSpace = false

    inner loop
    while dummy is not newline or EOF

    inner if
    if initialSpace is false and counter is 0 and dummy is not space
    write dummy to B
    read in next char to dummy
    end inner if

    inner else if initialSpace is false and counter is 0 and dummy is space
    assign 1 to counter
    write dummy to B
    read in next char to dummy
    end inner else if

    inner else if initialSpace is false and counter is 1 and dummy is not space
    change dummy to upper case
    write dummy to B
    change counter to 0
    read in next char to dummy
    end inner else if

    inner esle if initialSpace is true and counter is 2
    change dummy to upper case
    write dummy to B
    change counter to 0
    read in next char to dummy
    end inner esle if

    inner else if initialSpace is true and counter != 2 and dummy is space
    increment counter by 1
    write dummy to B
    read in next char to dummy
    end inner else if

    inner else if initialSpace is true and counter != 2 and dummy is not space
    write dummy to B
    read in next char to dummy
    end inner else if

    end inner loop
    end outer else if

    outer else if dummy is newline char
    write dummy to B
    read next char to dummy
    end outer else if.

    end outer loop


    Good luck

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function for removing character from a word
    By TheDeveloper in forum C Programming
    Replies: 6
    Last Post: 07-07-2009, 05:30 AM
  2. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  3. Unexplained "unhandled exception"
    By ulillillia in forum C Programming
    Replies: 6
    Last Post: 04-19-2007, 11:19 AM
  4. UNICODE and GET_STATE
    By Registered in forum C++ Programming
    Replies: 1
    Last Post: 07-15-2002, 03:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21