Hey all im new to the site and looking around i can tell i came to the rite place. Now for my question.

Im suppose to take a poem with punctuations and blank lines in it and reprint the poem one word per line. The reprint cant have punctuations or blank lines in it. I was able to do that so far, but the program does not recognize capital letters and I really dont know how to get it to print one whole word per line. Also I cant use any string handling functions in my code only getchar and putchar. So hopefully what i have writing down so far is still useable.

Here is a copy of my code for far:

#include <ctype.h>
#include <stdio.h>
#define _CRT_SECURE_NO_DEPRECATE

/*Function prototype*/
char NewPoem (char);


int main ()
{
	char iochar;
	

	printf("Enter the poem for format conversion.\n");
	
	scanf("%c", &iochar);

	printf("\nYou're new poem : %c\n", iochar);

	/*Call the function*/
	NewPoem (iochar);
		
}



/*Function to handle poem*/
char NewPoem (char iochar)
{

	while ( ( iochar = getchar() ) != EOF )
	{
		if ( ( 'A' <= iochar ) && ( 'Z' <= iochar) ) 
		{
			putchar (iochar);
		}

		else if ( ( 'a' <= iochar ) && ( 'z' <= iochar ) )
		{
			putchar (iochar);
		}
	}
}

I have tried to make another function to get the program to read the length of the word, but im not sure if it was even correct in anyway since the program just kept printing the output as if it was just the code I printed above.

what I want to know is:

1) What do I need in a function to take a whole word?
2) How do I print each word in a new line?

1) What do I need in a function to take a whole word?

using getchar() , you're gonna have to put it in a loop, like this:

while((c=getchar())!=EOF)

and save it to an array to form the complete word.

2) How do I print each word in a new line?

putchar('\n')

after each word.

Hey thx for the reply bro im glad for the help. I just have some more questions if you dont mind.

Your answer to 1;
As you can see if you looked at my code I use a similar loop to make it read the characters. Do i have to delete what I did or just make a new function with the same loop? I am really lost as to how to get it going after said while loop. Should I make a for loop to go through the characters? If so how can I set it to take the poem word by word?

you answer to 2;
Can I put that in my newpoem function?

What's the purpose of your program? Is it just to read some characters from the console and print it out after changing its case(i.e 'a' if 'A' and 'Z' if 'z')?

As you can see if you looked at my code I use a similar loop to make it read the characters.

yeah, I see that.
But you said you can only use getchar() and putchar() but you have also used printf and scanf in your code.I'm little confused here.It would be easier if you provide the answers to these questions:
1.Are you trying to take input from console or a file.
2.You want to take input a word at a time or a whole sentence or even paragraph(s).
3.Are you trying to change the case of the characters? Lower to upper or upper to lower or both?
4.And what's with the "poem" thing?

Can I put that in my newpoem function?

Yeah go ahed. No one's gonna stop you.

I described the purpose somewhat in the first post. I have a .txt file which has a poem writing in it. im suppose to input that poem into the program and get it to output the poem word for word. Each word in a new line without the the punctuations or spaces that are in the poem.

Im suppose to run the program through the command prompt as so:

lab2a.exe<data.txt>output.txt

The problem with the code that I have written so far is that it only reads each letter and not each word, also it doesnt read capital letters. And like I stated before I cant use any sting handling functions other than getchar and putchar. Correct me if Im wrong but isnt an array such a function?

I described the purpose somewhat in the first post. I have a .txt file which has a poem writing in it. im suppose to input that poem into the program and get it to output the poem word for word. Each word in a new line without the the punctuations or spaces that are in the poem.

Im suppose to run the program through the command prompt as so:

lab2a.exe<data.txt>output.txt

The problem with the code that I have written so far is that it only reads each letter and not each word, also it doesnt read capital letters. And like I stated before I cant use any sting handling functions other than getchar and putchar. Correct me if Im wrong but isnt an array such a function?

I hope this will help.

#include <stdio.h>
#include <ctype.h>
int main ()
{
	int c;
	printf("enter the word for format conversion:\n");
	while ((c=getchar())!=EOF){
		if(isalpha(c))
			putchar(c);
		else if(isspace(c))
			putchar('\n');
	}
	return 0;
}

thx for your reply. Im afraid i cant use isalpha or isspace in my code so i would have to write my own functions to use them like so:

int my_isalpha(char ch)
{
if (((ch>='A') && (ch<='Z')) || ((ch>='a') && (ch<='z')))
return 1;
}

This is an example my teacher gave us.

Do you think if I write your above code in that format would it work?

PS: Your code works great except for the spaces between the title of the poem and the poem itself. There are suppose to be no blank lines between words. so If i am able to add the my_isalpha function then how do I avoid this gap between words?

Do you think if I write your above code in that format would it work?

To find that out, you have to try it. and yes I forgot to mention

Correct me if Im wrong but isnt an array such a function?

Array is not a function, it's just a data type which itself is a collection of a particular data type (integers,chars,float,double etc.) and is also used for other purposes apart from handling strings.

I modded your code to this:

#include <stdio.h>
    #include <ctype.h>
    int main ()
    {
    int ch;
    
	printf("enter the word for format conversion:\n");
   
	while ((ch=getchar())!=EOF){
    
		if( ( ( ch >= 'A' ) && ( ch <= 'Z' ) ) || ( (ch >= 'a' ) && ( ch <= 'z' ) ) )
    
		putchar(ch);
    
		else if ( ( ch = ' ') || ( ch = '\t' ) || ( ch = '\n' ) )
    
			putchar('\n');
    }
    return 0;
    }

This is working, except for one thing. There are now more blank spaces in the output. ill give you an example.

This is the poem:
The Parsnip

The parsnip, children, I repeat,
Is simply an anemic beet.
Some people call the persnip edible;
Myself, I find this claim incredible.

this is the output with your code:

The
Parsnip

The
parsnip
children
I
repeat
Is
simply
an
anemic
beet
Some
people
call
the
persnip
edible
Myself
I
find
this
claim
incredible

this is the output for the modded one:

The
Parsnip

The
parsnip

children

I
repeat

Is
simply
an
anemic
beet

Some
people
call
the
persnip
edible

Myself

I
find
this
claim
incredible

How can I avoid the blank lines?

Remove
ch == ' '
from the else if portion of the code.

Unless a char has been printed first.

So you need a counter that is reset whenever a newline is printed. If the counter is zero still when it finds a space, don't have it print the newline char. Otherwise print the newline, and reset the counter.

thx for your reply adak. The problem isnt the the spaces between the words, its characters like "; , ." that are causing that gaps in the output. I tried adding
"ch = ';'" to my code but it didnt work what else can i do?

OK, you need to think about what you need. getchar() inputs a single character. putchar() outputs a single character.

When you input a character,
1) if it's a letter, output it.
2) if it's not a letter,
2a) if it's the first non-letter, output a \n
2b) if it's not the first non-letter, output nothing

You need to keep a counter for the non-letter characters. I leave it to you to figure out how to do it.

WaltP has said it better than I did.

If you work through it by hand a few times, you'll see the pattern. For some input, you do nothing - remember that.

adak can you give me an example of what the counter would look like?

Just an int or boolean variable. Set it to 0.

When you put the first char of a word out, you set the variable to 1.

And follow the logic that WaltP has laid out.

Give that a try. I can't say it any clearer than WaltP has, in his above post.

Whenever you're stumped understanding the logic, pull out the paper and pencil, and work it through by hand - use scrabble letters or bits of paper labeled with a letter, to make it a more concrete example to work through.

You may not get it the first time you try, but don't quit. That homo sapien brain of yours will knock off some rust, make a few connections, and pretty soon, you'll see the pattern, and from that, be able to derive the logic you need.

This is an example

int nlCounter;
while ((ch=getchar())!=EOF){    
	if( ( ( ch >= 'A' ) && ( ch <= 'Z' ) ) || ( (ch >= 'a' ) && ( ch <= 'z' ) ) ){
          	putchar(ch);
                  nlCounter=0;
                  }
         else if ( ( ch = ' ') || ( ch = '\t' ) || ( ch = '\n' ) )   
                  if(nlCounter==0){      /*If the previous letter was a \n, skip this*/
		putchar('\n');
                  nlCounter=1;
                  }
         }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.