R20 CP UNIT-3 Morse code and string functions
R20 CP UNIT-3 Morse code and string functions
In Example-2, we have used fgets ( ) function to read a string from the user.
fgets ( name, sizeof(name), stdin );
The sizeof(name) results to 20. Hence, we can take a maximum of 20 characters as input
which is the size of the name string.
String Functions in C library:
There are so many predefined functions regarding string operations. To use these functions
we must include “string.h” header file. And these functions should be write in lowercase.
Page 11
Programming for Problem Solving using C UNIT-3
Example-1:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "WeLcOmE";
printf (" String is %s", mystr);
printf ("\n Length is %d", strlen(mystr));
printf ("\n Uppercase is %s", strupr(mystr));
printf ("\n Lowercase is %s", strlwr(mystr));
return 0;
}
Example-2:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30] = "welcome";
char mystr2[30] = "WELCOME";
if(strcmp(mystr,mystr2)==0)
{
printf("Both are Equal");
}
else
{
printf("Both are Not Equal");
}
return 0;
}
Example-3:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30];
char mystr2[30] = "welcome";
strcpy(mystr,mystr2);
printf("%s",mystr);
return 0;
}
Example-4:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30]="welcome";
char mystr2[30] = " to cse";
strcat(mystr,mystr2);
printf("%s",mystr);
return 0;
}
Page 12
Programming for Problem Solving using C UNIT-3
Example-5:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30]="welcome to cse";
printf("%s",strchr(mystr,'c'));
return 0;
}
Example-6:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30]="welcome to cse";
printf("%s",strrchr(mystr,'c'));
return 0;
}
Example-7:
#include <stdio.h>
#include <string.h>
int main()
{
char mystr[30]="welcome to cse";
printf("%s",strrev(mystr));
return 0;
}
MORSE CODE:
Morse code is a method of transmitting text information as a series of on-off tones,
lights, or clicks that can be directly understood by a skilled listener or observer without
special equipment. It is named for Samuel F. B. Morse, an inventor of the telegraph.
The algorithm is very simple. Every character in the English language is substituted
by a series of „dots‟ and „dashes‟ or sometimes just singular „dot‟ or „dash‟ and vice versa.
Every text string is converted into the series of dots and dashes. For this every
character is converted into its Morse code and appended in encoded message. Here we have
copied space as it is. And we have not considered numbers, only alphabets.
Page 13
Programming for Problem Solving using C UNIT-3
Program:
#include<stdio.h>
#include<string.h>
void main()
{
char name[50],morse[250];
int i;
printf("Input: ");
gets(name);
printf("\nOutput : ");
for(i=0;i<strlen(name);i++)
{
switch(name[i])
{
case 'A': printf(".-");break;
case 'B': printf("-...");break;
case 'C': printf("-.-.");break;
case 'D': printf("-..");break;
case 'E': printf(".");break;
case 'F': printf("..-.");break;
case 'G': printf("--.");break;
case 'H': printf(". .. ");break;
case 'I': printf("..");break;
case 'J': printf(".---");break;
case 'K': printf("-.-");break;
case 'L': printf(".-..");break;
case 'M': printf("--");break;
case 'N': printf("-.");break;
case 'O': printf("---");break;
case 'P': printf(".--.");break;
case 'Q': printf("--.-");break;
case 'R': printf(".-.");break;
case 'S': printf("...");break;
case 'T': printf("-");break;
case 'U': printf("..-");break;
case 'V': printf("...-");break;
case 'W': printf(".--");break;
case 'X': printf("-..-");break;
case 'Y': printf("-.--");break;
case 'Z': printf("--..");break;
}
}
}
Page 14