Find the answer to your Linux question:
Results 1 to 8 of 8
I'm trying to write a program that counts the number of uppercase and lowercase characters in a sentence. Here's what i've done: /* Counts the number of upper and lowercase ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2011
    Posts
    38

    How do I avoid the null terminator?


    I'm trying to write a program that counts the number of uppercase and lowercase characters in a sentence. Here's what i've done:

    /*

    Counts the number of upper and lowercase characters in a sentence.

    */

    #include <iostream>
    #include <cctype>
    #include <cstdio>
    using namespace std;

    int main() {

    int i , j = 0, k = 0;
    char str[80];

    cout << "Enter a sentence: " << '\n';
    cin >> str;

    for(i = 0; str[i]; i++) {
    if(isupper(str[i])) j++;
    else if(islower(str[i])) k++;
    }

    cout << "Number of uppercase letters: " << j << '\n';
    cout << "Number of lowercase letters: " << k << '\n';
    cout << str << '\n';

    return 0;

    }

    This is the output that I get when I test it:


    Enter a sentence:
    Hello me
    Number of uppercase letters: 1
    Number of lowercase letters: 4
    Hello

    Clearly, when I enter a sentence, the computer is not recording what I write after the first null terminator. How can I get it to ignore the null terminator between "Hello" and "me" and terminate at the final null terminator instead?

  2. #2
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    I'm a bit confused, because to me it looks like your input is "Hello me" (with a space), but you are claiming that it contains a NUL byte. How are you entering your input?

    The short answer to your general question is that you cannot. Under the definition of a C string, a NUL byte ends a string. If you know the length of the string, you can treat it as essentially a memory buffer and work that way, but you cannot have NUL bytes in strings.

    Also, as a general note, since you're learning C++: you may want to check out the standard C++ string class. What you are using here is often called a C string, but C++ has a proper class that adds a bunch of functionality.

  3. #3
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    Hi,
    guess your problem ain't the null.

    Would suggest that you take a closer look into the for loop you are using.
    Especial the ";str[i];" part.

  4. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    51
    Yes, the issue is not null but the space. You need to look at how you are reading in your string. You think the str has the value of "Hello me" but it doesn't. Put a cout after your cin to see what the value of str really is and go from there.

  5. #5
    Just Joined!
    Join Date
    Jun 2006
    Posts
    55
    Normally, cin reads words delimited by whitespace (or end of line).
    The following link (that I found in Google) explains how it could be overcome:
    C++ Notes: Reading Characters

  6. #6
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    Huh, interesting, I never knew that about cin. Admittedly, I haven't used C++ in a VERY long time.

  7. #7
    Just Joined!
    Join Date
    Aug 2011
    Posts
    38
    I think i'm really missing the point here, but I still can't get it to work. The way I see it, when I use the command "noskipws", i'm telling the computer not to skip any whitespaces from now on. So I put it in my program like this:

    #include <iostream>
    #include <cctype>
    #include <cstdio>
    using namespace std;

    int main() {

    int i , j = 0, k = 0;
    char str[80];
    cin >> noskipws; // <------------Here it is!

    cout << "Enter a sentence: " << '\n';
    cin >> str;

    for(i = 0; str[i]; i++) {
    if(isupper(str[i])) j++;
    else if(islower(str[i])) k++;
    }

    cout << "Number of uppercase letters: " << j << '\n';
    cout << "Number of lowercase letters: " << k << '\n';
    cout << str << '\n';

    return 0;

    }

    But when I run it I still get:


    philip@Project-Hollywood:~/C++$ ./sentence
    Enter a sentence:
    Hello Me
    Number of uppercase letters: 1
    Number of lowercase letters: 4
    Hello

    So it still isn't registering past whitespaces =S

    Also, how do I write the cin.get command? I've tried it these ways:
    cout << "Enter a sentence: " << '\n';
    cin >> cin.get(str);

    cout << "Enter a sentence: " << '\n';
    cin.get(str) >> str;

    cout << "Enter a sentence: " << '\n';
    cin.get(str);

    and I keep getting errors.

    ?

  8. #8
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    noskipws is explained here:
    noskipws - C++ Reference

    In particular, it appears to mean that initial whitespace will not be discarded. If you provide input of: "____foo" (where each _ is a space) to normal cin, you will get "foo". With noskipws, the initial whitespace will be included.

    As for how to use cin.get(): first off, I recommend using cin.getline() instead, as it will remove the delimiting character from the stream (so in this case, it will remove the newline; cin.get() would leave it there).

    istream::getline - C++ Reference

    Secondly, your str variable can only store 80 characters. Therefore, you need to tell cin.getline() that you can only store 80 characters: if the string were 100 characters long, it wouldn't fit in the array, and would cause an error:
    Code:
    cin.getline(str, 80);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •