Assignment - Find Character in Character Array

Dec 28, 2011 at 6:32pm
Hi there,

as part of my assignment a program has to take in string of characters (using character array for that), then sort it alphabetically [working]. Subsequently it shall take in character and find it in the string + give its location within the string.

Here is the code (using g++ compiler on Linux):
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
// C1.3 i)

#include<iostream>
#include<cstring>

using namespace std;

int main()
{


	char ch[100];
	int i, j, t, l;

	cout<<"Enter a string: ";
	cin>>ch;

// C1.3 ii)
	l=strlen(ch);

	for(i=0;i<l;i++)
		{
		for(j=i+1; j<l; j++)
			{
			if(ch[i] > ch[j])
			{
			t=ch[i];
			ch[i] = ch[j];
			ch[j] = t;
			}
		}
	}
	cout<<"In Alphabetical Order \n";
	for(i=0;i<l;i++)
	{
	cout<<ch[i]<<" ";
	}

// C1.3 iii)
char sch;
int pos;
cout<<"Enter character to be looked for: "<<"\n";
cin>>sch; 

    
     		for(i=0;i<l;i++)
		{
			if(ch[i]==sch)
			{
			cout<<"Character "<<sch<<" encountered at position: "<<pos<<"\n";
			}
			else
			{
			i++;
			pos++;
			}
	     	}
	


return 0;

 }


I've been messing around with the while loops at the end for quite a while, but pos is giving weird values. With different modifications it either overshoots to end of array length, displays zero or gives values one would expect form undefined integer.

However, how can this one be rectified?

Thanks in advance, appreciate it.
Sternenfisch
Dec 28, 2011 at 8:14pm
Hi Sternenfisch,

Your problem with pos stems from the fact that you do not initialize it. It looks like you are trying to return the current position, but you never set this value to the current position.

In your case, i contains the position information so replace line 50 with:
cout<<"Character "<<sch<<" encountered at position: "<<i<<"\n";
Dec 28, 2011 at 8:16pm
or gives values one would expect form undefined integer


But it is undefined.
And generally useless. Won't the loop variable suffice?
And once you get rid of pos, you can just take lines 52-56 right out.
Last edited on Dec 28, 2011 at 10:35pm
Dec 28, 2011 at 8:16pm
Actually I just looked closer and there are a few problems with that last loop:

In the else statement you increment i and pos (pos isn't initialized). In the case where ch[i]!=sch, then i gets incremented twice. Once in the if and once at the end of the loop.

I'd replace the whole thing with this:
1
2
3
4
5
6
7
     		for(i=0;i<l;i++)
		{
			if(ch[i]==sch)
			{
			cout<<"Character "<<sch<<" encountered at position: "<<i<<"\n";
			}
	     	}
Dec 31, 2011 at 1:26am
To display it properly, i has to be incremented by 1 (humans don't start counting at 0 like computers do).

Apart from this, the solution provided here works.
Thanks
Jan 1, 2012 at 12:15am
You may not start counting at 0, but I do. At least whenever I am playing with code.
Topic archived. No new replies allowed.