Thread: Character to array (Noobie Q)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    35

    Character to array (Noobie Q)

    It seems as if someone else on here is doing the exact same assignment as me...haha...well...I have some different problems.

    If you run this, you see that, after you enter the word, you have to hit <return> 10 times for the next "please enter your ## word" to come up. Its obviously effecting my results if you keep going through the program. (I don't know why this is). On top of that I don't want my words to exceed 9 characters, and I can't really test that yet because of my first bug.

    The code with the substitution of \n to \0 was given by our prof, because we obviously have to use strings in our program. I am not sure if I placed it in my program correctly however, it makes sence. For every time 'b' increases once, 'c' will increase 9 times, and each time assigning a character to "word[b][c]" until it reaches the new line character.

    (However we/nor I, have learned strings yet, so Im not trying to worry about that right now, Id just like to get the basic input and output down, before any sorting w/ strings).

    ex. of what code should do

    Enter ## word : FrOG
    etc...

    Unsorted code:
    fRoggoRf


    Code:
    #include <stdio.h>
    #include <string.h>
    
    char word[10][19];
    int a;
    int b;
    int c;
    int d;
    
    main (){
    
    printf("Enter words no longer than 9 characters\n");
    for (a=0; a<10; a++)
    {
    	if (a==0)
    		printf("Enter your 1st word:");
    	else if (a==1)
    		printf("Enter your 2nd word:");
    	else if (a==2)
    		printf("Enter your 3rd word:");
    	else
    		printf("Enter your &#37;dth word:", a+1);
    for (b=0; b<10; b++)
    	for (c=0; c<10; c++)
    	{
    		word[b][c]=fgetc(stdin);
    			if (word[b][c]=='\n')
    			{
    				word[b][c]='\0';
    				break;
    			}
    
    	if (c>=10)
    		printf("You have entered a word more than 9 characters\n");
                    /*goto A*/
    
    
    	}
    
    }
    printf("\n");
    printf("Unsorted Input: \n");
    printf("\n");
    for (b=0; b<10; b++)
    {
    	for(c=0; c<10; c++)
    	{
    		d=word[b][c];
    		if (d>=65 && d<=91)
    			d=d+32;
    		else if (d>=97 && d<=124)
    			d=d-32;
    		printf("%c",d);
    	}
    	for(c=10; c<=20; c++)
    	{
    		d=word[b][c];
    		if (d>=65 && d<=91)
    				d=d+32;
    		else if (d>=97 && d<=124)
    				d=d-32;
    		printf("%c",d);
    	}
    }
    return 0;
    }

    I like finding the errors, so if its a simple syntax error, just give me the line # and ill try to figure it out. But if its all mashed up, well then any adivce would be very helpful. Thank you.
    Last edited by Iconate; 02-27-2008 at 12:44 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character Array comparison
    By magda3227 in forum C Programming
    Replies: 7
    Last Post: 07-09-2008, 08:36 AM
  2. Replies: 7
    Last Post: 05-11-2008, 10:57 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  4. two dimensional character array
    By feuerraeder in forum C Programming
    Replies: 4
    Last Post: 11-22-2002, 08:59 AM
  5. Array of Character Arrays
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 02-09-2002, 06:07 PM