Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C++ How to make lowercase words into uppercase?

I have the second half of this assignment done, but so very stuck on the lowercase to uppercase part! This is the assignment: Write a C++ program that reads in a text file ( called data10.txt), and prints to the screen the text with all extra blanks removed (that is, if there are 2 or more blanks in a row, replace them by a single blank) and changes all LOWERCASE letters to UPPERCASE letters. WATCH THAT YOU DON”T DELETE CARRIAGE RETURNS and tabs – whitespace includes tabs and carriage returns as well as blanks…so you only want to get rid of extra blank characters.

Here is my program:

//

//Amal Doleh

//CS5303 HW#10

//

#include <fstream>

#include <iostream>

#include <cstdlib>

#include <cctype>

using namespace std;

int main()

{

int count = 1;

char next_symbol, ch;

ifstream in_data;

in_data.open("/Users/amal_doleh/Desktop/hw10/data10");

if (in_data.fail())

{

cout << "Input file opening failed.\n";

exit(1);

}

while (!in_data.eof())

{

if (islower(ch))

{

ch = toupper(ch);

cout << ch;

}

in_data.get(next_symbol);

if (isspace (next_symbol))

{

count++;

}

else

{

if (count>0)

{

cout << " ";

}

count=0;

cout << next_symbol;

}

}

in_data.close();

cout << endl;

}

Update:

No, i'm using a program called Xcode on a Mac. I've tried to use "toupper" and it just screws up the output, ughhhh

6 Answers

Relevance
  • Cubbi
    Lv 7
    1 decade ago
    Favorite Answer

    Main problems with your program:

    .eof() is used for the file reading loop condition

    toupper(ch) is used before anything is stored in the object ch

    You *are* deleting "carriage returns" and tabs since you're using isspace()

    Because count is 1 from the beginning, you're printing an extra space before the first character of the file

    Otherwise, it's a sensible program, here it is all fixed up and working (file name is different since yours was cut off by yahoo)

    #include <fstream>

    #include <iostream>

    #include <cstdlib>

    #include <cctype>

    using namespace std;

    int main()

    {

         ifstream in_data("test.txt");

         if (!in_data)

         {

             cout << "Input file opening failed.\n";

             return 1;

         }

         char next_symbol;

         int count = 0;

         while( in_data.get(next_symbol) )

         {

             if (islower(next_symbol))

             {

                 next_symbol = toupper(next_symbol);

             }

             if (next_symbol == ' ')

             {

                 count++;

             }

             else

             {

                 if (count>0)

                 {

                     cout << " ";

                 }

                 count=0;

                 cout << next_symbol;

             }

         }

         cout << endl;

    }

    test: https://ideone.com/XOlZ2

    of course you don't have to use so many loops and branches if you don't want to... the following program does exactly the same thing (omitting file open test for brevity)

    #include <fstream>

    #include <algorithm>

    #include <iterator>

    #include <iostream>

    #include <ctype.h>

    bool two_spaces(char c1, char c2) { return c1 == ' ' && c2 == ' '; }

    int main()

    {

         std::ifstream in_data("test.txt");

         std::istreambuf_iterator<char> beg(in_data), end;

         std::string str;

         std::transform(beg, end, back_inserter(str), ::toupper);

         std::unique_copy(str.begin(), str.end(), std::ostreambuf_iterator<char> (std::cout), two_spaces);

    }

    test: https://ideone.com/wblL6

  • 1 decade ago

    Show us how you are using toupper(..).

    That function is in <cctype>

    int toupper ( int c );

    Convert lowercase letter to uppercase

    Converts parameter c to its uppercase equivalent if c is a lowercase letter and has an uppercase equivalent. If no such conversion is possible, the value returned is c unchanged.

    Notice that what is considered a letter may depend on the locale being used; In the default C locale, a lowercase letter is any of: a b c d e f g h i j k l m n o p q r s t u v w x y z, which translate respectively to: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z.

    In C++, a locale-specific template version of this function (toupper) exists in header <locale>.

    The link below is to details on cplusplus

  • Anonymous
    7 years ago

    qdwed2

  • 5 years ago

    hi, i will help you get to Uppercase :: fairly constructive you would be waiting to exercising consultation the thank you to get to lowercase in keeping with this occasion: #contain <stdio.h> #contain <ctype.h> #outline MAX 32 int substantial(void) {   char arr[MAX],ch='0';   int i=0;   printf("enter a string: ");   at the same time as(ch!='n') {     ch=getc(stdin);     if(i<(MAX-a million)) {       arr[i]=toupper(ch);       i++;     }   }   printf("%s",arr);   return 0; } Regards, Javalad

  • How do you think about the answers? You can sign in to vote the answer.
  • 1 decade ago

    /* toupper example */

    #include <stdio.h>

    #include <ctype.h>

    int main ()

    {

    int i=0;

    char str[]="Test String.\n";

    char c;

    while (str[i])

    {

    c=str[i];

    putchar (toupper(c));

    i++;

    }

    return 0;

    }

  • Anonymous
    1 decade ago

    Is you working in microsoft word?

Still have questions? Get your answers by asking now.