-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringencoding.cpp
executable file
·82 lines (74 loc) · 1.46 KB
/
stringencoding.cpp
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
// stringencoding.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include<stdlib.h>
#include"string.h"
#include<ctype.h>
char* stringencode(char *);
struct test
{
char input[30];
char output[50];
}testDB[8]=
{
{"abc","123"},
{"aAB","1AB"},
{"z1b","2612"},
{" A a @"," A 1 @"},
{" "," "},
{"",""},
{NULL,NULL},
{"abcdefghijklmnopqrstuvwxyz","1234567891011121314151617181920212223242526"}
};
//funtion to check if the character is a lower case
int isLowercase(char c)
{
if(c>='a' && c<='z')
return 1;
return 0;
}
//encoding function
char* stringencode(char *str)
{
char *output=(char*)calloc(50,sizeof(char));
int i,j,temp;//loop variable
for(i=0,j=0;str[i]!='\0';i++,j++)
{
if(isLowercase(str[i]))
{
if(toascii(str[i]>105))
{
temp=toascii(str[i])-96;//logic for encoding in the form a=1,b=2... z=26.
output[j+1]=temp%10+48;
output[j]=temp/10+48;
j++;
}
else
{
output[j]=toascii(str[i])-48;
}
}
else
output[j]=str[i];
}
return output;
}
//test function
void teststringencode()
{
int i;
char *encodedoutput=(char*)calloc(30,sizeof(char));
for(i=0;i<8;i++)
{
encodedoutput=stringencode(testDB[i].input);
if(strcmp(encodedoutput,testDB[i].output)==0)
printf("PASSED\n");
else
printf("FAILED\n");
}
}
int _tmain(int argc, _TCHAR* argv[])
{
teststringencode();
return 0;
}