Saturday, 27 September 2014

PL Assignment 3 : String Operations


1:  /*  
2:   ============================================================================  
3:   Name    : String Operations  
4:   Authors   : Shivkumar, Mujahid  
5:   Version   :  
6:   Copyright  : Your copyright notice  
7:   Description : Hello World in C, Ansi-style  
8:   ============================================================================  
9:   */  
10:  #include <stdio.h>  
11:  #define max 10  
12:  int length(char*);  
13:  void copy(char*,char*);  
14:  int palindrome(char*);  
15:  void reverse(char*);  
16:  int compare(char*,char*);  
17:  void concat(char*,char*);  
18:  void substr(char*,char*);  
19:  int main()  
20:  {  
21:       char s1[max],s2[max];  
22:       char *str1,*str2;  
23:       int ch,len,comp,temp,i;  
24:       do  
25:       {  
26:            printf("\nMENU\n");  
27:            printf("1. Length of String\n");  
28:            printf("2. Copy\n");  
29:            printf("3. Palindrome\n");  
30:            printf("4. Reverse\n");  
31:            printf("5. Compare\n");  
32:            printf("6. Concatenate\n");  
33:            printf("7. Substring\n");  
34:            printf("0. Exit\n");  
35:            printf("\nEnter choice :\n");  
36:            scanf("%d",&ch);  
37:            printf("Enter String :\n");  
38:            scanf("%s",&s1);  
39:            str1=s1;     //Assigns value to str1 of s1;  
40:            printf("Entered String : %s",s1);  
41:            switch(ch)  
42:            {  
43:            case 1:     len=length(str1);  
44:                      printf("\nLength of String : %d\n",len);  
45:                      break;  
46:            case 2:     printf("\nCopy :\n");  
47:                      copy(str2,str1);  
48:                      printf("\nCopied String : %s",str2);  
49:                      break;  
50:            case 3: temp=palindrome(str1);  
51:                      if(temp==1)  
52:                           printf("\n%s is not a Palindrome",str1);  
53:                      else  
54:                           printf("\n%s is a Palindrome",str1);  
55:                      break;  
56:            case 4: reverse(str1);  
57:                      printf("\nReversed String : %s",str1);  
58:                      reverse(str1);//For getting the original string back  
59:                      break;  
60:            case 5: printf("\nEnter second string :\n");  
61:                      scanf("%s",&s2);  
62:                      str2=s2;     //Assigning Char value to Pointer  
63:                      printf("\nEntered String : %s",str2);  
64:                      comp=compare(str1,str2);  
65:                      if(comp==0)  
66:                           printf("\nBoth strings are equal.");  
67:                      else  
68:                           if(comp<0)  
69:                                printf("\nString1 > String2");  
70:                           else  
71:                                printf("\nString1 < String2");  
72:                      break;  
73:            case 6: printf("\nEnter second string :\n");  
74:                      scanf("%s",&s2);  
75:                      str2=s2;     //Assigning Char value to Pointer  
76:                      printf("\nEntered String : %s",str2);  
77:                      concat(str1,str2);  
78:                      printf("\nConcat String : \n");  
79:                      puts(str1);  
80:                      break;  
81:            case 7: printf("\nEnter second string :\n");  
82:                      scanf("%s",&s2);  
83:                      str2=s2;     //Assigning Char value to Pointer  
84:                      printf("\nEntered String : %s",str2);  
85:                      substr(str1,str2);  
86:                      break;  
87:            case 0: break;  
88:            default:printf("\nInvalid Choice.");  
89:                      break;  
90:            }  
91:       }while(ch!=0);  
92:       return 0;  
93:  }  
94:  int length(char *str)  
95:  {  
96:       char *s1;  
97:       s1=str;  
98:       while(*s1!='\0')  
99:            s1++;  
100:       return (s1-str);  
101:  }  
102:  void copy(char *str2, char *str1)  
103:  {  
104:       while(*str1!='\0')  
105:       {  
106:            *str2=*str1;  
107:            str2++;  
108:            str1++;  
109:       }  
110:       *str2='\0';  
111:  }  
112:  int palindrome(char *str)  
113:  {  
114:       int temp=0;  
115:       char *s1,*s2;  
116:       s1=str;  
117:       s2=str;  
118:       while(*s1!='\0')  
119:            s1++;  
120:       s1--;  
121:       while(s2<s1)  
122:       {  
123:            if(*s2!=*s1)  
124:                 temp=1;  
125:            s2++;  
126:            s1--;  
127:       }  
128:       return temp;  
129:  }  
130:  void reverse(char *str)  
131:  {  
132:       char temp;  
133:       char *s1,*s2;  
134:       s1=str;  
135:       s2=str;  
136:       while(*s1!='\0')  
137:            s1++;  
138:       s1--;  
139:       while(s2<s1)  
140:       {  
141:            temp=*s2;  
142:            *s2=*s1;  
143:            *s1=temp;  
144:            s2++;  
145:            s1--;  
146:       }  
147:  }  
148:  int compare(char *str1,char *str2)  
149:  {  
150:       while(*str1!='\0')  
151:       {  
152:       if(*str1>*str2)  
153:            return(-1);  
154:       if(*str1<*str2)  
155:            return(1);  
156:       str1++;  
157:       str2++;  
158:       }  
159:       return 0;  
160:  }  
161:  void concat(char *dest,char *src) //Concatenates String 'src' at the end of String 'dest'  
162:  {  
163:       int i;  
164:       for(i=length(dest);*src!='\0';i++,src++)  
165:            *(dest+i)=*src;  
166:       *(dest+i)='\0';  
167:  }  
168:  void substr(char *str1,char *str2)  
169:  {  
170:       char *s1,*s2;  
171:       s1=str1;  
172:       s2=str2;  
173:       int flag=0,temp;  
174:       printf("\nSubstring :");  
175:       while(*s1!='\0'&&*s1!=*s2)  
176:            s1++;  
177:       temp=(s1-str1)+1;  
178:       while(*s1!='\0'&&*s1==*s2)  
179:       {  
180:            s1++;  
181:            s2++;  
182:            if(*s2=='\0')  
183:                 flag=1;  
184:       }  
185:       if(flag==1)  
186:            printf("\nSubstring at %d\n",temp);  
187:       else  
188:            printf("\nNot a Substring");  
189:  }  
190:  --------------OUTPUT---------------  
191:  MENU  
192:  1. Length of String  
193:  2. Copy  
194:  3. Palindrome  
195:  4. Reverse  
196:  5. Compare  
197:  6. Concatenate  
198:  7. Substring  
199:  0. Exit  
200:  Enter choice :  
201:  1  
202:  Enter String :  
203:  pict  
204:  Entered String : pict  
205:  Length of String : 4  
206:  MENU  
207:  1. Length of String  
208:  2. Copy  
209:  3. Palindrome  
210:  4. Reverse  
211:  5. Compare  
212:  6. Concatenate  
213:  7. Substring  
214:  0. Exit  
215:  Enter choice :  
216:  2  
217:  Enter String :  
218:  pune  
219:  Entered String : pune  
220:  Copy :  
221:  Copied String : pune  
222:  MENU  
223:  1. Length of String  
224:  2. Copy  
225:  3. Palindrome  
226:  4. Reverse  
227:  5. Compare  
228:  6. Concatenate  
229:  7. Substring  
230:  0. Exit  
231:  Enter choice :  
232:  3  
233:  Enter String :  
234:  radar  
235:  Entered String : radar  
236:  radar is a Palindrome  
237:  MENU  
238:  1. Length of String  
239:  2. Copy  
240:  3. Palindrome  
241:  4. Reverse  
242:  5. Compare  
243:  6. Concatenate  
244:  7. Substring  
245:  0. Exit  
246:  Enter choice :  
247:  4  
248:  Enter String :  
249:  pict  
250:  Entered String : pict  
251:  Reversed String : tcip  
252:  MENU  
253:  1. Length of String  
254:  2. Copy  
255:  3. Palindrome  
256:  4. Reverse  
257:  5. Compare  
258:  6. Concatenate  
259:  7. Substring  
260:  0. Exit  
261:  Enter choice :  
262:  5  
263:  Enter String :  
264:  pict  
265:  Entered String : pict  
266:  Enter second string :  
267:  pict  
268:  Entered String : pict  
269:  Both strings are equal.  
270:  MENU  
271:  1. Length of String  
272:  2. Copy  
273:  3. Palindrome  
274:  4. Reverse  
275:  5. Compare  
276:  6. Concatenate  
277:  7. Substring  
278:  0. Exit  
279:  Enter choice :  
280:  6  
281:  Enter String :  
282:  pict  
283:  Entered String : pict  
284:  Enter second string :  
285:  pune  
286:  Entered String : pune  
287:  Concat String :   
288:  pictpune  
289:  MENU  
290:  1. Length of String  
291:  2. Copy  
292:  3. Palindrome  
293:  4. Reverse  
294:  5. Compare  
295:  6. Concatenate  
296:  7. Substring  
297:  0. Exit  
298:  Enter choice :  
299:  7  
300:  Enter String :  
301:  pictpune  
302:  Entered String : pictpune  
303:  Enter second string :  
304:  pune  
305:  Entered String : pune  
306:  Substring :  
307:  Not a Substring  
308:  MENU  
309:  1. Length of String  
310:  2. Copy  
311:  3. Palindrome  
312:  4. Reverse  
313:  5. Compare  
314:  6. Concatenate  
315:  7. Substring  
316:  0. Exit  
317:  Enter choice :  
318:  0 
 
https://drive.google.com/file/d/0BzbuE6BfgoRbX21ob2RXMGwxeUU/edit?usp=sharing
 
 

No comments:

Post a Comment

Ads Inside Post