Thread: plz help me find out where i hv gone wrong wid dis c program on operstions on strings

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    8

    plz help me find out where i hv gone wrong wid dis c program on operstions on strings

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<ctype.h>
    #include<conio.h>
    
    
    
    
    
    
    
    
    void main()
    {int ch;
    printf("1.Calculate the length of string\n2.Compare 2 strings\n3.Concatenate 2 strings\n4.Copy a string to another\n5.Change the string case\n6.Sentence case\n");
    scanf("%d",&ch);
    switch(ch)
    {
    case 1:
            {       char s[20];
    
    
             printf("Enter the string the length of which is to be calculated:\n");
             gets(s);
            int len=strlen(s);
            printf("The length of the string %s is: %d",s,len);
             }
             break;
    case 2:
            { char x[20],y[20];
            printf("Enter the 2 strings to be compared:\n");
            gets(x);
            printf("\n");
            gets(y);
            printf("\n");
            if(strcmp(x,y)==0)
            printf("The 2 strings %s and %s are the same\n",x,y);
            else
            printf("The 2 strings %s and %s are not the same",x,y);
            }break;
    case 3:
            {
            char x[30],y[10];
            printf("Enter the 2 strings to be concatenated:\n");
            printf("x=");gets(x);printf("\ny=");
            gets(y);
            strcat(x,y);
            printf("\n After concatenation the two strings are x=%s and %s",x,y);
            }break;
    case 4:
            {
            char s[20],copy[20];
            printf("Enter the string to be copied into another string:\n");
            gets(s);
            strcpy(copy,s);
            printf("The copied string is:%s",copy);
            }break;
    case 5 :
         {
            int c; char s[20];
          printf("Enter the string");
          gets(s);
          int len=strlen(s);
          printf("\nTo what case would you like the string to be converted?\n1.Upper\n2.Lower\n");
          scanf("%d",&c);
          switch(c)
          {
          case 1:
          {
          for(int i=0;i<len;i++)
          s[i]=toupper(s[i]);
          printf("The converted string is:\n");
          puts(s);
          }
          break;
          case 2:
          {
          for(int i=0;i<len;i++)
          s[i]=tolower(s[i]);
          printf("The converted string is:\n");
          puts(s);
          }
          }
          }break;
    case 6:
          {       char s[30];
          printf("\nEnter the sentence");
          gets(s);
          int len=strlen(s);
          s[0]=toupper(s[0]);
          for(int i=0;i<len;i++)
          {
          if(s[i]==' ')
          {
          s[i+1]=toupper(s[i+1]);
          }
          }
          printf("The converted string is:\n");
          puts(s);
          }
    
    
          }
          getch();
          }


    here gets() is nt working!! :'-(

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Here's a list
    Try to sound intelligent
    void main is wrong
    gets() is evil beyond redemption
    Your code layout could be better
    Is your compiler up to date?

    Generally, you want to avoid mixing up scanf() calls with other input functions, like fgets() and getchar(). It takes some care to get them to work nicely with one another.
    This is discussed in our local FAQ -> FAQ - Cprogramming.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    ok..bt wat changes shud i mk to my source code?? ...i m jst abeginnr in programing...so dnt knw much...i tried replacing void main by int main...bt evn dat dint help..nd if i use scanf() in place f gets().the program is working bt my equiremnt of enterin a string including spaces is not met...so plz suggest changes...

  4. #4
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    It's generally a good idea to actually read the links people recommend to you.

    Bye, Andreas

  5. #5
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by dishagarg View Post
    ok..bt wat changes shud i mk to my source code?? ...i m jst abeginnr in programing...so dnt knw much...i tried replacing void main by int main...bt evn dat dint help..nd if i use scanf() in place f gets().the program is working bt my equiremnt of enterin a string including spaces is not met...so plz suggest changes...
    You need to tell us what is not working how you would like it, what you would like it to do, and an up to date version of your code

    Please read Salem's links first and feel free to ask any questions
    Fact - Beethoven wrote his first symphony in C

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    after going through all the above mentioned links..these were the changes that i had made in my program
    1.replaced void by int
    2.tried replacing gets() by fgets and scanf("%[^\n]s",name) but still my problem is the same
    the detailed version of my problem is:
    when i run the cases under switch individually on a separate window,i face no problem and i am getting a perfect output
    but when i am running the above code,i am not being asked to enter the string..for instance under case 1 of string operations,i am getting that the length f the string is 0; in short,the compiler is not executing gets() or the other input functions when under switch case....

  7. #7
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    also if i use scanf("%s",name),the program output is perfect but only i am not able to enter a sentence..

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,665
    Well after all those edits, it's best to post your latest attempt.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  9. #9
    Registered User
    Join Date
    Feb 2013
    Posts
    8
    ohkay..il do that now..

  10. #10
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Quote Originally Posted by dishagarg View Post
    ohkay..il do that now..
    It's very hard to help you with your program if you have not posted your code
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-21-2012, 01:39 AM
  2. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  3. Can someone find out what i'm doing wrong?
    By QuincyEQ in forum C Programming
    Replies: 2
    Last Post: 08-15-2002, 11:22 AM
  4. Whats wrong with my find Circle Area program?[compiles fine]
    By Golden Bunny in forum C++ Programming
    Replies: 22
    Last Post: 06-16-2002, 02:49 PM
  5. Help me find out what's wrong
    By biosx in forum C Programming
    Replies: 4
    Last Post: 08-15-2001, 12:37 PM