Thread: Please Help (capitalise sentences)

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    59

    Please Help (capitalise sentences)

    I am trying to read characters from output and converting the first letter of the character to capital case and ignore or delete space when ever i see '\0' or leave the space if the next character is in upper case ..please i need a clue on what to do ...i have already begin but i need help cuz i am confused now.

    Code:
    
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define UPPER_CASE 1
    #define LOWER_CASE 2
    
    int main(void) {
    
        int c;
        while ((c = getchar()) != '\n') {
                
             if ( isalpha ( c) ) 
    		 	{
             	
      		 if ( islower  ( c ) )
    		   	 {
      		 	
          		c = toupper( c );
    		  
    			  putchar(c);
    		   
      		 }
             }
             
              
        }
        return EXIT_SUCCESS;
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Think of how you'd do this, yourself, with just paper and pencil.

    First word gets a capitalized first letter.

    Now you leave the letters alone, until you find a space or newline. Even a comma or a period, just ignore them.

    After the space, if it's not the first word, then assign the first letter of that word, to uppercase - it doesn't matter WHAT it's case was before, see?

    Think through the logic by hand, until you really understand it, and THEN you'll be ready to code up the solution.

    Worst thing you can do is start typing up code, before you understand the logic you need to solve the problem.
    Last edited by Adak; 08-21-2011 at 12:12 AM.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Thanks for your reply....i have thought of that but my problem how do i read when the first letter has gone in and after reading that do i have to putchar and start reading for another letter ...please help me understand iit better.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Code:
    #include <stdlib.h>
    
    #define UPPER_CASE 1
    #define LOWER_CASE 2
    
    int main(void) 
    {
    
        char c;
    	int i=0; //flag to check if first character
    	while(c!='\n')
    	{
       	c = getchar(); 
                
             if (isalpha(c)) 
    		 {
              if ( islower  (c) )
    		   	{
      		 	if(i==0)
    			{
          		c = toupper(c);
    			i++;	
    			}
    		    putchar(c);
    		    }
    			else
    			if(isupper(c)) //checking if caps
    			printf(" %c",c);//if caps,print a space and then the char
             }
             
            
        }
        return EXIT_SUCCESS;
    }
    im just a beginner so completely following the above code might not be good as coding practice might not be so good.
    when i run your code, i get an access violation error .i don't know why.

    the above code does what you want.

    i made the following modifications:
    1>a flag variable i :to check if the character being read is the first character.
    2>if there is a caps character,then print a space and the character.

    hope this is what you want.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I was able to come up with this ...i don,t really know how to put a space in the while loop ......I want to put a space after the first character..


    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main(void) {
        int last_digit_seen = 0;
        
        int c;
        while ((c = getchar()) != '\n') {
            if (!isalnum(c))
                continue;
            if(last_digit_seen==0 && (islower(c)))
                {
                 c=toupper(c);
    			
                }
                putchar(c);
                
            
        }
        putchar('\n');
        return EXIT_SUCCESS;
    }

  6. #6
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    your question is not so clear.

    im assuming that if you give input as abcd
    then your o/p should be: A bcd
    is this what you want?

    so your problem is that if you put a space inside the while loop,it will get printed again and again. but you want only a single space.
    if this is what you want,confirm in your next post.

    if you want a space after the first character only,then use a flag variable as below:
    Code:
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    
    int main(void) {
        int last_digit_seen = 0;
        
        int c;
        int i=0; //flag variable
        while ((c = getchar()) != '\n') {
            if (!isalnum(c))
                continue;
            if(last_digit_seen==0 && (islower(c)))
                {
                 c=toupper(c);
    			
                }
                if(i==0)
                {
                printf(" ");  // prints a space in the first run
                 i++;// increment so that in the next looping,no space wont be printed as the 
                      if condition would go wrong.
                }
                putchar(c);
                
            
        }
        putchar('\n');
        return EXIT_SUCCESS;
    }

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    yes ..Thats what i want ...after runing the code, the first letter appears two place ....ie in the first place and after the space .

    Code:
    
     if(i==0)
                {
                printf(" ");  // prints a space in the first run
                 i++;// increment so that in the next looping,no space wont be printed as the 
                      if condition would go wrong.
                }
                putchar(c);

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    Code:
    
    
    
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define UPPER_CASE 1
    #define LOWER_CASE 2
    
    int main(void) {
        int last_digit_seen = 0;
        int i=0;
        int c;
        while ((c = getchar()) != '\n') {
            if (!isalnum(c))
            {
            	continue;
            }
            
            if(last_digit_seen==0)
                {
                 c=toupper(c);
      		    	putchar(c);
      		        ++last_digit_seen;	
                }
                
                 if(last_digit_seen==1)
                {
                printf(" ");  
                 ++last_digit_seen;
                }
                putchar(c);
    
        }
        return EXIT_SUCCESS;
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Nice to see you post more code, but remember to say something about the code, e.g., what is not working with this updated code. Also, it is good that you have made some effort to format your code, but your indentation can still be made more consistent.
    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

  10. #10
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    really sorry about that last post dude.that program wont give you the correct output.
    just make the following changes:
    Code:
      if(i==0)
                {
                printf("%c ",c);  
                 i++;
                }
    		else
                putchar(c);
    now a space will be printed after the first character.
    but now all characters are printed in caps. is that what you want?

    just give me a sample input and sample output that would make your need clear.

  11. #11
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Quote Originally Posted by laserlight View Post
    Nice to see you post more code, but remember to say something about the code, e.g., what is not working with this updated code. Also, it is good that you have made some effort to format your code, but your indentation can still be made more consistent.
    are you saying this to me or to code_lover?

  12. #12
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    I want some thing like this ...when you input the first , it will output thse second.


    Only letters and spaces are output, other symbols such as ^ are ignored.

    O nlylettersandspacesareoutputothersymbolssuchasarei gnored

    Exactly ONE Space is INSERTED betWeen letters of DIFferent cases!

    E xactly ONES paceis INSERTED bet W eenlettersof DIF ferentcases

  13. #13
    Registered User
    Join Date
    Aug 2011
    Posts
    91
    Quote Originally Posted by code_lover View Post
    I want some thing like this ...when you input the first , it will output thse second.


    Only letters and spaces are output, other symbols such as ^ are ignored.

    O nlylettersandspacesareoutputothersymbolssuchasarei gnored

    Exactly ONE Space is INSERTED betWeen letters of DIFferent cases!

    E xactly ONES paceis INSERTED bet W eenlettersof DIF ferentcases
    ok that needs some thinking. let me try to figure out.
    hope some veteran comes here soon.

    meanwhile,keep trying.if you get the code,dont forget to post!

  14. #14
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by code_lover View Post
    I want some thing like this ...when you input the first , it will output thse second.

    ....
    So far the only thing I have seen you do is copy the code theju112 posted for your directly without giving any thought to the problem yourself. With the code you have now, right up some psuedo code to cover how you would solve this problem. With that psuedocode, go through the problem on paper to check your answer. Once you get that working come back here and post what you have accomplished and any new errors / questions.

    @theju112: We have this thing called a homework policy. If you are going to participate here I suggest you become familiar with it.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  15. #15
    Registered User
    Join Date
    Aug 2011
    Posts
    59
    right now ...i have come up with this ....

    Code:
    
    
    #include <ctype.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    #define UPPER_CASE 1
    #define LOWER_CASE 2
    
    int main(void) {
        int last_digit_seen = 0;
        int c;
        while ((c = getchar()) != '\n') {
            if (!isalnum(c))
                continue;
            
            if(last_digit_seen==0)
                c=toupper(c);
    	     	 putchar(c);
               last_digit_seen++;
            if(last_digit_seen==1)
                printf("%c",'\0');
                last_digit_seen++;
                
                c=getchar();
    			putchar(c);
    		if(last_digit_seen >1)
    		 ;
    		  	/* i need to append the characters together ....*/
        }
    
        putchar('\n');
        return EXIT_SUCCESS;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to input sentences into file?
    By Kvinzzz in forum C Programming
    Replies: 4
    Last Post: 09-01-2010, 07:15 AM
  2. problems with repeating sentences..
    By patron in forum C Programming
    Replies: 6
    Last Post: 02-20-2008, 07:15 PM
  3. Parsing sentences
    By curlious in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-16-2004, 01:20 AM
  4. how to count the number of sentences.
    By Ray Thompson in forum C Programming
    Replies: 3
    Last Post: 11-10-2002, 10:25 AM
  5. how to count sentences and words?
    By Ray Thompson in forum C Programming
    Replies: 1
    Last Post: 11-08-2002, 01:42 PM