Mail Archives: djgpp/1999/11/11/19:01:56
one problem, when you use 'char *string", you define a pointer to a character,
but you don't actually allocate any space for the string. Try replacing it
with 'char string[80]' and see if that helps.
Actually, it's prolly better to use a name like 'InStr' or 'tempStr', etc
instead of 'string' which is a likely candidate for being a reserved word with
some languages.
Also, since string is now an array, in your scanf, you won't need the '&' now.
Marius Myburg wrote:
> Can anyone please tell me why this program doesn't want to work? It
> compiler fine, but when I run it, after I have entered the requested text,
> it returns with an error at the strlength=strlen(string) line. Also, when
> I remove that line, set string length to say 8 and enter a 8-character
> string, it returns with an error when I want to access the string's
> individual characters, at t1 = string[i].
>
> #include <conio.h>
> #include <string.h>
> #include <stdio.h>
> #include <stdlib.h>
>
> char *string;
> int strlength;
>
> void getstring()
> {
> clrscr();
> printf("%s", "Enter a string > ");
> scanf("%s", &string);
> printf("%s", &string);
> strlength = strlen(string); -- error here
> };
>
> int scanstring()
> {
> char t1; // temp char
>
> for (int i=0; i<strlength; i++)
> {
> t1 = string[i]; -- error here if first error
> bypassed
> for (int j=i; j<strlength; j++) // scan to end
> {
> if (string[j] == t1) // match!
> {
> printf("%s\n", "match !");
> }
> }
> }
> }
>
> void main()
> {
> getstring();
> scanstring();
> getch();
> }
>
> mariusm AT parys DOT lia DOT net
- Raw text -