Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

written this code but having trouble

Status
Not open for further replies.

Maulkin

Programmer
Apr 8, 2002
19
AU
I wrote this code it is surposed to take an input string like

a"bcd"e"fgh"i

and extract the strings that occur inside the " " and tsore them in an array once the whole input string has been examined the function dumpstring should print out all the extracted strings but instead it just prints the last extracted string.

so if the input above was used the output would be

string1
fgh
string2
fgh

but it should be

string1
bcd
string2
fgh

any help would be appreciated

Code:
#include <ctype.h>
#include <stdio.h>
#include <string.h>

int numberStrings = 0;
char in[256];
char extract[100];
char stored[100][256];
char key = '&quot;';
char quote[2];
char *p1, *p2;

getString()
{
	int p = 0, record = 0, i = 0, j = 0, c = 0;	
	while (in[p] != NULL)
	{
		if (strcmp(key, in[p]) == 0)
		{
			if (record == 0)
			{
				record = 1;
				p++;
			}else
			{
				record = 0;
				extract[i] = NULL;
				i = 0;
				while (extract[c] != NULL)
				{	
					stored[j][c] = extract[c];
					c++;
				}
				stored[j][c] = NULL;
				c++;
				numberStrings++;
				j++;	
				c = 0;
			}	
		}
		
		if (record == 1)
		{
			extract[i] = in[p];
			i++;
		}
		p++;
	}
}

dumpString()
{
	int a, b = 0, d = 0, count = 0;
	for (a = 0; a < numberStrings; a++)
	{
		printf(&quot;\nString %u\n&quot;, count);
		printf(&quot;\n%s&quot;, stored[b]);
		b++;
		count++;
	}	
}

main()
{	
	printf(&quot;\nPlease enter the data to be examined: &quot;);
	scanf(&quot;%s&quot;, in);

	getString();
	dumpString();
	
	exit(0);
}
 
Hi, problem is there in ur strcmp function. You should pass the address of the character array to that function not just the characters itself.

If your case replace the if statement which contains the strcmp() in the getString() like the following.

if(key == in[p])

it will do everything.

One more change in your code.

stored[j][c] = NULL;
c++; // unwanted, remove it.
numberStrings++;
j++;
c = 0;

Regrads,
Maniraja S
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top