i'm trying to manipulate a sting it think.
i have read a string with gets(input)
then i would like to determine what character is in say postion 5?
i thought this could be done by
but i seem to be getting casting errors about pointer to int???
any tips on where i should go with this?
If you're printing just a character in a position, just use:
char *mystring = "Hello there. Some message goes here.";
printf("%c\n", mystring );
so if i have to read it from the user can i:
mystring = input;
Use fgets() instead of gets. It is safer.
You could use: 'puts( input + 4 )'.
if i use input +4 it will print everything from position 4 and beyond. is it possible to restrict this to only position 4?
Yep. If you want to print a single character, you have to use CHARACTER functions instead of STRING functions.
puts = put string
putc = put a character. Your array[x] will work here too.
ok i think i'm getting a little over my head here.
all i want to do is read the a line of text.
i need to determine the first character and place it in its own variable. then i have to determine if the next character is either a space or return. if it is a return i continue with the program. if it is a space i read the rest of the characters and store them in a string.
i think i was off track with my logic earlier.
Use fgets() to read from the standard input device (called 'stdin', which basicly means your keyboard), and store it in a buffer (an array).
Next, check the first character:
if( buffer == whatever ) variable = buffer;
Next, check the following character and do whatever.
if( buffer == ' ' ) ...it's a space, do whatever...
else if ( buffer == '\n' ) ...it's a return, do whatever...
i've been using the logic you showed but i think my declarations may be incorrect.
char input; //read from keyboard
then when i try to reference this by input i get an error from the compiler saying that int converions from pointer or something like that.
so can i convert this input to an array. i figured it is an array since a string is an array of chars right?
Here, learn from this example:
int main ( void )
printf( "Enter a string of up to 49 characters:\n" );
fgets( myArray, 50, stdin );
printf("The first character in the array is: '%c'.\n", myArray );
printf("The second character in the array is: '%c'.\n", myArray );
thank you SOOOOOOOOOOOO much.
thats what i was looking for! i've been looking for a tutorial on this all day!
now i can get on with the hard part of the assignment.
doubly linked lists!!!
well i spoke to soon! same warning message!!! then when i compile i get a segmenation fault here's my exact code.
printf("\nPrompt >> ");
fgets(userInput, 50, stdin);
What compiler are you using? There's nothing wrong with your code.
well i'm on a windows machine right now connected to a sun server using the gcc compiler