Thread: Strings

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    5

    Strings

    Hi there, iv recently got this question:

    Write a fully modular C program that reads in a string of a defined size from the keyboard and reports how many times each letter of the alphabet occurs within that string (ignoring case). It should also report how many non-letters are included in the string.

    I understand what it's asking me to do, i just can't get my head around a function that reports how many times each letter of the alphabet occurs/how many non letters occur within the string without using a giant switch-case statement.

    Any help with regards to how i could report how many times each letter of the alphabet/ how many non letters occur within the entered string would be greatly appreciated. I'm not asking someone to do it all for me, just point me in the right direction .

    Here's my code so far that reads in a string and converts it to lower case. I know it's not much.. but it's a start.

    Code:
      
    #include <stdio.h>
    #include <string.h>
    
    
    void readin(char line[])
    {
    
    	printf("Enter string: ");
    	scanf("%s", line);
    	strlwr(line);
    	
    	return;	
    }
    
    {
    
    	const int MAXSIZE = 100;
    	char line[MAXSIZE] = {'\0'};
    	
    	readin(line);
    	
    	return(0);
    	
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well, set yourself down with a pencil and paper...
    Now work the problem as though you are the computer...
    Write a letter ... add to a count for that letter
    write another letter ... add to a count add to a count...
    and so on.

    Try some different approaches... see if you can figure out a real simple way of doing it.

    Then when you think you finally understand the problem, write up some quicky point by point notes on what you need to code it.

    Then... finally turn on your computer and start writing code.

    One of the first lessons a programmer learns is that you simply can't code the solution to a problem you don't understand.

    FWIW... You should be able to do this in about 25 lines of code...

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    Thanks, I'm doing that right now, i understand the problem... I'm just finding it hard to put the solution i have in my head into code. I'm very new to C programming, anyway thanks again.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by joel0z View Post
    Thanks, I'm doing that right now, i understand the problem... I'm just finding it hard to put the solution i have in my head into code. I'm very new to C programming, anyway thanks again.
    Write it as "pseudo code"... sort of a pigeon english of your own making... then translate it to C...

    On of the big problems people have when they first start coding is that they think in big blobs... "Just count all the As..." but the computer works in much smaller steps... set to start of string, what is first character? Where to count first character? How to increase the count?... now do next character... One of the diciplines is to learn to think in blobs at least as small as the computer does...

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    5
    Thanks again, ill write up some pseudo code and post it on here, hopefully you can give me some help with that.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    5
    sigh.. i can't think of a way to do it without simply just iterating through the string using a massive switch-case statement to increment of the 27 integer variables(one for each of the letters of the alphabet and an additional variable to store the non-alphabetic characters). I know it would work... but it's poor design and i know there's a much more efficient way... I just can't figure out how to do it, surely it's not that hard. geez..

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Looping through the string is the way to go, but you don't need a huge switch when there are built in library functions that will work for you. Look at the ctype.h functions.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    5
    thanks quzah I got the program to compile I'm pretty sure it does what the question asks. here's the code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    
    void readin(char line[])
    {
    
    	printf("Enter string: ");
    	scanf("%s", line);
    	strlwr(line);
    	
    	return;	
    }
    void counter(char line[], int &count)
    {
    	
    	while(line[count])
    	{
    		if(isalpha(line[count]))
    			printf("Character %c is alphabetic\n",line[count]);
    		else
    			printf("character %c is not alphabetic\n",line[count]);
    		count++;
    	}
    
    	return;
    }
    
    int main()
    {
    
    	const int MAXSIZE = 100;
    	char line[MAXSIZE] = {'\0'};
    	int count;
    	
    	count = 0;
    	
    	readin(line);
    	counter(line,count);
    	
    	return(0);
    	
    }

  9. #9
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    The fundamental flaw is using C++ compiler to compile C code.
    Code:
    void counter(char line[], int &count)
    Edit:

    Code:
    const int MAXSIZE = 100;
    char line[MAXSIZE] = {'\0'};
    This will produce compiler error for C89 compiler.
    If this compile without any error, it means you're using C99 Variable Length Array(VLA).

    Code:
    #define MAXSIZE 100
    seems much more better choice since the size is known at compile time.


    You could use if ignoring case,

    Code:
    int alpha_count[26] = {0};
    if( isalpha(line[i]) ) {
       alpha_count[ tolower(line[i]) - 'a'];
    }
    Pick a good book on C and learn. Don't confuse C and C++. and of course know what C standard feature you are using.
    Last edited by Bayint Naung; 05-18-2011 at 12:15 AM.

  10. #10
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by joel0z View Post
    sigh.. i can't think of a way to do it without simply just iterating through the string using a massive switch-case statement to increment of the 27 integer variables(one for each of the letters of the alphabet and an additional variable to store the non-alphabetic characters). I know it would work... but it's poor design and i know there's a much more efficient way... I just can't figure out how to do it, surely it's not that hard. geez..
    Your thinking process is on the right track.

    Yes, pretty much 27 integer variables.
    Whenever there’s a need for a lot of variables, yet each variable does similar jobs as the rest, think of using an array.

    26 elements, one for each letter counter. Then access the array element that needs incrementing using an index which was calculated based on the letter. 'A' is 0, 'B' is 1, etc. There's a simple formula for that.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... let me give you a hint where to start... Read the file or string character by character not line by line.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Swapping strings in an array of strings
    By dannyzimbabwe in forum C Programming
    Replies: 3
    Last Post: 03-03-2009, 12:28 PM
  2. malloc() strings VS array strings
    By Kleid-0 in forum C Programming
    Replies: 5
    Last Post: 01-10-2005, 10:26 PM
  3. Table mapping Strings to Strings
    By johnmcg in forum C Programming
    Replies: 4
    Last Post: 09-05-2003, 11:04 AM
  4. converting c style strings to c++ strings
    By fbplayr78 in forum C++ Programming
    Replies: 6
    Last Post: 04-14-2003, 03:13 AM
  5. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM