manipulating strings

This is a discussion on manipulating strings within the C Programming forums, part of the General Programming Boards category; i'm trying to manipulate a sting it think. i have read a string with gets(input) then i would like to ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    manipulating strings

    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

    puts(input[4]);

    but i seem to be getting casting errors about pointer to int???

    any tips on where i should go with this?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you're printing just a character in a position, just use:

    char *mystring = "Hello there. Some message goes here.";

    printf("%c\n", mystring[4] );

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    so if i have to read it from the user can i:

    char input[50];
    char *mystring;

    mystring = input;

    gets(input);

    puts(mystring[4]);

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use fgets() instead of gets. It is safer.

    You could use: 'puts( input + 4 )'.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    if i use input +4 it will print everything from position 4 and beyond. is it possible to restrict this to only position 4?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    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.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    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[0] == whatever ) variable = buffer[0];

    Next, check the following character and do whatever.

    if( buffer[1] == ' ' ) ...it's a space, do whatever...
    else if ( buffer[1] == '\n' ) ...it's a return, do whatever...

    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    i've been using the logic you showed but i think my declarations may be incorrect.

    char input[50]; //read from keyboard


    then when i try to reference this by input[4] i get an error from the compiler saying that int converions from pointer or something like that.

    so can i convert this input[50] to an array. i figured it is an array since a string is an array of chars right?

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here, learn from this example:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main ( void )
    {
    	char myArray[50];
    
    	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[0] );
    	printf("The second character in the array is: '%c'.\n", myArray[1] );
    	return 0;
    }
    
    
    Quzah.
    Hope is the first step on the road to disappointment.

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    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!!!

  12. #12
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    well i spoke to soon! same warning message!!! then when i compile i get a segmenation fault here's my exact code.

    #include <stdio.h>
    #include <string.h>

    int main(void)
    {
    char userInput[50];

    printf("\nPrompt >> ");
    fgets(userInput, 50, stdin);

    printf("%c", userInput[0]);
    printf("\n");

    return 0;
    }

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What compiler are you using? There's nothing wrong with your code.

    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    Unregistered
    Guest
    well i'm on a windows machine right now connected to a sun server using the gcc compiler

  15. #15
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    ttt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Manipulating C strings
    By black_spot1984 in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2008, 10:23 AM
  2. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  3. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  4. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  5. manipulating strings
    By student2005 in forum C++ Programming
    Replies: 5
    Last Post: 01-08-2004, 02:21 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21