-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcopyInput.c
101 lines (92 loc) · 1.8 KB
/
copyInput.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
93
94
95
96
97
98
99
#include <stdio.h>
#include <stdlib.h>
#define MAXLINE 100
#define ARBITRARY 10
int readLine(char line[],int maxline);
void copyLine(char from[],char to[],int len);
void reverse(char string[]);
void removeTrailingSpace(char string[]);
int strrindex(char[], char[]);
int main()
{
int len, max;
char currentLine[MAXLINE];
char mostLengthLine[MAXLINE];
max = 0;
while((len=readLine(currentLine,MAXLINE))>0){
printf("%d", strrindex(currentLine, "help"));
}
return 0;
}
int readLine(char charLine[], int max){
int c, i;
for(i=0;(c=getchar())!=EOF&& c!='\n'&&i<max-1;i++)
charLine[i]=c;
if(c=='\n'){
charLine[i]=c;
++i;
}
charLine[i]='\0';
return i;
}
void copyLine(char initLine[], char newLine[], int length){
newLine[length-1]='\0';
for(--length;length>0; --length){
newLine[length-1]=initLine[length-1];
}
}
void reverse(char s[]){
int i, len;
i = 0;
while(s[i]!='\n'){
++i;
}
len = i;
if(i%2==0)
i/=2;
else
i =i/2 +1;
char temp[len];
for(;i>0;--i){
temp[len-i]=s[len-i];
s[len-i]=s[i-1];
s[i-1]=temp[len-i];
}
}
void removeTrailingSpace(char s[]){
int i, whiteSpaceCounter, first;
whiteSpaceCounter = first = 1;
for(i=0;s[i]!='\0';++i){
if(s[i]==' '||s[i]=='\t'||s[i]=='\n'){
if(first){
whiteSpaceCounter=i;
first = 0;
}
}else{
first = 1;
}
}
s[whiteSpaceCounter+1] = '\0';
if(whiteSpaceCounter!=0)
s[whiteSpaceCounter] = '\n';
else
s[whiteSpaceCounter] = '\0';
}
/*
* Exercise 4-1
* Write the function strrindex(s,t) which returns the position of the rightmost occurrence of t
* in s, or -1 if there is none
*/
int strrindex(char s[], char t[]){
int i, j, storage;
storage = -1;
for(i = 0; s[i]!='\0';++i){
if(s[i]==t[0]){
for(j=1;s[i+j]==t[j]&&s[i+j]!='\0'&&t[j]!='\0';++j)
;
if(t[j]=='\0')
storage = i;
}
}
return storage;
}