Calculating first and last character in an array

Page 1 of 1

9 Replies - 9671 Views - Last Post: 02 June 2011 - 10:52 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=234313&amp;s=1b65dbad9dcdbcbc4da4f56a1d4cc10c&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 Wannabehacker

Reputation: 0
• Posts: 59
• Joined: 31-August 10

Calculating first and last character in an array

Posted 02 June 2011 - 08:05 AM

Hello everyone,

I'm trying to get an input from the user for example like: "Tom", which is actually t = 20, o = 15 and m = 13. before I explain further I would like to add that I have assigned
the characters in the alphabetical order for example a = 1, b = 2 and...

My only problem is that how do I make the program to calculate the first and last character in the array, since I don't know what's the length of the word that user is going enter?
In this case it's 20 + 13.

BTW the code is mixed with c++ as I'm trying to translate it to c, please also tell me what's wrong with the first two printf?

```#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cctype>

using namespace std;

int main()
{
int vowelCount = 0;
int unsigVowel = 0;
int index = 0;
int total = 0;
char wordInput[28];

printf ("Please enter a word: ");
gets (wordInput);
printf ("-------------------");

for(int i = 0; i<strlen(wordInput);i++)
{
if(wordInput[i] == 'a' || wordInput[i] == 'A' || wordInput[i] == 'e' || wordInput[i] == 'E' ||
wordInput[i] == 'i' || wordInput[i] == 'I' || wordInput[i] == 'o' || wordInput[i] == 'O' ||
wordInput[i] == 'u' || wordInput[i] == 'U')
{
vowelCount++;
}
else
unsigVowel++;
}

printf ("\nThere are %d vowels in the %c \n", vowelCount, wordInput);
printf ("There are %d unsigned vowel in the %c \n", unsigVowel, wordInput);
//cout << "\nThere are " << vowelCount << " vowels in the " << wordInput << "\n";
//cout << "There are " << unsigVowel << " unsigned vowel in the " << wordInput << "\n";
cout << "There are " << strlen(wordInput) << " characters in the " << wordInput << "\n\n";

for( int index = 0; index < strlen(wordInput);index++)
{
if ( ! std::isspace(wordInput[index]))
{
cout << "The value of character at position " << wordInput[index] << " is: ";
cout << (int)(toupper(wordInput[index]) - 'A' + 1) <<"\n";
total += (int)(toupper(wordInput[index]) - 'A' + 1);
}
}

cout << "The total is: " << total << "\n";

/*
:(/>
*/
return 0;
}
```

Thank you

Is This A Good Question/Topic? 0

Replies To: Calculating first and last character in an array

#2 snoopy11

• Engineering ● Software

Reputation: 1460
• Posts: 4,726
• Joined: 20-March 10

Re: Calculating first and last character in an array

Posted 02 June 2011 - 08:49 AM

the first two printf's
should be

```printf ("\nThere are %d vowels in the word %s \n", vowelCount, wordInput);
printf ("There are %d unsigned vowel in the word %s \n", unsigVowel, wordInput);

```

lines 21 and 39 you are comparing a signed int to an unsigned int
i would suggest making i an unsigned int.

and on line 12 you declare index
but do nothing with it either remove it or do something with it.

If your array starts at 0 and ends with the length of the word it shouldnt be too hard to calculate the first and last letters of the word no matter if you dont know the length of the word.

This post has been edited by snoopy11: 02 June 2011 - 08:56 AM

#3 Wannabehacker

Reputation: 0
• Posts: 59
• Joined: 31-August 10

Re: Calculating first and last character in an array

Posted 02 June 2011 - 08:58 AM

Thanks for the reply, done (But wasn't necessary, because user must enter a character anyway) and done but on line 12 I'm already using the variable in the for loop, I had other plans for it, I wanted to be able to use it outside the loop but don't need that anymore.

Yes, finding the first one is easy but how to find the last element of the array?

This post has been edited by Wannabehacker: 02 June 2011 - 09:01 AM

#4 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: Calculating first and last character in an array

Posted 02 June 2011 - 09:10 AM

#1 you should not be using gets()!!! gets() is bad and can cause some major troubles because it makes no check to ensure the input is smaller than the buffer size.

see cin.getline() or getline()

next those headers should be:

#include <cstdio>
#include <cstring>

(of course you could also be using C++'s string class and that would be:

#include <string>

)

That said, since you are using character arrays (c-strings) you should know that they are all terminated with the char 0x00 (sometimes written '\0') -- so the char right before the '\0' char is the last char of the string.

#5 Wannabehacker

Reputation: 0
• Posts: 59
• Joined: 31-August 10

Re: Calculating first and last character in an array

Posted 02 June 2011 - 09:28 AM

NickDMax, on 02 June 2011 - 09:10 AM, said:

#1 you should not be using gets()!!! gets() is bad and can cause some major troubles because it makes no check to ensure the input is smaller than the buffer size.

see cin.getline() or getline()

next those headers should be:

#include <cstdio>
#include <cstring>

(of course you could also be using C++'s string class and that would be:

#include <string>

)

That said, since you are using character arrays (c-strings) you should know that they are all terminated with the char 0x00 (sometimes written '\0') -- so the char right before the '\0' char is the last char of the string.

Hello thanks for the reply, yes and that's why I had to use
```if(sizeof(wordInput) < strlen(wordInput))
```

to check for overstacking, Yes I would really like to use C++ but it's not allowed. Yes, that's why I used wordInput[28] which capacity is actually 27.

Thank you for your replies guys but any solution on how to get the last element's value?

This post has been edited by Wannabehacker: 02 June 2011 - 09:31 AM

#6 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: Calculating first and last character in an array

Posted 02 June 2011 - 09:40 AM

Quote

Yes I would really like to use C++ but it's not allowed.

Well I should warn you that your program is currently a C++ program.

Quote

```if(sizeof(wordInput) < strlen(wordInput))
```

and what did you think that is supposed to do? Once you have an access violation your program crashes, there is no chance to check it.

gets() suffers from a buffer overrun attack. That is you have a buffer of 28chars and the user pumps in 20k your program crashes because gets() will overrite everything past the end of the buffer -- that includes the stack, it may include your program code, it may just cause an access violation or it could (well in older OS's) overwrite system memory etc.

gets() is considered evil because YOU CAN'T STOP IT. once you use gets() you have opened yourself up to a malicious attack or a mistake by a user (hey I have pasted in huge blocks of data when I thought there was just a word or so in the clipboard).

#7 Wannabehacker

Reputation: 0
• Posts: 59
• Joined: 31-August 10

Re: Calculating first and last character in an array

Posted 02 June 2011 - 10:02 AM

Quote

Well I should warn you that your program is currently a C++ program.

Yes, it was, since the structure of the two language is the same actually and the difference is mostly is with input and output and libraries I wrote it with C++ and then It's C now. Hopefully the lecturer won't look at some things that I didn't change such as isspace ! He tends to look at printfs only! It's a bit difficult to work with CStyle string!

Quote

and what did you think that is supposed to do?

Oh yes I noticed after I posted this, but I was kinda hoping that no one would use more than 27 characters

Thank you that was very informative

This post has been edited by Wannabehacker: 02 June 2011 - 10:03 AM

#8 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

Re: Calculating first and last character in an array

Posted 02 June 2011 - 10:24 AM

I can't imagine how incompetent he'd have to be to not see that this is not C code.

After the char array is filled with meaningful values you can just use strlen() to get the last character.
```size_t length = strlen(buffer);
char last = buffer[length - 1];
```

Note however, that if buffer was filled with fgets() the last character will most probably be '\n'.

I'll reiterate - don't use gets(), there's a much safer fgets() function that you should look up in the docs (first link in Google).
You really shouldn't trust a user of your application in any way. Actually, start calling the person a "misuser" and always assume that the misuser will screw something up. Because someone will, and it'll be your fault if the program misbehaves instead of handling the situation properly.

This post has been edited by Xupicor: 02 June 2011 - 10:38 AM

#9 Wannabehacker

Reputation: 0
• Posts: 59
• Joined: 31-August 10

Re: Calculating first and last character in an array

Posted 02 June 2011 - 10:35 AM

Oh he would notice if I show him the code I posted here, but it's changed now.
Perfect, thank you very much also thanks for your advices! Thanks everyone!

#10 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

Re: Calculating first and last character in an array

Posted 02 June 2011 - 10:52 AM

Rather than using gets() which is bad, you can use scanf() or fgets() both of which allow you to specify a length:

```scanf("%28s", buffer);
fgets(buffer, 28, stdin);
```