0

I had written the code like this but I am getting the gap between the two characters of output. How to remove this spaces.

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

int main(void)

{
    int s[];
    // takes input from user as their name
    int name = get_string();
    if (name != NULL)
    {
        for (int i = 0; i <= strlen(name))
        {
            s[i] = name[i];
        }
    }
    printf ("%c"toupper(s[0]));
}
1
  • Does not look like valid C. I used a state machine, initially ready to print the first alphabetical character it encounters. On printing that one, it would go into ignore mode, and be reset to print-next-alphabetical-character only on the next space. But with small adjustments, the while loops from your previous code would work, too. I forgot that NULL check again in my own code, I should add this as soon as I use get_string.
    – Blauelf
    Feb 28, 2017 at 9:29

1 Answer 1

0

When I run your code in C, I get many errors. However, these errors are pretty straight-forward so you'll be able to solve them easily. I am concerned mainly about the logistics of your code.

I can understand what you are doing with your code - asking for a string, going through a for-loop, setting a new array. But there is a major problem in your code, and it lies in your for-loop.

Okay, so your for-loop has a great main idea - get a new array, set name[i] to s[i], all of that. And, if executed properly, your code could produce the correct results. However, in this case, your code doesn't produce the proper results. Why is this? Well, initials.c wants you to print out the initials of a person's name in capital, or uppercase letters. Does your for-loop print out capital letters? You are only setting s[i] to name[i], but the only use of toupper() is on the first letter. You might want to do something like name[i] = toupper(name[i]).

In addition, how do you know that the letter you are printing is alphabetical? In the more comfortable version of initials.c, there could be one or more spaces in front of and in between each name. How do you know that you aren't printing the spaces? It might be useful to use an if statement to check whether or not name[i] is a space. It might also be useful to check if the letter after name[i] is a space.

You might want to go back through your code line-by-line and review your thinking process throughout the program. If you fix the errors I stated above - as well as the terminal errors - your code will work beautifully.

If this answers your question, hit the green checkmark! If you still have errors, comment below. :-)

4
  • By the way I had written another program for this one. Thanks for your help
    – Rahul
    Mar 1, 2017 at 13:59
  • No problem. Did this solve all your errors? Mar 1, 2017 at 14:20
  • yeah but one is remaining that is I am getting spaces between two characters of output and for that I had thought to store my output as an array then print only the characters without spaces
    – Rahul
    Mar 3, 2017 at 6:53
  • That sounds like a nice idea. Let me know if it works so I can improve upon my answers :-) Mar 3, 2017 at 12:36

You must log in to answer this question.

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