Thread: Reading in string

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

    Reading in string

    This is my assignment: Write a 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.

    So far, I can read in a sentence and it counts the letters of the alphabet. I have copied the code for reading in a string from my lecture notes, but it doesn't seem to work and I'm not sure what I'm doing wrong.

    I am also not sure how to count the non-alphabetical characters.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int Pos, CountArray[26];
    char ch;
    const int MAXSIZE = 10;
    
    int main ()
    {
    
    	char line[MAXSIZE] = {'\0'};
    
    	printf("Enter string: ");
    	fgets(line, sizeof(line), stdin);
    	line[strlen(line) - 1] = '\0';
    
    	for(Pos=0;Pos<26;Pos++)
    	{
    	CountArray[Pos] = 0;
    	}
    	
    	printf("Enter a sentence, ending with a fullstop: ");
    	
    	ch=getchar();
    	while(ch != '.')
    	{
    		if(isalpha(ch))
    		{
    		ch = toupper(ch);
    		CountArray[ch-'A']++;
    	}
    					
    	ch = getchar ();
    	}
    	
    	for(Pos=0;Pos<26;Pos++)
    	{
    	printf("%c=", Pos+'A');
    	printf("%d: ", CountArray[Pos]);
    	}
    	
    	return(0);
    
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I have copied the code for reading in a string from my lecture notes, but it doesn't seem to work and I'm not sure what I'm doing wrong.
    Well for one thing you're trying to do scoop and poop coding rather than spending a few minutes to analyse the problem and work out a solution of your own.

    The programmer's most important skills are:
    1) Problem Analysis ... to break a problem down and understand it.
    2) Project Planning... to work out a solution once the problem is understood.
    3) Documentation Study... looking stuff up.

    "Writing code" is somewhere just below the top 10... and the last step in creating any project.

    Nowhere on that list is "copy random blobs of code into a perfectly good project".


    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <string.h>
    
    int Pos, CountArray[26];
    char ch;
    const int MAXSIZE = 10;
    
    int main ()   <--- it's int main ( void )
    {
    
    	char line[MAXSIZE] = {'\0'};
    
    	printf("Enter string: ");
    	fgets(line, sizeof(line), stdin);   <----- all this code is not used by your program.
    	line[strlen(line) - 1] = '\0';	
    
    for(Pos=0;Pos<26;Pos++)
    	{
    	CountArray[Pos] = 0;
    	}
    	
    	printf("Enter a sentence, ending with a fullstop: ");
    	
    	ch=getchar();
    	while(ch != '.')
    	{
    		if(isalpha(ch))
    		{
    		ch = toupper(ch);
    		CountArray[ch-'A']++;
    	}
    					
    	ch = getchar ();
    	}
    	
    	for(Pos=0;Pos<26;Pos++)
    	{
    	printf("%c=", Pos+'A');
    	printf("%d: ", CountArray[Pos]);
    	}
    	
    	return(0);
    
    }
    Really, what you need to do is sit down with pencil and paper and spend a few minutes ... become the process... what steps do you have to complete to solve this problem?

    Now that you actually *understand* the problem, you can begin working out the code.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    2
    There was nothing else really explaining the reading in string code, it just said 'if you need to read in a string, use this code'. I don't understand the code, so I don't really know what I should do with it.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Perhaps you copied this down wrong?

    Anyway, the fact that you don't understand the code is a major issue, because it is about as simple as it gets -- this is the day after "hello world" if you have never programmed before in your life.

    That said, maybe you should find some kind of C reference, online or in a book or something, and lookup the various standard functions used in that code. Try to write very short (5-10 line) programs that make use of them in a clear and straightforward way. This will help you to understand, and if you keep these you will have some code samples to refer to later on.

    printf() -- see also fprintf and sprintf
    fgets()
    getchar()
    isalpha()
    toupper()

    Most of those are very simple.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 10:29 PM
  2. Java: Is reading String by String possible?
    By alphaoide in forum Tech Board
    Replies: 5
    Last Post: 08-28-2004, 07:01 PM
  3. Reading in an int followed by a string
    By ChwanRen in forum C Programming
    Replies: 4
    Last Post: 05-07-2003, 03:09 PM
  4. reading a string!!!
    By condorx in forum C++ Programming
    Replies: 5
    Last Post: 12-07-2002, 11:41 AM
  5. Reading a string
    By LavaLamp27 in forum C++ Programming
    Replies: 2
    Last Post: 10-05-2001, 06:27 PM