Skip to main content
added 20 characters in body
Source Link
hotwebmatter
  • 3.1k
  • 2
  • 14
  • 30

\u0000 is ana character escape sequence for a NULL terminator, so you might want to rethink your while loops so that you don't go past the end of the string.

More generally, you should keep your code DRY -- in other words, Don't Repeat Yourself. 

Repetition like this is a sign of an inefficient algorithm:

  int n = 0;
  while ( s[n] != ' ' && s[n] != '\0')

{
    n++;
}

 int p = n;

 while (s[p] == ' ' && s[n] != '\0')
 {
      p++;
 }

 int q = p;

 while ( s[q] != ' ' && s[q] != '\0')

{
    q++;
}

There's absolutely nothing to be gained by declaring three integers for n, p, and q, then doing the exact same thing with them. You can combine and streamline to greatly improve your algorithm.

Think about this pseudo code instead:

int main(void)
{
    // prompt user for input (you are doing this correctly)
    string s = get_string();

    // if not NULL
    // print first character of string, no matter what
    // convert to uppercase if necessary

    // iterate through string, looking for spaces
    // instead of a while() loop, consider using a for() loop
    // you may find strlen(s) helpful with this approach!
    // if we find a space at s[n], print the character at s[n + 1]
    // convert to uppercase if necessary

    // finally, print the newline.
}

\u0000 is an escape sequence for a NULL terminator, so you might want to rethink your while loops so that you don't go past the end of the string.

More generally, you should keep your code DRY -- in other words, Don't Repeat Yourself. Repetition like this is a sign of an inefficient algorithm:

  int n = 0;
  while ( s[n] != ' ' && s[n] != '\0')

{
    n++;
}

 int p = n;

 while (s[p] == ' ' && s[n] != '\0')
 {
      p++;
 }

 int q = p;

 while ( s[q] != ' ' && s[q] != '\0')

{
    q++;
}

There's absolutely nothing to be gained by declaring three integers for n, p, and q, then doing the exact same thing with them. You can combine and streamline to greatly improve your algorithm.

Think about this pseudo code:

int main(void)
{
    // prompt user for input (you are doing this correctly)
    string s = get_string();

    // if not NULL
    // print first character of string, no matter what
    // convert to uppercase if necessary

    // iterate through string, looking for spaces
    // instead of a while() loop, consider using a for() loop
    // you may find strlen(s) helpful with this approach!
    // if we find a space at s[n], print the character at s[n + 1]
    // convert to uppercase if necessary

    // finally, print the newline.
}

\u0000 is a character escape sequence for a NULL terminator, so you might want to rethink your while loops so that you don't go past the end of the string.

More generally, you should keep your code DRY -- in other words, Don't Repeat Yourself. 

Repetition like this is a sign of an inefficient algorithm:

  int n = 0;
  while ( s[n] != ' ' && s[n] != '\0')

{
    n++;
}

 int p = n;

 while (s[p] == ' ' && s[n] != '\0')
 {
      p++;
 }

 int q = p;

 while ( s[q] != ' ' && s[q] != '\0')

{
    q++;
}

There's absolutely nothing to be gained by declaring three integers for n, p, and q, then doing the exact same thing with them. You can combine and streamline to greatly improve your algorithm.

Think about this pseudo code instead:

int main(void)
{
    // prompt user for input (you are doing this correctly)
    string s = get_string();

    // if not NULL
    // print first character of string, no matter what
    // convert to uppercase if necessary

    // iterate through string, looking for spaces
    // instead of a while() loop, consider using a for() loop
    // you may find strlen(s) helpful with this approach!
    // if we find a space at s[n], print the character at s[n + 1]
    // convert to uppercase if necessary

    // finally, print the newline.
}
Source Link
hotwebmatter
  • 3.1k
  • 2
  • 14
  • 30

\u0000 is an escape sequence for a NULL terminator, so you might want to rethink your while loops so that you don't go past the end of the string.

More generally, you should keep your code DRY -- in other words, Don't Repeat Yourself. Repetition like this is a sign of an inefficient algorithm:

  int n = 0;
  while ( s[n] != ' ' && s[n] != '\0')

{
    n++;
}

 int p = n;

 while (s[p] == ' ' && s[n] != '\0')
 {
      p++;
 }

 int q = p;

 while ( s[q] != ' ' && s[q] != '\0')

{
    q++;
}

There's absolutely nothing to be gained by declaring three integers for n, p, and q, then doing the exact same thing with them. You can combine and streamline to greatly improve your algorithm.

Think about this pseudo code:

int main(void)
{
    // prompt user for input (you are doing this correctly)
    string s = get_string();

    // if not NULL
    // print first character of string, no matter what
    // convert to uppercase if necessary

    // iterate through string, looking for spaces
    // instead of a while() loop, consider using a for() loop
    // you may find strlen(s) helpful with this approach!
    // if we find a space at s[n], print the character at s[n + 1]
    // convert to uppercase if necessary

    // finally, print the newline.
}