Like Tree2Likes
  • 1 Post By laserlight
  • 1 Post By Barney McGrew

Capitalize first Character in word.

This is a discussion on Capitalize first Character in word. within the C Programming forums, part of the General Programming Boards category; Hii all, I need your help. I want to do programming that requires: Input: my friend or My friend Output: ...

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    6

    Capitalize first Character in word.

    Hii all,
    I need your help. I want to do programming that requires:
    Input: my friend or My friend
    Output: My friend Or My Friend
    My code just can print output: my Friend Or My Friend
    I dont know how to capitalize the first character of word in sentence. From 2th word to end of sentence is OK.
    I'm using plain C
    Code:
    //Count character, words, space, line,count capital, count lowcase,convert capital <-> lowercase
    //Capitalize in first character of word
    #include<stdio.h>
    #include<string.h>
    int main(void)
    {
       char name[200], name1[200],name2[200];
       int i,j, count = 0, end = 0, line = 0, word = 0, flag = 0,wspace=0,capital=0,lowcase=0;
       printf ( "Enter the strings\n" );
       printf("\n");
       gets ( name );
       for ( i = 0; name[i] != '\0'; i++ )
       {
    
          if (name[i]==' ')
              wspace++;
    
          if (name[i] == '\t' )
             printf (" ");
              count++;
    
          if ( name[i] == '\0' )
             end = 0;
    
          else if ( end == 0 )
          {
             end = 1;
             line++;
          }
    
          if ( name[i] == ' ' || name[i] == '\t' || name[i] == '\n' )
             flag = 0;
    
          else if ( flag == 0 )
          {
             flag = 1;
             word++;
          }
          if (name[i]>='A'&& name[i]<='Z')
              capital++;
    
    
          if (name[i]>='a'&& name[i]<='z')
              lowcase++;
    
          if (name[i]>=97 && name[i]<=122)
              if (name[i]==32)
                  name[i]=name[i];
                else
                  name[i]=name[i]-32;
          else
              if (name[i]==32)
                  name[i]=name[i];
                else
                  name[i]=name[i]+32;
    
         name1[i] = toupper(name[i]);
          for (j=0;j<strlen(name);j++)
              if (name[j]==' '||name[j]=='\0' || name[j]=='\n')
                  name1[j+1]=toupper(name[j+1]);
    
         name2[i] = tolower(name[i]);
          for (j=0;j<=strlen(name);j++)
              if (name[j]==' ' || name[j]=='\0' || name[j]=='\n')
                  name2[j+1]=toupper(name[j+1]);
    
       }
       printf("\n");
       printf ( "Total number of characters %d\n", count-wspace );
       printf("\n");
       printf ( "Total number of word %d\n", word );
       printf("\n");
       printf ( "Total number of line %d\n", line );
       printf("\n");
       printf ( "Total number of space %d\n", wspace );
       printf("\n");
       printf ( "Total number of capital %d\n", capital );
       printf("\n");
       printf ( "Total number of lowcase %d\n", lowcase );
       printf("\n");
       printf ( "Exchanged case %s\n",name);
       printf("\n");
       printf ( "Exchanged case into capital always %s\n",name1);
       printf("\n");
       printf ( "Exchanged case into capital in the first character %s\n",name2);
       printf("\n");
    
    }
    Last edited by duongducthieniu; 12-31-2012 at 11:48 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by duongducthieniu
    I want to do programming that requires:
    Input: my friend or My friend
    Output: My friend Or My Friend
    I am guessing that you actually mean that the output should be: My Friend Or My Friend

    Anyway, at the moment you have code that goes over the string character by character to count them. My suggestion is this: take advantage of the fact that when you count a word, you toggle a flag. At that point, the current character should be capitalised (using toupper), so you just need to capitalise it and move on. Thus, you can discard your inner for loops that call strlen.

    By the way, instead of comparing with 'A' and 'Z', make use of isupper. Likewise, make use of islower. Also, instead of having variables named word and line, rename them to word_count and line_count. It would be easier to understand.

    Oh, and do not use gets as it is inherently vulnerable to buffer overflow. fgets would be an alternative here, but you should note that fgets reads in the newline character at the end if there is enough space.
    stahta01 likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    Thanks for your reply leserlight.
    1) Yes, you are right. My opinion is: My Friend Or My Friend
    2) You mean erase
    Code:
    name2[i] = tolower(name[i]);
          for (j=0;j<=strlen(name);j++)
              if (name[j]==' ' || name[j]=='\0' || name[j]=='\n')
                  name2[j+1]=toupper(name[j+1]);
    and change a litle bit after "count a word" and "toggle a flag"?
    3) Variables name can be change easily
    4) I tried to use "fgets" replace "gets" as mentioned but program did not run. Appear: "to few arguments to function fgets"

    I need some more details from you.
    Last edited by duongducthieniu; 01-01-2013 at 03:10 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by duongducthieniu
    2) You mean erase (...) and change a litle bit after "count a word" and "toggle a flag"?
    Yes. Not only the loop for name1, but also for name2. name should probably be renamed to text, name1 should probably be renamed fully_capitalized_text and name2 should probably be renamed to word_capitalized_text.

    Quote Originally Posted by duongducthieniu
    3) Variables name can be change easily
    Then make the changes and post your updated code.

    Quote Originally Posted by duongducthieniu
    4) I tried to use "fgets" replace "gets" as mentioned but program did not run. Appear: "to few arguments to function fgets"
    That is because you made no effort to find out how to call fgets correctly.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    You're making it way too complicated.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    
    int main(void)
    {
        int inspace = 1, c;
    
        while ((c = getchar()) != EOF) {
            putchar(inspace ? toupper(c) : c);
            inspace = isspace(c);
        }
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by Barney McGrew
    You're making it way too complicated.
    You're making it way too simplified though, but yeah, your code implements the core of my suggestion.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    I was referring to the overall design of his program, particularly how he was storing sequences of characters into arrays then operating on them (instead of operating on them one-by-one), rather than your responses. Sorry for the miscommunication.
    laserlight likes this.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,744
    Quote Originally Posted by Barney McGrew
    I was referring to the overall design of his program, particularly how he was storing sequences of characters into arrays then operating on them (instead of operating on them one-by-one)
    I actually think that is necessary here though: apparently there is a requirement to print various transformed versions of the input string after doing the various counting. In that sense, reading and processing one by one is not very different from reading the whole string first then processing one by one: in the end, the entire string still needs to be stored.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Dec 2012
    Posts
    6
    This is code that I changed a litle bit. It runs as my aspect.
    Code:
    name2[i] = tolower(name[i]);
          for (j=0;j<=strlen(name);j++)
          { if (j==0)
                 name2[j]=toupper(name2[j]);
            else
              {if (name[j]==' ' || name[j]=='\0' || name[j]=='\n')
              name2[j+1]=toupper(name2[j+1]);}}

  10. #10
    Registered User
    Join Date
    May 2012
    Posts
    333
    Quote Originally Posted by Barney McGrew View Post
    I was referring to the overall design of his program, particularly how he was storing sequences of characters into arrays then operating on them (instead of operating on them one-by-one), rather than your responses. Sorry for the miscommunication.
    By working directly on streams you can produce very succinct, efficient C programs.
    However the programs are also harder to modify. For instance my name has a capital that doesn't follow a space, as does yours for that matter. How would you patch the program to handle Scots surnames correctly?
    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.
    http://www.malcolmmclean.site11.com/www

  11. #11
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    Prefix trees are quite useful for that.

    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    struct letter {
        int letter;
        struct letter *leaf;
        struct letter *next;
    };
    
    static struct letter *maketrie(char *const *prefixes);
    
    static struct letter *lookup(struct letter *trie, int c);
    
    static int blah(struct letter **cp, int c)
    {
        struct letter *lp;
    
        if ((lp = lookup(*cp, '\0'))) {
            *cp = lp->leaf;
            return 1;
        }
        *cp = (lp = lookup(*cp, c)) ? lp->leaf : 0;
        return 0;
    }
    
    int main(void)
    {
        static char *prefixes[] = {
            "mc", "o'", 0
        };
        struct letter *trie = maketrie(prefixes);
        struct letter *tp;
        int inspace, c;
    
        for (inspace = 1; (c = getchar()) != EOF; inspace = isspace(c))
            if (inspace) {
                tp = trie;
                blah(&tp, tolower(c));
                putchar(toupper(c));
            } else {
                putchar(blah(&tp, tolower(c)) ? toupper(c) : c);
            }
    }
    
    struct letter *lookup(struct letter *tp, int c)
    {
        while (tp && tp->letter != c)
            tp = tp->next;
        return tp;
    }
    
    static struct letter *add(struct letter *trie, const char *str)
    {
        struct letter *tp;
    
        if ((tp = lookup(trie, *str))) { 
            tp->leaf = !*str ? 0 : add(tp->leaf, str + 1);
            return trie;
        }
        if (!(tp = malloc(sizeof *tp))) {
            fputs("memory error\n", stderr);
            exit(EXIT_FAILURE);
        }
        tp->letter = *str;
        tp->next = trie;
        tp->leaf = !*str ? 0 : add(0, str + 1);
        return tp;
    }
    
    struct letter *maketrie(char *const *prefixes)
    {
        struct letter *tp;
    
        for (tp = 0; *prefixes; prefixes++)
            tp = add(tp, *prefixes);
        return tp;
    }
    Last edited by Barney McGrew; 01-06-2013 at 12:19 AM. Reason: added a const qualifier

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Capitalize first letter of a word (function)
    By xwielder in forum C++ Programming
    Replies: 13
    Last Post: 10-05-2011, 11:11 PM
  2. Character String to Hex Word
    By asic_designer in forum C++ Programming
    Replies: 13
    Last Post: 03-11-2011, 01:35 PM
  3. Function for removing character from a word
    By TheDeveloper in forum C Programming
    Replies: 6
    Last Post: 07-07-2009, 05:30 AM
  4. 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
  5. how to obtain first character of every other word
    By archie in forum C++ Programming
    Replies: 8
    Last Post: 02-18-2002, 12:58 PM

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