how to read sentence including space ?

curious here. how to solve them?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   printf("The phrase that u enter is: \n");
   
   for(i=0; i<len; i++)
   {
   	printf("%c", phr[i]);
   }  
       for(a=0; a<len; a++)
      {
            for(b=0; b<37; b++)
            {
                      
              if(tolower(phr[a])==alpha[b])
                 printf("%s ", morse[b]);     
            }
      }
closed account (28poGNh0)
The whole code please
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
   printf("\n\tEnter in a phrase in English for encrypting\n");                 
   ("%s", phr);
   
   len=strlen(phr);
   
   printf("The phrase that u enter is: \n");
   
   for(i=0; i<len; i++)
   {
   	printf("%c", phr[i]);
   }  
      
  
    printf("\nConverting the phrase to Morse Code.... \n");
	
        for(a=0; a<len; a++)
      {
            for(b=0; b<37; b++)
            {
              if(tolower(phr[a])==alpha[b])
                 printf("%s ", morse[b]);                
            }
      }
       
      
      printf("\n\tDone!\n"); 


i need to encode phrases , between phrase, there's 3 spaces.

 The phrase that you enter is :
:)
Converting the morse code...

        Done! 
Last edited on
This is C, right? You can read the input (including spaces) with the function fgets. Here is the doc: http://www.cplusplus.com/reference/cstdio/fgets/?kw=fgets
yup, C. apparently, i cant use gets. when i put gets(phr), didnt detect.

 Enter code to encrypt:

Do you want to continue?
Actually I said fgets, since gets is deprecated, but this is not the problem. The following code works:

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>

int main() {

	char word[100];

	printf("\n\tEnter in a phrase in English for encrypting: ");
	fgets(word, 99, stdin);

	printf("The phrase that u enter is: %s\n", word);

	return 0;
}
oh, i see. get it.

wait. length of string is word right?

for(i=0;i<word;i++) ?

i need to submit it today. really appreciate your help
> length of string is word right?

Not at all! (word) is the name of the variable, and in this case it is an array of char. Its size is the number specified between the square brackets: this means that (word) can contain up to 100 characters. Note that this doesn't mean that it contains exactly 100 chars: it is just a maximum potential size.
my new code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
do//while(phr[i]!='\0')
   {                
      scanf("%s", phr);
      len=strlen(phr);

      for(a=0; a<len; a++)
      {
            for(b=0; b<37; b++)
            { 

              if(tolower(phr[a])==alpha[b])
                 printf("%s ", morse[b]);
            }
            
      } 
     i++; printf("    ");
   }  while(phr[i]!='\0'|| len=='\0' );


the output like i need, but still ask user input to encode until null.


class cancel
-.- .-.. .- ... ...    -.-. .- -. -. -. . .-..   c
-.-. c
-.-. c
-.-. c
-.-. 
done!




second, my loop for morse_to_english func

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
do
	  {               
	     scanf("%s", phr);
	     len=strlen(phr);
	 
	     for(a=0; a<len; a++)
	     {
              for(b=0; b<37; b++)
	           {
	 
	             if(strcmp(phr,morse[b])==0)
	                printf("%s ", alpha[b]);
	           } i++; printf("    ");
	  }  while(phr[i]!='\0');
	            
	     } 

cant run the loop.

i need submit today.
You should post the entire code, so that we can take a look and correct the mistakes. It's easier for us to work on a complete code, that we can compile and run on our own.
Topic archived. No new replies allowed.