Thread: How to capitalize first letter in a sentences

  1. #1
    Registered User
    Join Date
    Jul 2020
    Posts
    2

    How to capitalize first letter in a sentences

    There is a task of capitalize the first letter of every sentence, not every word, into uppercase.
    cannot be used to upper function.
    I made a code that capitalize the first letter of each word, but I canít guess how to make only the first letter of each sentence.

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <string>
    
    #define IsAlpha(x)\
    (x>='A'&& x<='Z'||x>='a'&& x<='z'? true: false)
    
    
    using namespace std;
    
    
    int main() {
        char s[1024];
        puts("Enter the string");
        cin.getline(s, sizeof(s));
        bool IsPrev = false;
       
        for (int i = 0; i < sizeof(s)/sizeof(char)&& s[i]!= 0; i++)
        {
            if (IsAlpha(s[i]))
            {
                if (!IsPrev)
                    s[i] += 'A' - 'a';
                IsPrev = true;
            }
            else
                IsPrev = false;
                
                   
            }
        printf("%s", s);
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,941
    What is a "sentence"? You need to be clear on what that means in the context of your program before you can arrive at a solution.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,176
    > #include <iostream>
    > #include <stdio.h>
    Decide whether you're a C programmer, or a C++ programmer.

    Random mix-and-match of both languages won't work in the long run.



    > #define IsAlpha(x)
    Use the appropriate 'is....' function in ctype.h
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    426
    You've got two states, "outside sentence" and "within sentence". You start in the "outside sentence" state. If you hit an alphabetical character in the "outside sentence" state, you set it to upper case, and switch to the "inside sentence" state. If you hit a period in the "inside sentence" state, you switch to the "outside sentence" state.

    That's a basic program. You can make it more sophisticated by detecting periods which are in fact decimal points, and by considering what to do if you hit a non-alpha cahracter in the "outside sentence" state. You might also want special rules for quotes, though then it becomes very complicated indeed.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  5. #5
    null pointer Structure's Avatar
    Join Date
    May 2019
    Posts
    303

    Post

    Code:
    #include <stdio.h>
    #include <string.h>
    
    #define true 1
    #define false 0
    
    void capitalize(char *paragraph){
        char outBuffer[2048]; int c = 0; 
        char this = '\0'; int capThis = true, skip = false;
        for (int i=0; i < strlen(paragraph); i++) {
            switch(this = paragraph[i]) {
                case '.':
                    capThis = true;
                break;
                default:
                    if (capThis && (this != ' ')) { 
                        capThis = false;
                        if ( ((this - 32) < 91) && ((this - 32) > 64) ) {
                            this = (this - 32);
                        }
                    }
                break;
            }; if (!skip) outBuffer[c++] = this;
        }
        outBuffer[c++] = '\0';
        printf("%s",outBuffer);
    }
    
    int main(int argc, char *argv[]) {
    
        capitalize( "the first letter... of each sentence." );
    
        return 0;
    }
    "without goto we would be wtf'd"

  6. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    59
    Hallo laserlight!

    I think with asentence he means something like that:
    "good wine needs no bush"
    or
    "the bread never falls but on its buttered side"

    The first letter into uppercase:
    "GOOD wine needs no bush"
    "THE bread never falls but on its buttered side"

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,941
    Quote Originally Posted by rusyoldguy View Post
    Hallo laserlight!

    I think with asentence he means something like that:
    "good wine needs no bush"
    or
    "the bread never falls but on its buttered side"

    The first letter into uppercase:
    "GOOD wine needs no bush"
    "THE bread never falls but on its buttered side"
    That was a question for Mihrusha to think about, not for you to answer. If I did want to implement a solution to this task, your answer is not helpful: all you did was provide two examples with no explanation as to why they constitute a "sentence".

    Furthermore, your example of the result is obviously wrong: you're transforming the entire first word into uppercase rather than only the first letter.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 11-23-2016, 03:02 PM
  2. capitalize letter without using arrays
    By Ph0x in forum C Programming
    Replies: 11
    Last Post: 10-28-2014, 06:03 AM
  3. Replies: 12
    Last Post: 11-24-2012, 04:10 AM
  4. Capitalize first letter of a word (function)
    By xwielder in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2011, 11:11 PM
  5. Capitalize first letter of every word in .txt file
    By crazygopedder in forum C Programming
    Replies: 9
    Last Post: 10-15-2008, 12:09 PM

Tags for this Thread