find char function

Jan 30, 2017 at 2:14am
Below is a simple find character in char array.

It returns either the position value or -1 for not found.

Was hoping a little feedback and how to make it better and clearer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int  stringFindchar(const char s[], const char ch, const int startpos)
{
        char s[] = "abc";
        char c[] = "xyb"
        int startpos = 2;
	int i = 0;
	int len2 = stringLength(s);
	while (s[i] != 0)

	{
		for (int z = startpos; z < len2; z++)
		{
			if (s[z] == ch[2])
				return z;
		}
		i++;

	}
	
	return(-1);
}
Jan 30, 2017 at 2:18am
Was hoping a little feedback and how to make it better and clearer.

Your function is wrong and possibly does not compile. You are re-declaring your variables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int  stringFindchar(const char s[], const char ch, const int startpos)
{
        char s[] = "abc";
        char c[] = "xyb"
        int startpos = 2;
	int i = 0;
	int len2 = stringLength(s);
	while (s[i] != 0)

	{
		for (int z = startpos; z < len2; z++)
		{
			if (s[z] == ch[2])
				return z;
		}
		i++;

	}
	
	return(-1);
}
Jan 30, 2017 at 2:49am
Thanks. I actually didn't mean to include the prototype. I am running it in main to test before turning it into a function.

So, minus the duplicates.
Jan 30, 2017 at 2:51am
A normal way is to return the position as soon as a character is found.

1
2
3
4
5
6
7
8
9
int stringFindchar(const char s[], const char ch, const int startpos)
{
	int i = 0;
	int len = strlen(s);

	for(i = startpos; i < len; i++) if(s[i] == ch) return i;

	return -1;
}
Jan 30, 2017 at 4:49am
Thanks! That works better and is more efficient.

I have one more function that compares two char strings, but my loop is only comparing the first character and not the entire char string:

int len = stringLength(s);

for (int i = 0; i<len; i++)
{
if (s[i] < t[i])
return -1;
if (s[i] > t[i])
return 1;
if (s[i] == t[i])
return 0;
}
Jan 30, 2017 at 5:00am
I have one more function that compares two char strings, but my loop is only comparing the first character and not the entire char string


If it is only checking the first character, you ask for it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	int len1 = stringLength(s);
	int len2 = stringLength(t);

	if(len1 > 0 && len2 > 0) // If both strings have certain size
	{
		if(s[0] < t[0]) return -1;
		if(s[0] > t[0]) return 1;

		return 0;
	}
	else if(len1 == 0 && len2 == 0) // If both strings are empty
	{
		return 0;
	}

	// If either string is empty, return a large value
	return 100;


Disclaimer : I may have misunderstood what the OP said. I have emphasized the both part to prove that the OP's goal was not clear enough.
Last edited on Jan 30, 2017 at 6:17am
Jan 30, 2017 at 5:46am
That works for the first character, but what if you are comparing abc and abd. Don't you need to put a loop in there and make abd greater than abc?
Jan 30, 2017 at 5:59am
Here's what I came up with. It compares greater,less, and equal for two strings:

int i = 0;

char s[] = "ab d";
char t[] = "abc";
int counter = 0;
while (s[i] != '\0' && t[i] != '\0')
{
if (s[i] > t[i])
counter++;
if (s[i] < t[i])
counter--;
if (s[i] == t[i])
counter = 0;
i++;
}

cout << counter << endl;
Jan 30, 2017 at 6:07am
That works for the first character, but what if you are comparing abc and abd. Don't you need to put a loop in there and make abd greater than abc?

Since you ask to check the first character, please provide more information.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int i = 0;

char s[] = "abd";
char t[] = "abc";
int counter = 0;
while (s[i] != '\0' && t[i] != '\0')
{
  if (s[i] > t[i])
  {
    counter = s[i] - t[i]; break;
  }
  else if (s[i] < t[i])
  {   
    counter = s[i] - t[i]; break;
  }
 
   i++;
}

cout << counter << endl; 


http://stackoverflow.com/questions/13571907/when-will-strcmp-not-return-1-0-or-1?rq=1

This version works when both the strings have the same length.
Last edited on Jan 30, 2017 at 6:08am
Feb 1, 2017 at 4:21am
Okay, I have two char strings I am comparing, all goes well until values have the same characters: John Johnson

char s[10] = "John";
char t[10] = "Johnson";

int i = 0;
int counter = 0;

{
while (s[i] != '\0' && t[i] != '\0')
{
if (s[i] > t[i])
{
counter = s[i] - t[i]; break;
}
else if (s[i] < t[i])
{
counter = s[i] - t[i]; break;
}
i++;
}
if (counter > 0)
{
return 1;
}
if (counter < 0)
{
return -1;
}
else { return 0;}
}

This is still returning 0, even though they are not the same.
Last edited on Feb 1, 2017 at 4:37am
Feb 2, 2017 at 4:43am
Mantorr22 wrote:
This version works when both the strings have the same length.

That "version" only works for the last character of strings that have the same length. "acd" and "bfd" would be "equal".


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
#include <iostream>
#include <string>

int comp(const char* a, const char* b) {

    while (*a && *b && *a == *b)    // while corresponding characters are not nul and are equal
        ++a, ++b;                   // advance the pointers.

    // At this point one of two things has happened:
    // - A nul character was found indicating the end of one (or both) of the strings.
    // - Corresponding characters were encountered that were not equal

    // Therefore, if *a is equal to *b, then the strings are equal.
    if (*a == *b) return 0;

    // At this point it is a given that *a is not equal to *b
    // If both *a and *b are not end-of-string, then if *a is less than *b,
    //    a should come before b.
    if (*a && *b) return *a - *b;

    // At this point it is a given that one of our pointers is at end-of-string.
    // The one that is should come before the other.
    return *a ? 1 : -1;
}

bool results_equivalent(int v1, int v2) {
    if (v1 == 0 && v2 == 0) return true;
    if (v1 < 0 && v2 < 0) return true;
    if (v1 > 0 && v2 > 0) return true;
    return false;
}

void test(const char* s1, const char* s2) {
    int result = comp(s1, s2);
    int expected = std::string(s1).compare(s2);
    bool equivalent = results_equivalent(result, expected);
    std::cout << (equivalent ? "** CORRECT **  " : "** INCORRECT **");

    std::cout << " \tcomp(\"" << s1 << "\", \"" << s2 << "\") is " << result << '\n';
}

int main()
{
    test("John", "Johnson");
    test("John", "John");
    test("abc", "abd");
    test("dba", "cba");
    test("Hammerpants", "Hammer");
}


Edit: Added results_equivalent since comparing the results of the two functions directly wasn't necessarily meaningful.
Last edited on Feb 2, 2017 at 4:55am
Topic archived. No new replies allowed.