Thread: HELP! How To Enter Spaces into a string

  1. #1
    Registered User
    Join Date
    Dec 2016
    Posts
    5

    Question HELP! How To Enter Spaces into a string

    Hello, I was trying to run this piece of code that allows me to scanf a sentence, and then print it, but the only problem is, when I print it the spaces are not counting

    For Example I want the output to be:
    Code:
    Hey how are you doing
    But instead the output is:
    Code:
    Heyhowareyoudoing
    Code:
    int main()
    {
        char sentence[50];
        puts ("Write your sentence");
        scanf("%s",&sentence);
        printf("\n\nYour centence is: %s\n",centence);
    }
    Thanks


    And this is what Im trying to do as an exercise for tomorrow
    if you have any suggestions, I'll be appreciative

    P.S. Sorry for the output code being in Portuguese


    Code:
    void menu()
    {
        
        puts("\t\t ______________________");
        puts("\t\t|                      |");
        puts("\t\t|         Menu         |");
        puts("\t\t|                      |");
        puts("\t\t|      1. STRLEN       |");
        puts("\t\t|      2. STRCPY       |");
        puts("\t\t|      3. STRCAT       |");
        puts("\t\t|      4. STRCMP       |");
        puts("\t\t|      5. STRCHR       |");
        puts("\t\t|      6. STRSTR       |");
        puts("\t\t|      7. STRLWR       |");
        puts("\t\t|      8. STRUPR       |");
        puts("\t\t|                      |");
        puts("\t\t|______________________|");
        
    }
    void decision()
    {
        int op;
        
        puts("\nQual a funcao que quer testar? ");
        scanf("%d",&op);
        
        if(op==1)
        {
            stringLenght();
            fflush(stdin);
        }
        
        if(op==2)
        {
            stringCopy();
            fflush(stdin);
        }
        
        if(op==3)
        {
            stringMerge();
            fflush(stdin);
        }
        
        if(op==4)
        {
            stringCompare();
            fflush(stdin);
        }
        
        if(op==5)
        {
            stringSearchChar();
            fflush(stdin);
        }
        
        if(op==6)
        {
            stringSearchString();
            fflush(stdin);
        }
        
        if(op==7)
        {
            stringToLower();
            fflush(stdin);
        }
        
        if(op==8)
        {
            stringToUpper();
            fflush(stdin);
        }
        
        
        
        
        
    }
    
    
    void menuInfo()
    {
        
        
    }
    
    
    //1
    int stringLenght()
    {
        //Syntax for STRLEN = strlen (<comparable string>);
        int var1;
        
        puts("Quantos Caractreres Pensa em Introduzir? ");
        scanf("%d",&var1);
    
    
        int lenght;
        char str[var1],name1[var1];
        
        puts ("De um nome a String");
        scanf("%s",&name1);
        
        fflush(stdin);
        printf("\n\nInsira algums caracteres em %s: ",name1);
        scanf("%s",&str);    
        lenght=strlen(str);
        fflush(stdin);
        printf("O tamanho que '%s' = %s ocupa e de: %d Bytes",name1,str,lenght);
        
    }
    //2
    void stringCopy()
    {
        int var1;
        
        puts("Quantos Caractreres Pensa em Introduzir? ");
        scanf("%d",&var1);
        
        char name1[var1],name2[var1],str[var1],str1[var1];
        int op;
        
        fflush(stdin);
            
        puts ("De um nome a Primeira String");
        scanf("%s",&name1);
        puts ("De um nome a Segunda String");
        scanf("%s",&name2);
        
        fflush(stdin);
        printf("\n\nEscreva Algo dentro da string: %s\n",name1);
        scanf("%s",&str);
        printf("Escreva Algo dentro da string: %s\n",name2);
        scanf("%s",&str1);
        
        fflush(stdin);
        printf("\n\n1. Quer Copiar a string %s para a string %s?\n",name1,name2);
        puts("ou");
        printf("2. Quer Copiar a string %s para a string %s?\n\n",name2,name1);
        puts("Confirme Inserindo '1' ou '2' na consola");
        scanf("%d",&op);
        
        if(op==1)
        {    
            printf("A string Final e: %s = %s",name2,strcpy(str1,str));
        }
        
        if(op==2)
        {        
            printf("A string Final e: %s = %s",name1,strcpy(str,str1));
        }
    
    
    }
    //3
    void stringMerge()
    {
        int var1;
        
        puts("Quantos Caractreres Pensa em Introduzir? ");
        scanf("%d",&var1);
        
        char name1[var1],name2[var1],str[var1],str1[var1];
        int op;
        
        fflush(stdin);
            
        puts ("De um nome a Primeira String = Fonte");
        scanf("%s",&name1);
        puts ("De um nome a Segunda String = Destino");
        scanf("%s",&name2);
        fflush(stdin);
        printf("\n\nEscreva Algo dentro da string: %s\n",name1);
        scanf("%s",&str);
        fflush(stdin);
        printf("Escreva Algo dentro da string: %s\n",name2);
        scanf("%s",&str1);
        fflush(stdin);
        
        strcat(str,str1);
        
        printf("A string de destino = '%s' vai ter como produto: %s",name1,str);
        
        
    }
    //4
    void stringCompare()
    {
        
        
        
    }
    //5
    void stringSearchChar()
    {
        
        
        
    }
    //6
    void stringSearchString()
    {
        
        
        
    }
    //7
    void stringToLower()
    {
        
        
        
    }
    //8
    void stringToUpper()
    {
        
        
        
    }
    
    
    int main()
    {
        int op;
        op=1;
        
        do{
        system("cls");
        menu();
        decision();
        _getch();
        
        }while(op=1);
        
    }

  2. #2
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Code:
    printf("\n\nYour centence is: %s\n",centence);
    You mis-spelled your own variable. It should be sentence not "centence."


    To read in spaces in your sentence, you can use fgets instead of scanf:

    Code:
    fgets(sentence, 50, stdin);
    Will store up to 50 characters from the console (stdin) in the variable sentence.

    The reason why you have to specify stdin is because fgets is designed to accept text from any file so you could even use it with a text file by putting the filename instead of stdin, but that is not your purpose here.

    You could use sentence = gets() too but this does not limit how much the user can enter so they can enter more than 50 characters and overflow your buffer (the array that you made which is only 50 chars big) and cause all sorts of issues.
    Last edited by Asymptotic; 12-12-2016 at 10:47 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You should also stop using fflush(stdin)
    FAQ > Why fflush(stdin) is wrong - 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.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    while(op=1)
    Be aware of the difference between the assignment operator = and the equality operator ==

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 12-04-2010, 12:04 AM
  2. Replies: 7
    Last Post: 10-03-2009, 10:58 PM
  3. String with Spaces. HELP.
    By tanpearl in forum C Programming
    Replies: 3
    Last Post: 02-20-2009, 08:49 AM
  4. Can a String Have Spaces In It?
    By Grantyt3 in forum C++ Programming
    Replies: 6
    Last Post: 08-28-2005, 09:49 AM
  5. Getting rid of spaces in a string
    By scythe in forum C Programming
    Replies: 3
    Last Post: 10-23-2003, 04:05 AM

Tags for this Thread