-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathString.c
92 lines (88 loc) · 1.49 KB
/
String.c
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
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
unsigned int len(char *str)
{
char *p = str;
while (*p != '\0')
p++;
return (p - str);
}
char *rev(char str[])
{
int i, l = len(str);
char t;
for (i = 0; i < l / 2; i++)
{
t = str[i];
str[i] = str[l - 1 - i];
str[l - 1 - i] = t;
}
return str;
}
char *cat(char str1[], char str2[])
{
int i = 0, j = 0;
//goto end of the first str
while (str1[i] != '\0')
i++;
while (str1[i++] = str2[j++])
;
return str1;
}
char *cpy(char str1[], char str2[])
{
int i = 0;
while ((str1[i] = str2[i]) != '\0')
i++;
return str1;
}
/*
int cmp(char str1[],char str2[]){
int i;
for(i=0; str1[i] == str2[i]; i++)
if(str1[i] == '\0')
return 0;
return str1[i]-str2[i];
}*/
int cmp(char *str1, char *str2)
{
while (*str1 == *str2)
{
if (*str1 == '\0')
return 0;
str1++;
str2++;
}
return (*str1 - *str2);
}
/*
unsigned int len(char str[]){
int i=0;
while(str[i]!='\0')
i++;
return i;
}*/
int main(void)
{
char str1[50], str2[50];
printf("Enter a string : ");
gets(str1);
printf("\n");
puts(str1);
printf("\nLenght is : %d\n", len(str1));
rev(str1);
puts(str1);
//printf("\nEnter another string : ");
//gets(str2);
/*printf("\n");
puts(str2);
printf("\nLenght is : %d", len(str2));
if(cmp(str1,str2)==0)
printf("\nStrings are same\n");
else
printf("\nStrings are not same\n");
printf("%d", cmp(str1,str2));
*/
cat(str1, str2);
//cpy(str2,str1);
puts(str1);
}