0

I don't think my code is that accurate for the purpose of this problem. The goal is to print and capitalise the first letters of a user's name. I'm having the most trouble finding out how to move around inside of the string once I discover a white space. I realise that the string should be recognised as an array of characters, and once I have the number of them I can iterate over each character and should be able to call up specific characters within that array. I have read online that this problem shouldn't require an array, argument count/ argument vector and I should be able to get by with a for loop.

What would the community suggest that I try? Perhaps using 'isspace' is the greatest issue in my code. I did try this:

if (s[i] == " ")

But that pulled up two errors that I couldn't work around.

Thanks, everyone.

#include <cs50.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>

int main(void)
{
    printf("Please provide first and last name:\n");
    string s = GetString();

    if (s != NULL)
    {
        for (int i = 0, n = strlen(s); i < n; i++)
// set i to 0, n represents the length of string s, and i < n, i++ will assign placeholders 
// i up through n.
        {
            if (isspace(s[i]))
            {
                printf("%c %c\n", toupper (s[i] + 1), toupper (s[i] - 1)); 
            }
        }
    }
}
2

You have 2 main problems. The first one is that s[i] is a character (typically an ASCII value). If you add/subtract 1 to/from that, this will do exactly that — adding/subtracting 1 to/from this value.

For example, if s[i] was 'a', that means that it stores the value 97. s[i] + 1 will be 98 which is corresponds to 'b' in the ASCII table.

What you wanna do is to manipulate the index, in this case the i.

The second problem is that you should be careful when manipulating the index as, for example, i + 1 is only within the array bounds (i.e., a valid array index in this example) if and only if i fells in a specified range. How to know the range? Well, we know that arrays are zero-based. Thinking about the maximum valid index for your array could help too.

4
  • I understand the mistake with trying to add an int to the ASCII equivalent int. That just won't do. What I need is a function that can call up the char within the string that comes before and after the space char. What I expected the for loop to do was assign all the values of 'i' to each char in the string, and that was what I wanted to add and subtract from, not the ASCII equivalent. I'm pretty certain that argv and argc are not going to be useful in this case. – Bradley Stone Mar 20 '15 at 14:58
  • Is it possible to use argv without declaring it in the place of "void" in the main function, but rather using it as a function by itself within a nested for loop? My biggest hurdle right now, I think, is to assign values to each char within my string. Because the string is an array, it should be possible to do this. – Bradley Stone Mar 20 '15 at 15:06
  • @BradleyStone I don't think argv and argc are relevant since you don't need any command-line arguments for this pset. The thing is that you could do the math inside the [] of s. So s[i + 1] is perfectly valid assuming that i + 1 is a valid array index for s. – Kareem Mar 21 '15 at 6:48
  • Kareem you are a wizard. Thank you once again. – Bradley Stone Mar 23 '15 at 7:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .