Switch problem in C

This is a newbie question, but I can't seem to solve the prob

here is the source

int Str_len(char str1[])
      int count=0;
      while(*str1 != '\0')
return count;


int main()
            char string1[100];
      int choice = 0;
      printf("\n\nPlease Enter a String: ");      
      printf("\n1 - Count Number of Characters in String");
      printf("\n2 - Count Number of Words in String");
      printf("\n3 - Remove spaces in String");
      printf("\n4 - Reverse String");
      printf("\n6 - Look for sub-string in String");
      printf("\n7 - Insert sub-string into String\n\n");
                printf("Please option 1 to 7:");

      switch (choice)
      case 1:
            printf("%s has %d characters",string1, Str_len(string1));

      {      printf("illegal selection");

        return 0;

My problem is that everytime I enter a string with spacings, example "C is fun", the result is 'Illegal selection' which is in the default case, But it works if the string is just 1 word without spacing, example "cow"

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi Simon,

I don't think it's the spaces in the string causing the problem ;-)

I assume that the problem occurs when you enter a selection from your menu that is anything other than "1".

This is because your switch statement needs "case"s for each of the other options 2 .. 7.

Copy your "case 1:" statement to create these.  To start with, just have a printf inside each one that outputs the number of the selection you have made (don't forget to include the break statement though) ;-)
simonsngAuthor Commented:

thats a negatory. Tried that before, but only works when i input a string without spacing, otherwise, same result. wasnt able to select as there was an immediate display of the menu and illegal selection printing out, didnt even get to input a choice for the switch  

You are accepting a string from user using the
scanf("%s",string1) function. the scanf() function takes a
' '(space) as the end of string character.
So what happens when u enter a string with multiple words is that the scanf function reads the characters from the keyboard buffer until it reaches the 1st SPACE or ENTER character. It ignores the rest of the line. So actually the programme runs with the 1st word of the string.

But then the menu is displayed again and the next scanf() accepts the choice. Now, the remaining words of the previous string are still there in the keyboard buffer, which are read by the scanf() into the variable 'choice'.
Since the value of choice now doesn't match any of the choices you provided, the default case is executed.

I order to be able to accept multiword strings, use gets(char*) instead of scanf();


This function will accept input string in string1 until the enter key is not pressed.

No need for the rest of the cases to be written for this case to run!

Happy Programming!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

How come you only awarded logicTRANCE a grade B??
It seems a perfectly good answer to me.
Hi shamstar!

Thanks a lot for mentioning that!
I didn't cos i thought it would not be appropriate!

I'd be grateful if hta grade is somehow changed to an A.

Happy Programming!
I know what you mean about not mentioning it yourself.
To me giving a grade of A would make sense especially since you provided a solution which only needed one line of code to be changed, and you also provided a good description of why the problem happened with the old code. (Not to mention you beat me to typing the answer :p )

Dunno if the grade can be changed afterwards, maybe Mods can do it??
simonsngAuthor Commented:

my apology, didn't realise it till I submitted.. is there anyway to change the grade?
Naman GoelPrincipal Software engineerCommented:
actually u can not enter multiple word string using scanf function in this way.
beacuse sacnf just get the first word.
do try out getch() or scanf("%[^~]",s)
that means it accepts all the character to string s until u enter ~ sign as terminator.
hello simonsng!

I think you can make a request to CS for changing the grade. Just leave a message in the Customer Support(CS) section.
I'd be really grateful!

Shamstar, thanks for ur appreciation!

Happy Programming!
Not a problem logicTRANCE.
Sending a message to CS should take care of it.
Also to naman_goel's answer....   I use (sometimes)  scanf("%[^\n]",s);    which will accept until a newline (without the need for a ~ terminator).
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.