0

I've been trying to solve the problems in study.cs50. I know for myspace I need to loop through the input string and stop at spaces, before finally stopping at strlen(input string), (and I'm not doing a good job of that either) but how to properly loop through each substring bounded by the spaces is not easy to understand. I wonder if anyone would like to review my code, tell me where I'm off?

int main(int argc, char *argv[])
{
char input[2048], output[2048], i;
int count = 0;
printf("Give us a string: ");
while (i != '\n')
{
    i = getchar();
    input[count] = i;   //adding a character to each element of the array.
    count++; 
}
input[strlen(input) - 1] = '\0';    //add a null terminator, get rid of the last '\n'.
int j = 0;
int len = strlen(input);
char words[] = {0};
//loop thats going to alternate character cases
//for each character in the input string passed in;
for (int i = 0; i < len; i++)
{
    //if the string array element is not a space, nor a NULL terminator,
    // and if j is less than or equal to the length of the entire string;
    // ("j" is for watching to see we dont get to the null terminator, I think)
    // might not be necessary nor useful.
    if (input[i] != ' ' && j <= len)
    {
         //printf("%c", input[i]);
        //store the character in our words array
        words[i] = input[i];
    }else {
        //printf("%c", ' ');
        words[i] = ' ';
    }

//converting odd numbered items into uppercase
for (int j = 0, n = strlen(words); j < n; j = j + 2)
{
    words[j] = toupper(words[j]);
} 
 printf("%s\n", words); 

return 0;
}

Problem is, the output is not the same as the example. My output is:

   ./myspace 
  Give us a string: thanks for the add
  ThAnKs fOr tHe aDd

Example output is: ThAnKs FoR tHe AdD

1

okay, first, the problem statement never mentioned anything about the maximum length the input can be. you are limiting that to 2047 characters + the null character, plus, you're never ensuring that the user will provide a string that is at max is of that length which makes your code vulnerable to buffer overflow attacks.

you could have used the CS50 library's function GetString to get input from the user and avoid this vulnerability.

another thing is that it's probably not the most efficient approach if you are calculating the length of the input again and again by calling strlen.

also arrays in C have fixed lengths. that is, once an array length is determined, it can never change. you are declaring words to be of length 1 because you are initializing it with a 1-element long list initializer. see Arrays for more on how you can declare an array of a specified length!

you also wanna consider to add a null character in the end in order to terminate your string!

you could also make use of the set of standard functions that check if a given character is an alphabetical, a space, or something else. man isalpha for more!

and lastly, I'm not seeing you altering the letters' cases. to do so, you may need a flag variable to hold an indication of the current case, use it to determine the case of the current character and alter its value as necessary.

| improve this answer | |
  • I'm trying to first get each space separated substring alone first, and once I have, to try and loop through each of them individually. – SinaOwolabi Dec 22 '15 at 18:57
  • @Sina I don't think that's necessary! – Kareem Dec 22 '15 at 19:21
  • I added the capitalization loop, the problem I have is it doesn't follow the example output. – SinaOwolabi Dec 23 '15 at 1:38
  • @Sina I see you are changing the flag when there is a space while you shouldn't. – Kareem Dec 23 '15 at 9:01
  • Which is exactly my problem, I do not know how to avoid the spaces. I would be grateful for any pointers. – SinaOwolabi Jan 3 '16 at 13:43

You must log in to answer this question.

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