Thread: Find number of capital letters and punctuations in a string!

  1. #1
    Registered User
    Join Date
    Jan 2015
    Posts
    12

    Exclamation Find number of capital letters and punctuations in a string!

    write a C program which repeatedly reads in sentences from the user and reports on how many capital letters are in the sentence and how many punctuation characters. Your program will stop asking for input once the user has entered in a blank line.
    Consider the following example usage with the program. User input is marked in underline:
    Enter a sentence: John and Mary went to Fred's house.
    You used 3 capital letters and 2 punctuation characters.
    Enter a sentence: I like A&W more than McDonald's.
    You used 5 capital letters and 3 punctuation characters.
    Enter a sentence:
    Good bye!

    Hint: make use of the standard C functions ispunct and isupper.

    Other requirements

    You must make two functions.

    1. Make a function called find_characters, which has a return type of void, and which hase three parameters: one of type char * (a string to find characters in), one of type int * (a reference to int variable indicating how many capital letters are in the string) and the last one also of type int * (a reference to an int variable indicating how many punctuation characters are in the string). Your find_characters function should scan the string and update the two variables it has references to.
    2. Make a main function. This function should repeatedly read in a string from the user, call your find_characters function, and output the information returned to it by the find_characters function indicating how many capital letters and how many punctuation characters were in the string. Your main function should stop reading in input when the user enters in a blank string (i.e., the user just hits enter without entering anything else in). You may assume that the user will not enter in a sentence longer than 100 characters



    This is what i did so far.. help me out
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    #include<string.h>
    
    
    int main(void) 
    {  
    	int i;
    	char str[100];
    	printf("\nEnter The String : ");
    
    
     	while(scanf("%99[^\n]s", str) == 1){
    		int upper_case = 0;
    		int punctuation = 0;
    
    
    		for(i = 0; i <= (int) strlen(str)-1;i++){
    			if (isupper(str[i])){
    				upper_case++;
          		}
          		if (ispunct(str[i])){
            		punctuation++;
          		}
    		}
    
    
    		printf("\nUppercase Letters : %d", upper_case);
        	printf("\npunctuation : %d\n", punctuation);
        	printf("------------------------\n\nEnter The String : \n");
    	
     	}
    	return (0);
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You have it almost ready. All you need to do now is move that for loop inside function "find_characters". What do you need help with? Making functions? There are some good tutorials over here.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jan 2015
    Posts
    12
    yes i need help with it..because i am a beginner in programming, help me with that

  4. #4
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by Jass Preet View Post
    yes i need help with it..because i am a beginner in programming, help me with that
    You have already created a function called main, which takes no arguments (void) and returns an int.

    Based off of that and on one of the tutorials GReaper suggested, how would a function like the one you need look like?

  5. #5
    Registered User
    Join Date
    Jan 2015
    Posts
    2

    try this

    try this
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void find_characters(char * str, int * cap_num_t, int * pc_num_t);
    
    int main()
    {
        char string[100];
        int punct_num, cap_num,n=0;
    
        while(n==0){
            punct_num=0;
            cap_num=0;
            system("cls");
            printf("Enter a sentence: ");
            gets(string);
            if(string[0]=='\0'){
            n++;
    
            }else{
            find_characters(string, &cap_num, &punct_num);
    
    
            printf("\nYou used %d capital letters and %d punctuation characters\n", cap_num, punct_num);
            getch();
            }
    
    
    
        }
        printf("Good bye!");
    
        return 0;
    }
    void find_characters(char * str, int * cap_num_t, int * pc_num_t){
    
        while(*str!='\0'){
    
    
            if(*str>64&&*str<91){
    
            *cap_num_t=*cap_num_t+1;
    
    
    
            }else if((*str>32 && *str<48) || (*str>57&&*str<65) || (*str>90&&*str<97) || (*str>122&&*str<127)){
    
             *pc_num_t=*pc_num_t+1;
            }
    
            *str++;
        }
    
    
    
    
    
    
    
    
    
    }
    Last edited by klosowski0; 01-31-2015 at 12:17 PM.

  6. #6
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Quote Originally Posted by klosowski0 View Post
    try this
    ....
    Your solution is horrible. You didn't use ispunct or isupper and your indentation is unacceptable.

  7. #7
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by klosowski0 View Post
    try this
    <snip>
    Also, giving out solutions goes against the purpose and the rules of this forum. Please stop doing that.

  8. #8
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    Quote Originally Posted by klosowski0 View Post
    Code:
    void find_characters(char * str, int * cap_num_t, int * pc_num_t){
    
        while(*str!='\0'){
    
    
            if(*str>64&&*str<91){
    
            *cap_num_t=*cap_num_t+1;
    
    
    
            }else if((*str>32 && *str<48) || (*str>57&&*str<65) || (*str>90&&*str<97) || (*str>122&&*str<127)){
    
             *pc_num_t=*pc_num_t+1;
            }
    
            *str++;
        }
    }
    Well, that couldn't be good for your grades.

  9. #9
    Registered User
    Join Date
    Jan 2015
    Posts
    2
    I agree, i'm new here.
    It's an app from one of my first lessons in c.
    We were forbidden to use string.h library to actually learn about how several functions work.
    Sorry for not reading rules, sorry for providing a notsogood solution.

  10. #10
    Lurker
    Join Date
    Dec 2004
    Posts
    296
    Quote Originally Posted by klosowski0 View Post
    I agree, i'm new here.
    It's an app from one of my first lessons in c.
    We were forbidden to use string.h library to actually learn about how several functions work.
    Sorry for not reading rules, sorry for providing a notsogood solution.
    In the future, if you have a solution you want to show people, then it is best to wait until the original poster have a more or less complete solution himself.

    Or create a new thread to show your solution and ask for critique, if you feel that you can't wait. It is quite alright to get inspired from threads and feel that you would like to cut your teeth at solving the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why capital letters in function
    By vrkiller in forum C++ Programming
    Replies: 2
    Last Post: 02-19-2011, 01:21 PM
  2. Capital Letters?
    By MiroMage in forum C Programming
    Replies: 8
    Last Post: 11-05-2008, 04:32 PM
  3. strcmp, a problem with capital letters
    By RichardH in forum C Programming
    Replies: 11
    Last Post: 04-23-2007, 08:49 AM
  4. Capital Letters ...
    By twomers in forum C++ Programming
    Replies: 11
    Last Post: 01-11-2006, 03:10 AM
  5. check for capital letters
    By blackwyvern in forum C++ Programming
    Replies: 3
    Last Post: 01-30-2002, 10:32 PM

Tags for this Thread