loop problem.

i cant figure out how to size the length of string when there's null.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define STR 1000

void English_to_Morse();
void Morse_to_English();

int main()
{  
    char ch,ans;
    printf("Would you like to decode or encode the text now? \n 'y' for yes and 'n' for no.\n");
    scanf("%c",&ans);
    
    while(ans =='y' || ans=='Y')
    {
    	printf("\n\tSelect from the menu:\n"
               "\tA. English to Morse\n"
               "\tB. Morse to English\n");
      fflush(stdin);
      scanf("%c",&ch);

     if(tolower(ch) == 'a')
     {
         English_to_Morse();
         printf("\n\n");
         printf("Do you want to continue to decode or encode another text?\n 'y' for yes and 'n' for no.\n");
         fflush(stdin);
         scanf("%c",&ans);
         }
         
         else if (tolower(ch) == 'b')
          {       
            Morse_to_English();
            printf("\n\n");
            printf("Do you want to continue to decode or encode another text?\n 'y' for yes and 'n' for no.\n");
            fflush(stdin);
            scanf("%c",&ans);
              }
       else 
       {
            printf("Invalid! Enter again: \n");
            scanf("%c",&ch);
            }
            
            
     }
    
     while(ans=='y' || ans=='N');
      {
    	 if(ans=='n' || ans=='N')
    	 {
          printf("\n\n");
          printf("Thank you for using this system \2\n\n\n");
          }
    	 
    	 else 
    	 printf("Invalid! \n");
       }
   
   system("pause");
   return 0;
}

void English_to_Morse()
{
   int len, a, b,i=0;

   char phr[STR],ans;

   char alpha[]={"abcdefghijklmnopqrstuvwxyz1234567890"};
   char *morse[37]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
                   "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
                   "..-","...-",".--","-..-","-.--","--..",".----","..---",
                   "...--","....-",".....","-....","--...","---..","----.",
                   "-----","   "};
   printf("\n\tEnter in a phrase in English for encrypting\n");                 
   
   while (phr[i]!='\0')
   {  
      scanf("%s", phr);
      len=strlen(phr);
   
    printf("\nConverting the phrase to Morse Code.... \n");
	sleep(50);  
      
      for(a=0; a<len; a++)
      {
            for(b=0; b<37; b++)
            {
              if(tolower(phr[a])==alpha[b])
                 printf("%s ", morse[b]);   
            }
      }
      
     i++; printf("    ");
     } 
     
     printf("\n\tDone!\n");
      
   printf("\n");
   return;          
}


void Morse_to_English()
{  
   int len, a, b, i=0;

   char phr[STR];

   char alpha[]="abcdefghijklmnopqrstuvwxyz1234567890";
   char *morse[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---",
                   "-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-",
                   "..-","...-",".--","-..-","-.--","--..",".----","..---",
                   "...--","....-",".....","-....","--...","---..","----.",
                   "-----"};
   printf("\t\t\tMorse Code: \n");
    for(i=0; i<36; i++)
    {
    	printf("%s", morse[i]);
    	printf("\t");
    }
    printf("\n\n\tEnter a line of Morse Code for decrypting\n\n");     
	scanf("%s",&phr);
    len=strlen(phr);
    printf("\nConverting the Morse Code.... \n");
    
    for(a=0; a<36; a++)
      {
         if(strcmp(phr, morse[a])==0 && *phr!='\0')
           printf("%c", alpha[a]);
           
          }     
     printf(" ");
    
    
    printf("\n");
    return;
}
For the moment I fixed the errors in English_to_Morse function.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
void English_to_Morse() {
	int len, a, b;

	char phr[STR];

	char alpha[] = { "abcdefghijklmnopqrstuvwxyz1234567890" };
	char *morse[37] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--",
			"-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".----",
			"..---", "...--", "....-", ".....", "-....", "--...", "---..", "----.", "-----", "   " };

	// clear the buffer
	while(getchar() != '\n');

	printf("\n\tEnter in a phrase in English for encrypting\n");

	fgets(phr, STR-1, stdin);

	len = strlen(phr);

	// remove trailing '\n'
	if(phr[len-1] == '\n') {
		phr[len-1] = '\0';
		--len;
	}

	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("    ");

	printf("\n\tDone!\n");

	printf("\n");
	return;
}


Take a look at it and try to fix the rest of the code.

P.S. Your goal should be compiling without any warning!
actually, i didnt quite understand function fgets. well, can said that my lecturer didnt on that part.

fgets(phr,STR-1,stdin)

phr is to get the string, STR-1 is size and stdin? what's that?

Here is the doc: http://www.cplusplus.com/reference/cstdio/fgets/

stdin is the standard input and it is the stream we want to read from.

fgets has a little problem (if we want to call it "a problem"): it stores the trailing newline (i.e. the '\n' char) in the string. If you don't want it (and you usually don't), then you have to remove it manually, and this is what I did at lines 20-24. Then everything is pretty much straight forward.
if i need the length of string including space between words,

len=strlen(text);

strlen only up to '\0' right? it cant grabs the whole text


is there a way to get the length of string including null then get the size? size to use in for loop
strlen is fine... the '\0' is at the end of the string, so if you read "hello world" you actually store "hello world\0" and strlen returns 11, which is 5 (hello) + 1 (the space) + 5 (world).
@minomic, why STR-1 ? cant quite understand.

in func morse, i should remake the morse array with space? ex, a = .- b=-...

morse[37] = { ".- " , ".--- ", ...};

i try to loop them, get the input ,

text : .- .----    .-


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

    }while(*phr!='\0');    


the ouput :

aabbbcccc


my idea, get the code length including null, compare with morse, find, search for another code. i cant express em into the coding.


Topic archived. No new replies allowed.