#include <stdio.h>#include <stdlib.h>
int strlength(char *x);
char* strConcatenate(char *,char *);
char* strRev(char *);
int main (int argc, const char * argv[])
{
///////////length///////////////
char *s ="welcome";
char *y = "tocpmging";
printf("Length:%d\n", strlength(s));
///////////concatenation/////////
char *result = strConcatenate(s, y);
printf("concatenation:%s\n",result);
free(result);
///////////reversing////////////
char *reverse = "texttrial";
char *resultstring = strRev(reverse);
printf("reversedstring:%s\n",resultstring);
free(resultstring);
return 0;
}
int strlength(char *x)
{
int count=0;
for (int i=0; x[i]!='\0'; i++) {
count ++;
}
return count;
}
char* strRev(char *x)
{
int i,j;
char *s = malloc(strlength(x));
for (i=0;x[i]!='\0'; i++);
j=0;
while (i>0) {
s[j] = x[i-1];
j++;
i--;
}
s[j] = '\0';
return s;
}
char* strConcatenate(char *x,char *y)
{
int i,j;
char *s = (char *)malloc(strlength(x)+strlength(y));
for (i=0; x[i]!='\0'; i++)
{
s[i] = x[i];
}
for (j=0; y[j]!='\0'; j++) {
s[i] = y[j];
i++;
}
s[i]='\0';
return s;
}
String functions through pointers
02:44 |
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment