11 Replies - 2272 Views - Last Post: 16 February 2009 - 10:31 PM Rate Topic: -----

#1 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Check Puncuation mark at the end of a string

Posted 16 February 2009 - 11:27 AM

I am working on a Pig latin translator for class, and right now it's working ok except for the fact that if someone enters a question mark or a n exclamation point, it will print a period in its place. It's not so much that I care what it prints at the end of the sentence, but I think that it will show some effort to try and at least have my program figure out what was on the end and print it out. I've at least figured out this much:

...
token = strtok (NULL, "' \n'.?!");
}//End while

printf("%s", token);



The only problem is that it prints a null in my output. How can I take the NULL after the strtok and properly make that a string that I can print out on the line below. Basically I want to know how I can have my program print out whatever it striped from the string with that last strtok. Any help on this is appreciated. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Check Puncuation mark at the end of a string

#2 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 11:41 AM

Oh yeah I realized I should have mentioned that the strtok was in a while loop.

I already tried declaring a small string char puncStr [3]; right above the code and using that in place of NULL, but it threw a runtime error, I tried declaring it outside of the loop, again variable puncStr is being used without being declared. I've tried several variations of this. Please point me in the right direction on this one. Thanks
Was This Post Helpful? 0
  • +
  • -

#3 bszmyd  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 11:44 AM

Are you guaranteed that the punctuation mark is at the end of the string or do you accept multiple sentence strings?
Was This Post Helpful? 0
  • +
  • -

#4 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 11:50 AM

Let me say this much, once someone hits the enter key, it goes on to the next part of the program, so as far as I'm concerned only one sentence will be entered. Anyways, the instructions in my Book say: "Your program will read a sentence using fgets . It is to parse .... "

So I'm assuming my teacher will only test with a sentence.
Was This Post Helpful? 0
  • +
  • -

#5 bszmyd  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 15
  • View blog
  • Posts: 104
  • Joined: 09-February 09

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 01:22 PM

View Postwebmin, on 16 Feb, 2009 - 10:50 AM, said:

So I'm assuming my teacher will only test with a sentence.

I don't know what the rest of your code looks like, but it seems to me that what you have should work except in the case that you don't have a punctuation mark which you could handle by just checking token for NULL before printing it:
if ( token != NULL ) printf("%s", token);

Or maybe i'm not fully understanding your problem, it's a little tough without the rest of the code.
Was This Post Helpful? 0
  • +
  • -

#6 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 04:32 PM

The only thing I can think of is writing your own tokenization routine.
Was This Post Helpful? 0
  • +
  • -

#7 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 06:47 PM

okay, here's the section of code that I have for this. If this is not enough, please let me know.

char *tokenize (char *tempStr, int vowel)
{
	char *token;
	int counter = 0;

	token = strtok (tempStr, " ");

	while (token)
	{
		char tokenWord[WLENGTH];
		strcpy (tokenWord, token);
		
		counter++;
		vowel = strcspn (tokenWord, "AEIOUaeiou");
		translate (tokenWord, vowel);
		token = strtok (NULL, " '\n'.?!");
	}
	printf("%s", token);

	return token;
}


Was This Post Helpful? 0
  • +
  • -

#8 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 07:30 PM

What exactly is that code doing?
Was This Post Helpful? 0
  • +
  • -

#9 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 07:38 PM

It is breaking each part of the string into separate words based on where the spaces are at. The last bit of code is; that this the token = strtok (NULL, " '\n'.?!"); is taking any punctuation and stripping it so to speak and placing it in a NULL pointer. What I would like to do is instead of putting that in NULL, I would like to store that value in a variable of sorts that can be used in the printf statement just outside of the loop.

I wonder, maybe I was going about this the wrong way by trying to put it into a string. Can I use this strtok to store that value in a char varialbe? Or is there another usable function that would do that?

Thanks

Edit: I tried this
char *punc
...


token = strtok (punc, " '\n'.!?");
}//End while

printf("%s", token);



gave me a runtime error and said that punc was being used without being defined.

This post has been edited by webmin: 16 February 2009 - 07:44 PM

Was This Post Helpful? 0
  • +
  • -

#10 webmin  Icon User is offline

  • D.I.C Regular

Reputation: 4
  • View blog
  • Posts: 256
  • Joined: 21-May 08

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 09:48 PM

well I answered my own question on this one, and I will explain what I did:

First here is my revised code on this section:
char *tokenize (char *tempStr, int vowel)
{
	char *token;
	char *punc;
	char puncVal;
	int counter = 0;

	//Check what punctuation mark is used at the end of the sentence
	if (punc = strchr (tempStr, '.'))
		puncVal = *punc;
	else if (punc = strchr (tempStr, '?'))
		puncVal = *punc;
	else if (punc = strchr (tempStr, '!'))
		puncVal = *punc;

	token = strtok (tempStr, " ");

	while (token)
	{
		char tokenWord[WLENGTH];
		strcpy (tokenWord, token);
		
		counter++;
		vowel = strcspn (tokenWord, "AEIOUaeiou");
		translate (tokenWord, vowel);
		token = strtok (NULL, " '\n'.?!");
	}
	printf("\b%c", puncVal); //Print that value here

	return token;
}



First as you can see I have added two variables char *punc & char puncVal. Then from there I used if else statements in conjunction with strchr to check for each of the possible punctuation marks. If it found one, then it sets punc equal to puncVal which is later printed after the loop after each word has been tokenized. Note that I did this on my original input, so that I knew nothing had been messed up by it being tokenized. Well, hope this helps anyone who needs it.
Was This Post Helpful? 0
  • +
  • -

#11 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 10:31 PM

I rewrote my old Pig Latin translator program. In my program, getNextWord is the tokenization routine:

///////////////////////////////////////////////////////////////////////
// Program: Pig Latin Translator 2
//
// File: piglatin2.c
//
// Purpose: Translates an input string to Pig Latin
//
// Author: Brenton Saunders <brentonsaunders@yahoo.com>
//
// Date: 17 Feb 2009
//
// Revision History: N/A
//
// Bugs: None
//
// Notes: This program treats contractions (words containing
//		apostrophes) as two seperate words, so "can't"
//		becomes "ancay'tay" and not "antcay". It shouldn't
//		be much of a problem, though
///////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

void convertToPigLatin(char *);
int beginsWithVowel(const char *);
int hasVowel(const char *);
char *rotateWord(char *);
int getNextWord(char *, char *, unsigned int);

int main()
{
	char string[256];

	puts("Enter a string to be translated to Pig Latin:");

	gets(string);

	puts("Your string in Pig Latin:");

	convertToPigLatin(string);

	return 0;
}

///////////////////////////////////////////////////////////////////////
// Function: void convertToPigLatin(char *string)
//
// Purpose: Translates "string" to Pig Latin and prints it to standard
//		  output
//
// Return Value: None (void)
///////////////////////////////////////////////////////////////////////
void convertToPigLatin(char *string)
{
	char word[64]; // Hopefully, no word is longer than 64 chars
	int moreWords;

	moreWords = getNextWord(string, word, 64); // Get the first word of the string
	
	while(moreWords) { // Are there any more words?
		// If the word begins with a vowel, we add "yay" to it
		if(beginsWithVowel(word))
			printf("%syay", word);
		else {
			// If it doesn't begin with a vowel, we rotate it until it does
			// If the word doesn't have a vowel in it, just leave it alone
			if(hasVowel(word)) {
				while(!beginsWithVowel(word))
					rotateWord(word);
			}

			printf("%say", word); // Print the Pig Latin-ized word
		}

		moreWords = getNextWord(NULL, word, 64); // Get the next word in the string, if any
	}

	printf("\n");
}

///////////////////////////////////////////////////////////////////////
// Function: int beginsWithVowel(const char *string)
//
// Purpose: Determines if the first character of "string" is a vowel
//
// Return Value: 1 if the specified string begins with a vowel;
//			   0 otherwise
///////////////////////////////////////////////////////////////////////
int beginsWithVowel(const char *string)
{
	// Is the first character an "A", "E", "I",
	// "O", or "U"?
	if(*string == 'a' || *string == 'A' ||
	   *string == 'e' || *string == 'E' ||
	   *string == 'i' || *string == 'I' ||
	   *string == 'o' || *string == 'O' ||
	   *string == 'u' || *string == 'U') // "Y" is a tricky one, so just forget about it
	   return 1;

	return 0;
}

///////////////////////////////////////////////////////////////////////
// Function: int hasVowel(const char *string)
//
// Purpose: Determines if "string" contains a vowel
//
// Return Value: 1 if the specified string contains a vowel;
//			   0 otherwise
///////////////////////////////////////////////////////////////////////
int hasVowel(const char *string)
{
	if(strpbrk(string, "aeiouAEIOU")) // Does the string contain a vowel?
		return 1;

	return 0;
}

///////////////////////////////////////////////////////////////////////
// Function: char *rotateWord(char *string)
//
// Purpose: Rotates "string"--moves the first character to the end of the
//		  string so that the second character becomes the first, the
//		  third becomes the second, etc.
//
// Return Value: A pointer to the rotated string;
//			   NULL if "string" is an invalid pointer
///////////////////////////////////////////////////////////////////////
char *rotateWord(char *string)
{
	char firstChar;
	int cap;
	int len;

	if(!string)
		return NULL;

	firstChar = *string;

	cap = isupper(firstChar); // Is the first character uppercase?

	len = strlen(string);

	// Shift the string to the left
	strcpy(string, string + 1);

	// Add the original first character to the last
	// position in the string
	string[len - 1] = tolower(firstChar);

	// Capitalize the string if the original
	// first letter was uppercase
	if(cap)
		*string = toupper(*string);

	return string;
}

///////////////////////////////////////////////////////////////////////
// Function: int getNextWord(char *string, char *word,
//						   unsigned int len)
//
// Purpose: Gets the next word of "string" and stores it in the buffer
//		  pointed to by "word"
//		  If "string" is NULL, getNextWord gets the next word of the
//		  string that was used in the last call of the function
//		  "len" specifies the maximum number of characters to copy
//		  into "word" and must be nonnegative
//
// Return Value: 1 if there are more words in the string;
//			   0 if there are no more words, if no input string was
//			   provided, or if "word" is an invalid pointer
//
// Notes: A word in this context is a sequence of letters (a-z, A-Z).
//		All other characters preceding a word, including digits,
//		punctuation marks, and whitespace, are ignored and printed to
//		the screen
///////////////////////////////////////////////////////////////////////
int getNextWord(char *string, char *word, unsigned int len)
{
	static char *stringPtr = NULL;
	unsigned int i = 0;

	if(string)
		stringPtr = string;

	if(!string && !stringPtr) // No string was provided
		return 0;

	if(!word) // Word buffer is an invalid pointer
		return 0;

	if(!*stringPtr)  // There are no more characters left in the string
		return 0; // No more words

	// We're looking for a word, so let's skip all whitespace,
	// punctuation marks, and digits
	// Just print those guys to the screen
	// It's not that I have anything against punctuation marks; I mean,
	// I like them. It's just that they're NOT WORDS
	while(*stringPtr &&
		  (isspace(*stringPtr) ||
		   ispunct(*stringPtr) ||
		   isdigit(*stringPtr))) {
		// Don't forget about them, though
		// Print them out so the sentence remains
		// syntactically correct (that is, if it was to begin with)
		printf("%c", *stringPtr);
		stringPtr ++;
	}

	if(!*stringPtr) // There are no more characters left in the string
		return 0; // No more words

	// Empty the word buffer
	memset(word, '\0', len);

	// Now that we've skipped all unwanted characters, let's copy all
	// the characters we do want into the word buffer. We only want
	// letters, so we'll keep copying into the word buffer until
	// we find a non-letter character
	while(*stringPtr &&
		  isalpha(*stringPtr) && // Only letters
		  i < len) {
		*word = *stringPtr;
		word ++;
		stringPtr ++;
		i ++;
	}

	return 1; // Return 1, meaning we have a word
}

This post has been edited by bsaunders: 16 February 2009 - 10:43 PM

Was This Post Helpful? 1
  • +
  • -

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Check Puncuation mark at the end of a string

Posted 16 February 2009 - 10:31 PM

For character comparisons have a look at cctype (ctype.h)
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1