12 Replies - 1222 Views - Last Post: 01 October 2012 - 08:11 PM Rate Topic: -----

#1 a_slow_descent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

how to ignore certain characters in an array

Posted 01 October 2012 - 09:13 AM

i am doing a project for school in c and i am about 98% done with it. the program has to check if the word is a palindrome. did that fine. then it needs to count the number of vowels. did that. then it needs to make sure the letter case doesn't matter. did that. then i need to count the words between length 10 and 15 and print all of them and also compute the average number of vowels per word. did that too. now here is where my problem lies. you need to ignore these five things ,.;?! they are only at the end of the words in the test file he is using. he told us so. so what i am tring to do is whenever it reads that there is one of the punctuation marks, change it to \0 so it is the end of that array. here is the code i thought that would work but made my entire program f up.

 while(scanf("%s",word)==1)
  {
    for(j=0;j<=MAX_LEN;j++)
      {
        if(word[j]==','||';'||'.'||'!'||'?')
          j='\0';
      }



it makes sense to me logically. scans the array (word) until it finds \0. increments to the max length which is 15 (given to us by teacher). if the variable at position j is one of those characters the make that character \0. what is wrong?

Is This A Good Question/Topic? 0
  • +

Replies To: how to ignore certain characters in an array

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 09:21 AM

How was word defined? Usually using <= in a for loop, when iterating thru arrays is a mistake. The following statement is incorrect:

Quote

scans the array (word) until it finds \0.

You loop is scanning through 16 characters, it is not stopping when it sees a '\0' character. You may want to consider a while loop instead of a for loop.

Lastly in your if statement:
if(word[j]==','||';'||'.'||'!'||'?')

For each item you need both sides of the equation:
if(word[j] == ',' || word[j] == ':')


Jim
Was This Post Helpful? 1
  • +
  • -

#3 a_slow_descent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 09:37 AM

Jim. i will show you my entire main. everything works perfect except for when i add in the part to try to ignore the five punctuation marks.

int main(void)
{
  char word[MAX_LEN];
  int i,j,k,x,vowel=0,length=0,numPals=0,getchar(),totalWords=0,three_vowel_min\
=0,arr[LEN_OF_WORDS]={0};
  float average_vowel=0;
  printf("write out some words: ");
  while(scanf("%s",word)==1)
  {
    for(j=0;j<=MAX_LEN;j++)
      {
        if(word[j]==','||';'||'.'||'!'||'?')
          j='\0';
      }
 if(is_palindrome(word))
  {
 numPals++;
  }
 x=vowel_count(word);
 totalWords++;
 vowel=x+vowel;

 if(x>=3)
   three_vowel_min++;
 for(i=10;i<=15;i++)
   {
     if(strlen(word)==i)
       arr[i-10]++;
   }


 }
  average_vowel=((float)vowel/(float)totalWords);
  printf("the number of palindromes is: %d\n",numPals);
  printf("the number of vowels is: %d\n",vowel);
  printf("the average number of vowels per word is: %f\n",average_vowel);
  printf("the number of words with at least 3 vowels: %d\n",three_vowel_min);
  for(i=10;i<=15;i++)
    printf("the number of words of length %d: %d\n", i,arr[i-10]);
  return 0;
}




i have no clue why or how the ignoring the punctuation marks works. like i said, what i wrote logically made sense to me but clearly i was wrong.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 09:43 AM

Quote

i have no clue why or how the ignoring the punctuation marks works.

Did you read all of my last post? I told you why you are not ignoring the punctuation marks.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3160
  • View blog
  • Posts: 9,532
  • Joined: 05-May 12

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 10:00 AM

View Posta_slow_descent, on 01 October 2012 - 09:13 AM, said:

so what i am tring to do is whenever it reads that there is one of the punctuation marks, change it to \0 so it is the end of that array. here is the code i thought that would work but made my entire program f up.

 while(scanf("%s",word)==1)
  {
    for(j=0;j<=MAX_LEN;j++)
      {
        if(word[j]==','||';'||'.'||'!'||'?')
          j='\0';
      }



it makes sense to me logically. scans the array (word) until it finds \0. increments to the max length which is 15 (given to us by teacher). if the variable at position j is one of those characters the make that character \0. what is wrong?


jimblumberg is quite correct about your for loop condition and your if condition being incorrect.

Additionally, you are not setting the punctuation marks that you find to '\0'. All you are doing on line 6, is resetting your indexer j back to zero since '\0' == 0.
Was This Post Helpful? 1
  • +
  • -

#6 a_slow_descent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 10:02 AM

Quote

Did you read all of my last post? I told you why you are not ignoring the punctuation marks.

Jim


yeah i read it. i changed to look like this:

 while(scanf("%s",word)==1)
  {
    for(j=0;j<=MAX_LEN;j++)
      {
        if(word[j]==','||word[j]==';'||word[j]=='.'||word[j]=='!'||word[j]=='?')
          j='\0';
      }



you said to do another while loop instead of the for loop? the first while loop skips the leading white space characters then reads the array until it gets to another white space character then auto puts \0 and the end of the array. im lost.
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 10:11 AM

Your for loop is incorrect! First if you defined your array as words[15] using the <= operator will access your array out of bounds. Arrays in C start at 0 and stop at size - 1, 14 in this case.

I suggest changing it to a while loop, to loop thru the string until it finds the end of the string. The length of your string is not necessarily 15 characters. Remember the length of a string in C is the number of characters before the terminating end of string character ('\0').
int j = 0;
while(words[j] != '\0')
{
   // Check if the character is a punctuation character.
   // If it is then replace the character, not the index.


   // Increment the character.
   ++j;
}



Jim
Was This Post Helpful? 1
  • +
  • -

#8 a_slow_descent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 10:28 AM

Jim. I love you. thank you for all the help man. it worked when i did this:

 while(scanf("%s",word)==1)
  {
    while(word[j]!='\0')
      {
        if(word[j]==','||word[j]==';'||word[j]=='.'||word[j]=='!'||word[j]=='?')
          word[j]='\0';
          ++j;
      }



it makes perfect sense too. thank you for explaining and not just giving me the answer. works perfectly. example of output:

write out some words: I am racecar? nnnnooonnnn eeeeeiiieeeee
the number of palindromes is: 3
the number of vowels is: 21
the average number of vowels per word is: 4.200000
the number of words with at least 3 vowels: 3
the number of words of length 10: 0
the number of words of length 11: 1
the number of words of length 12: 0
the number of words of length 13: 1
the number of words of length 14: 0
the number of words of length 15: 0

before it would say that racecar? was not a palindrome. thank you again.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 10:33 AM

Don't forget to fix the last for loop as well.
 for(i=10;i<=15;i++)
   {
     if(strlen(word)==i)
       arr[i-10]++;
   }


 }
  average_vowel=((float)vowel/(float)totalWords);


Don't use operator<= when dealing with array indexs, just use the operator<. Using operator<= will usually lead to accessing your array out of bounds.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 a_slow_descent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 04:47 PM

so i am still having one problem with this. when i put input of: noon? noon noon noon?
it only reads it as 3 palindromes. still says 8 vowels and two vowels per word. it counts the first word with a punctuation mark and doesn't for the following ones. why could this be?
Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 04:53 PM

I suggest you try running this program through your debugger. Set a breakpoint at the start of your program and watch how your variables change throughout your program.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 a_slow_descent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 01-October 12

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 05:53 PM

so i did the debugging and its not going as i plan. when it does scanf i enter words. i put: noon racecar? noon?
it went through the while loop until j was 4 then it went down to my is_palindrome function. did the rest of the stuff in the main then back to the while loop. i thought it would go back to zero once it came back to here but it said j was 5 and then when it got to 8, it went to my is_palindrome. then the rest of the functions. then back to the while loop. it didn't even do anything. it just went right to the is_palindrome function. does this make sense to you?
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 3845
  • View blog
  • Posts: 11,735
  • Joined: 25-December 09

Re: how to ignore certain characters in an array

Posted 01 October 2012 - 08:11 PM

Please show exactly what you entered. And post your current code. Please show the complete code.

Do you know that scanf() stops processing the input when it encounters white space characters?

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1