-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcstring.cpp
More file actions
70 lines (64 loc) · 1.38 KB
/
Copy pathcstring.cpp
File metadata and controls
70 lines (64 loc) · 1.38 KB
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
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <array>
#include <cstdio>
using namespace std;
int mylen(char s[]){
int n = 0;
while (s[n] != '\0'){
n++;
}
return n;
}
int compare_cstrings(char s1[], char s2[]){
int i = 0;
while (i< 100000){
if (s1[i] == '\0' && s2[i] == '\0'){
return 0;
}
else if (s1[i] < s2[i] || s1[i] == '\0'){
return -1;
}
else if (s2[i] < s1[i] || s2[i] == '\0'){
return 1;
}
i++;
}
return 0;
}
bool palindrome(char str[]){
int n = mylen(str);
for (int i = 0; i<n/2; i++){
//std::printf("%c : %c \n", str[i], str[n-i-1]);
if (str[i] != str[n-i-1]){
return false;
}
}
return true;
}
void reverse(char str[]){
int n = mylen(str);
for (int i = 0; i<n/2; i++){
char c = str[i];
str[i] = str[n-i-1];
str[n-i-1] = c;
}
}
int main(){
char str[4] = { 'd', 'o', 'g', '\0' };
char str2[5] = { 'l', 'o', 'o', 'l', '\0' };
std::printf("%d\n", mylen(str));
std::printf("%d\n", compare_cstrings(str, str2));
if (palindrome(str2)){
std::printf("true\n");
}
else{
std::printf("false\n");
}
reverse(str);
for (int i = 0; i<mylen(str); i++){
std::printf("%c", str[i]);
}
return 0;
}