473,513 Members | 2,401 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Returning a character in an array of strings

4 New Member
\\I begin with opening a file, declaring an array of strings, and placing the \\individual lines in the file into arrays of strings.

string line[1000]; \\the number 1000 is an arbatrary expected
\\maximum number of lines in the file
int i=0;
while(getline(infile1, line[i], '\n'))
{
i++;
}

\\ The array is size i

Now I would like to read each string in this array while searching for a particular character and then return the number (of the string in the array) that the character occured at.


Any suggestions as to how to go about this would be greatly appreciated.
Mar 25 '07 #1
8 1750
Ganon11
3,652 Recognized Expert Specialist
Well, you will need two loops. The first will iterate through the array of strings. The second will check each character in the string to see if it matches your character. What would the limits of these loops be?
Mar 25 '07 #2
Red265
4 New Member
Well, you will need two loops. The first will iterate through the array of strings. The second will check each character in the string to see if it matches your character. What would the limits of these loops be?

Sorry, I don't know if I was entirely clear in my post. What I am trying to do is find the line number in a file that a particular character occurs at.

My strategy was to begin by reading all of the lines in the file into an array of strings, with each string representing an individual line within the file.

What I don't know how to do, and consequently, what I need to do next is read each string (individually) while looking for the character, stopping when the character is found at some line in the array. This line could be represented as line[j], for example with (int j) representing the string (line in the file) the character was found at) and then returning the j value.

As far as boundaries, any decently large number that would represent a maximum number of characters expected in a line of a text file would do.
200 would more than suffice for this purpose.

If there is an easier way to go about this I'm open to suggestions.

Thanks
Mar 25 '07 #3
Ganon11
3,652 Recognized Expert Specialist
My suggestion still stands - you need to loop through the array of strings and through each character in the array. This time, however, as soon as you find the correct character, you can return the 1st index - this will be the line index where the character was found.
Mar 26 '07 #4
Red265
4 New Member
Thanks for your input, Gannon. Here is my attempt at what you suggested
It compiles without error, but it doesn't output anything because my loops
must be a botched. Any suggestions on how to fix them are welcome.

Expand|Select|Wrap|Line Numbers
  1. // Some code has been removed.
  2. // line is an array of strings - i is the arrays size.
  3.  
  4. char t='a';     // character I'm looking for the first occurrence of
  5.  
  6. int n;            // will represent individual string (line) lengths
  7.  
  8. for (int j=0; j<i; j++) // counter loop for sorting through strings
  9.    {
  10.    n=line[j].length();   // finds length of individual strings
  11.  
  12.    char ch[n];        // declares array of characters
  13.  
  14.    line[j]=ch[n];      // sets array of characters equal to the string at that instance
  15.  
  16.    int k;                 // counter variable
  17.  
  18.    for (k=0; k<=n; k++)  // loop for counting characters in the char array
  19.     {
  20.         if ( ch[k] == t )  // if this character is the character I'm looking for
  21.            {
  22.        break;           // don't look anymore
  23.  
  24.                          cout << "Character "
  25.         << t
  26.           << " was found on line "
  27.         << j
  28.         << " on position "
  29.         << k
  30.         << " within the line. "
  31.         << endl;
  32.                     }
  33.     }
  34.    }
  35.  
  36. return 0;
  37. }
Mar 27 '07 #5
weaknessforcats
9,208 Recognized Expert Moderator Expert
Use the find_if() algorithm
Mar 27 '07 #6
Ganon11
3,652 Recognized Expert Specialist
The main problem you have is that you write

line[j] = ch[n];

This sets the jth element of line (a string) to the nth character of ch (a character). This will rewrite whatever information you had stored in line. Also, ch[n] is accessing a single character - should you be setting it equal to a sequence of characters (i.e. char array)? Finally, ch[n] is out of bounds - ch has elements at ch[0], ch[1], ch[2],..., ch[n-1].

You should probably write

Expand|Select|Wrap|Line Numbers
  1. ch = line[j].c_str();
or instead of this, just use

Expand|Select|Wrap|Line Numbers
  1. line[j][k];
in place of

Expand|Select|Wrap|Line Numbers
  1. ch[k];
Mar 27 '07 #7
Red265
4 New Member
Gannon, I don't think this does anything but rename the character.

Expand|Select|Wrap|Line Numbers
  1. ch = line[j].c_str();

And wouldn't something like this only work if all the columns were the same in each row?

Expand|Select|Wrap|Line Numbers
  1. line[j][k];
All I'm trying to do is figure out line and position number in a text file.
Mar 27 '07 #8
Ganon11
3,652 Recognized Expert Specialist
Gannon, I don't think this does anything but rename the character.

Expand|Select|Wrap|Line Numbers
  1. ch = line[j].c_str();
Not sure what you mean - as long as ch was declared as a char*, this should make ch the cstring representing line[j].

And wouldn't something like this only work if all the columns were the same in each row?

Expand|Select|Wrap|Line Numbers
  1. line[j][k];
The first operation done will be line[j]. This will return a string object. Then the [k] resolves. std::string has overloaded the bracket operation to access the character in the kth position, so line[j][k] will work. Instead of looping to n, though, you will have to loop until line[j].length(), ensuring that you don't go out of bounds.
Mar 28 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

12
2737
by: LongBow | last post by:
Hello all, From doing a google serach in the newsgroups I found out that a string can't be returned from a function, but using a char* I should be able to do it. I have spent most of the day...
19
2936
by: Rick | last post by:
Hi, I was wondering, can it be safely assumed that any function that returns a *char will return a NULL terminated string? Or does the function explicitly mention this always (and the ones that...
7
2302
by: wonderboy | last post by:
Hey guys, I have a simple question. Suppose we have the following functions:- //-----My code starts here char* f1(char* s) { char* temp="Hi"; return temp;
8
1864
by: shan | last post by:
How to return an two dimensional array in user defined function to main function. I think it is posible by using pointers but don't know how to code.or is there any method without using...
3
11749
by: DG is a god.... | last post by:
Dear All , This is my first post - please go easy...! I have a DLL written in C++ that has the following function exported from it -: char** ListHandles(int *processID); The processID...
14
4045
by: Shhnwz.a | last post by:
Hi, I am in confusion regarding jargons. When it is technically correct to say.. String or Character Array.in c. just give me your perspectives in this issue. Thanx in Advance.
17
11498
by: kleary00 | last post by:
Hi, I am writing a function that needs to return an array of strings and I am having some trouble getting it right. I need some help. Here is what I consider an array of 100 strings: char...
8
2189
by: darren | last post by:
Hi everybody, have a quick look at this code: ===== ===== int main(void) { string msg; makeString(msg); cout << "back in main, result = " << msg << endl;
19
2013
by: bowlderyu | last post by:
Hello, all. If a struct contains a character strings, there are two methods to define the struct, one by character array, another by character pointer. E.g, //Program for struct includeing...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7386
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7543
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.