Thread: Change a line of text eash first char to uppercase

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Change a line of text eash first char to uppercase

    Write a C program that reads in one line of text from the Standard input, and writes out the line with the following changes:

    The first letter of all sentences in uppercase (capitalize the first letter).

    There are two spaces between the end of one sentence and the following sentence (except the first sentence).
    A sentence is always terminated by either a full stop (.), a question mark (?) or an exclamation mark (!), and then followed by zero or more spaces.

    Code:
    Enter a line for processing:
    abc. abc? abc! abc?! abc.abc, abc
    Abc. Abc? Abc! Abc?! Abc. Abc, abc

    This is my work out, does any line in my program is having mistake or any things need to change to make the program ore effiency?

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #define size 160
    
    void main() {
    
      char charArray[size];
      int i;
      char firstupper;
    
      printf("Enter a line for processing:\n");
      scanf("%[^\n]",charArray);
    
      firstupper=toupper(charArray[0]);  /* Change first char to uppercase*/
      printf("%c", firstupper);      /* Print first char after uppercase*/
    
        for (i=1; i<=size; i++) {
          if (charArray[i]=='\0')    /* Stop if \n */
               break;
    
           if ( (charArray[i]=='.' && charArray[i+1]==' ') ||
                (charArray[i]=='!' && charArray[i+1]==' ') ||
                (charArray[i]=='?' && charArray[i+1]==' ') )   /* Check char='! ','? ','. '  or '!','?','.' following by space */
                {  printf("%c ",charArray[i]);
                   i=i+2;
                      printf("%c", toupper(charArray[i]));}
            else if  ((charArray[i]=='.') || (charArray[i]=='!') || (charArray[i]=='?'))
                { printf("%c ",charArray[i]);
                  i=i+1;
                      printf("%c", toupper(charArray[i]));}
            else
                printf("%c", (charArray[i]));
    
            }
               
      fflush(stdin);
      getchar();
    
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > void main()
    > fflush(stdin);
    Read the FAQ - both these are incorrect 'C' usage.

    > for (i=1; i<=size; i++)
    Array subscripts for a[N] are 0 to N-1
    Since you've already dealt with element 0, it should be
    for (i=1; i<size; i++)

    On the other hand,
    for (i=1; i<strlen(charArray); i++)
    would save you having to do this inside the loop

    if (charArray[i]=='\0') /* Stop if \n */
    The comment does not match the code either

    > charArray
    Well yeah, I can see that it's a char array simply from looking at the declaration.
    Choose a name which reflects it's use, say
    char userInputSentence[size];

    > (charArray[i]=='.') || (charArray[i]=='!') || (charArray[i]=='?')
    You do this twice. A function is starting to look like a nice idea to clean up the code

    Code:
    int isSentenceEnd ( char ch ) {
      return (ch=='.') || (ch=='!') || (ch=='?');
    }
    Your conditions then become say
    if ( isSentenceEnd(charArray[i]) && charArray[i+1]==' ' )
    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.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    16

    Wink

    Thank for your point out,I am nebie in C for just 3 month.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I am nebie in C for just 3 month.
    I'm curious why so many people say "I'm a newbie. I've only been programming for x months" all of the time in their posts. It doesn't make you look better or worse, and doesn't add anything relevant to the question.

    On top of Salem's advice:

    >#define size 160
    Named constants really should be distinguishable from variables. The accepted convention is to use all caps for macros:
    Code:
    #define SIZE 160
    >scanf("%[^\n]",charArray);
    There's no reason to use scanf in this way. It adds nothing and takes quite a bit away. What if the user enters more than 160 characters before a newline? Fgets is a much better solution:
    Code:
    if ( fgets ( charArray, sizeof charArray, stdin ) == NULL ) {
      fprintf ( stderr, "Bad input\n" );
      /* Try again or exit gracefully */
    }
    >for (i=1; i<strlen(charArray); i++)
    >would save you having to do this inside the loop
    Of course, it would also be unnecessarily inefficient:
    Code:
    int len;
    ...
    len = strlen ( charArray );
    for ( i = 1; i < len; i++ )
    My best code is written with the delete key.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Originally posted by Prelude
    >I am nebie in C for just 3 month.
    I'm curious why so many people say "I'm a newbie. I've only been programming for x months" all of the time in their posts. It doesn't make you look better or worse, and doesn't add anything relevant to the question.
    Simply no one likes looking like their dumb. Especailly in the midst of talented individuals regardless of the field. A preface of that kind allows us and any other newbie to feel comfortable to make mistakes and feel free to ask any questions without fear of reprecussions. but you knew that already.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    how can I modify the program to make it stop it the user press 'Enter' when asking for the line input?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > scanf("%[^\n]",charArray);
    If you just press enter, what is
    the value of strlen(charArray)
    the return result of scanf(), which you're presently ignoring.
    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.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    if I just press the enter key, the o/p is a rubbish ascii code(not alpha char) and the strlen is 3

  9. #9
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317

    Reason I say I am a newbie.

    The only reason I say I am a newbie is if I am wrong I want someone to point it out. I may believe something is true only to find out that I was misinformed or had made some incorrect assumptions.

    Experts make mistakes too, but generally a newbie gets more detailed answers to questions because the level of knowledge is known to be weak where an expert is supposed to know better.
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but you knew that already.
    Yes, I just wanted to hear someone else say it.

    >if I am wrong I want someone to point it out
    Don't worry, if you're wrong someone will call you on it. In fact, several someones will probably call you on it. In painful detail. With examples. And quotes. And they'll never forget. Especially if you don't often make mistakes.
    My best code is written with the delete key.

  11. #11
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by Prelude
    >if I am wrong I want someone to point it out
    Don't worry, if you're wrong someone will call you on it. In fact, several someones will probably call you on it. In painful detail. With examples. And quotes. And they'll never forget. Especially if you don't often make mistakes.
    Been there, eh Prelude? Do I detect some slapped posts in your past?
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Been there, eh Prelude? Do I detect some slapped posts in your past?
    Indeed. Some rather embarrassing ones too.
    My best code is written with the delete key.

  13. #13
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I'd like to see some of those post Prelude any links(Don't Worry we know your the best)

  14. #14
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I'd like to see some of those post Prelude
    I couldn't hope to find any of the good ones. If you want to search all of my posts I'm sure you'll see a few.
    My best code is written with the delete key.

  15. #15
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    more than 133 pages Wowzers, and to think I would have to go through all of that to find one or two errors!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Program Crashing
    By Pressure in forum C Programming
    Replies: 3
    Last Post: 04-18-2005, 10:28 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Strings are V important...
    By NANO in forum C++ Programming
    Replies: 15
    Last Post: 04-14-2002, 11:57 AM