-
-
Save justjkk/432844 to your computer and use it in GitHub Desktop.
String Functions in C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<stdlib.h> | |
#define ARRAY_SIZE 15 | |
typedef enum {false, true} bool; | |
int leng(char *ipstr) | |
{ | |
int i = 0; | |
while(ipstr[i] != '\0') | |
i++; | |
return i; | |
} | |
char *concat(char *str1, char *str2) | |
{ | |
int leng1, i; | |
leng1 = leng(str1); | |
for(i = leng1; *str2 != '\0'; i++, str2++) | |
{ | |
str1[i] = *str2; | |
} | |
str1[i] = '\0'; | |
return str1; | |
} | |
bool compare(char *str1,char *str2) | |
{ | |
int leng1, leng2, i, count = 0; | |
leng1=leng(str1); | |
leng2=leng(str2); | |
if(leng1 == leng2) | |
{ | |
for(i = 0; i < leng1; i++) | |
{ | |
if(str1[i] != str2[i]) | |
{ | |
return false; | |
} | |
} | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
char *reverse(char *str1) | |
{ | |
int leng1, i, j, midleng; | |
char temp; | |
leng1 = leng(str1); | |
j = leng1 - 1; | |
midleng = leng1 / 2; | |
for(i = 0; i < midleng; i++, j--) | |
{ | |
temp = str1[i]; | |
str1[i] = str1[j]; | |
str1[j] = temp; | |
} | |
return str1; | |
} | |
int main() | |
{ | |
char str1[ARRAY_SIZE],str2[ARRAY_SIZE],*resultstr; | |
int length=0,i=0,choice=0,result=-99; | |
printf("\n\n\tChoose the operation you need to perform..."); | |
printf("\n\n\t1.StringLength\n\t2.StringReverse\n\t3.StringConcatenation\ | |
\n\t4.StringCompare\n\n\tYour Choice(in numbers) : "); | |
scanf("%d",&choice); | |
switch(choice) | |
{ | |
case 1: | |
printf("\n\n\tEnter the input string\n\n\t"); | |
scanf("%s",str1); | |
length=leng(str1); | |
printf("\n\n\t The length of the string is %d",length); | |
break; | |
case 2: | |
printf("\n\n\tEnter the input string\n\n\t"); | |
scanf("%s",str1); | |
resultstr=reverse(str1); | |
printf("\n\n\t The reversed string is %s",resultstr); | |
break; | |
case 3: | |
printf("\n\n\tEnter two input strings\n\n\t"); | |
scanf("%s %s",str1,str2); | |
resultstr=concat(str1,str2); | |
printf("\n\n\t The concatenated string is %s",resultstr); | |
break; | |
case 4: | |
printf("\n\n\tEnter two input strings\n\n\t"); | |
scanf("%s %s",str1,str2); | |
result=compare(str1,str2); | |
printf("\n\n\t The result is %d",result); | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment