0

I have encountered some problems with pset2 initials more comfortable. The output I get is correct, but check50 shows some error.Can`t find problem. Maybe someone has some hints what is wrong here?

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

int main(void)
{
int k = 0;
string name=get_string();
if (name != NULL)
{
for( int i = 0, n = strlen(name) ; i < n; i++)
    {
        if (name[k] != ' ' && name[k] != '\0')
            {
                printf("%c", toupper(name[k]));
                while (name[k] !=' ' && name[k] != '\0')
                {
                    k++;
                }
            }
        else
            {
                k++;
            }
    }
    printf("\n");
}
}

This is check50 output:

initials.c exists
:) initials.c compiles
:) outputs "MB" for "Milo Banana"
:) outputs "MB" for "milo banana"
:) outputs "MB" for " milo  banana "
:) outputs "MB" for "  milo banana  "
:( outputs "RTB" for "   robert   thomas bowden"
\ expected output, but not "RTB\u0002\n"
:( outputs "RTB" for "Robert Thomas Bowden"
\ expected output, but not "RTBá\n"
:) outputs "R" for "ROB"
:( outputs "RTB" for "Robert thomas Bowden"
\ expected output, but not "RTBá\n"
1

No, the output is not correct, not in all cases. Look closely at how the code executes. The outer for loop appears to be intended to count the letters consumed in the name string, but because of the inner loops, i is actually counting the number of spaces. The inner loop is consuming the letters, counting them with k.

Essentially, the interaction of the for loop and the while loop is causing the code to go beyond the end of the string and processing garbage data in memory. You should try walking through the code with pencil and paper.

It would be far easier to print the first letter as upper case and then use the for loop to find the char following each space, and remove the while loop entirely.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

5
  • I still dont get it :) So, I shouldnt use while loop, but instead of, I may use for loop? But still Im gonna do one more nesting and Im already looping through characters in the first for loop.I ve got really confused here already and I feel its something simple, but I don`t get it.. May 3 '17 at 20:54
  • Why do you think you need a nested loop?
    – Cliff B
    May 3 '17 at 20:56
  • So, my logic here is that after user gives me an input, program should loop through characters, checking whether first character is a space or initial. If found, printing first initial and after that, by my logic one more loop(nested) should join the party - for checking further characters for space. That`s why I thought I need nested loop May 3 '17 at 21:17
  • Why does checking the first character need to be part of a loop? Also, loops don't have to start at 0 (or 1, or 42 or any other number).
    – Cliff B
    May 4 '17 at 15:54
  • Thank you, I got it now :) I`m overthinking
    – Karlaaz
    May 4 '17 at 19:39

You must log in to answer this question.

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