0% found this document useful (0 votes)
2 views4 pages

R20 CP UNIT-3 Morse code and string functions

The document covers string manipulation functions in C, detailing their usage and providing examples for functions like strlen, strcat, and strcmp. It also introduces Morse code, explaining its concept and providing a simple program to convert text to Morse code. The examples illustrate various string operations including reading input, comparing strings, and concatenating them.

Uploaded by

Jyothi Yadla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

R20 CP UNIT-3 Morse code and string functions

The document covers string manipulation functions in C, detailing their usage and providing examples for functions like strlen, strcat, and strcmp. It also introduces Morse code, explaining its concept and providing a simple program to convert text to Morse code. The examples illustrate various string operations including reading input, comparing strings, and concatenating them.

Uploaded by

Jyothi Yadla
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming for Problem Solving using C UNIT-3

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.

SNO FUNCTION NAME DESCRIPTION


1 strlen ( string ) It returns length of string
2 strlwr ( string ) It converts string into lowercase
3 strupr ( string ) It converts string into uppercase
It concatenates string_1 to string_2 and then
4 strcat ( string_1, string_2 )
store into string_1.
It concatenates string_1 to specific no. of
5 strncat ( string_1, string_2,size ) characters of string_2 and then store into
string_1.
It compares string_1 with string_2.
➢ If both are equal then it returns 0.
6 strcmp ( string_1, string_2 )
➢ If string_1 < string_2 then it returns –ve.
➢ If string_1 > string_2 then it returns +ve.
It compares string_1 with specific no. of
characters of string_2.
7 strncmp ( string_1, string_2, size ) ➢ If both are equal then it returns 0.
➢ If string_1 < string_2 then it returns –ve.
➢ If string_1 > string_2 then it returns +ve.
8 strcpy ( string_1, string_2 ) It copies string_2 into string_1.
It copies specific no. of characters of
9 strncpy ( string_1, string_2, size )
string_2 into string_1.
It searches first position of character in the
10 strchr ( string, character ) string and prints the string from that
character to the end.
It searches last position of character in the
11 strrchr ( string, character ) string and prints the string from that
character to the end.
12 strrev ( string ) It returns reverse of string

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

You might also like