Thread: Functions and Strings Question

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    3

    Functions and Strings Question

    I am a beginning C programmer, having just reached the "Functions" chapter in my O'Reilly book Practical C by Steve Oualline. I have been assigned to make a program that replaces - with _. I have so far done this(note that some parts are not completed):
    Code:
    #include <stdio.h>
    char inputstring[100];
    char outputstring[100];
    char minus;
    char underscore;
    minus=-;
    underscore=_;
    main()
    {
    printf("Input a string of words. Make sure to include a minus sign: ");
    fgets(inputstring, sizeof(inputstring), stdin);
    printf("\nTest: %s",inputstring);
    char fun1();
    }
    There are no comments, and fun1 just means function one. Anyway, I know that I have to make a function that scans through the string inputstring, finds -'s, and replaces them. I have checked extensively through the internet, and I have not found one. That's maybe because I don't know how to look, but I would be extremely happy if someone could refer me to a website or give me a small amount of code(or a hint) on how to make this "scanning" function.

    PS: Do I have to #include <strings.h>?

    Thanks!

    -StrikeMech

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    * Indenting couldn't hurt
    * Be explicit with int main() (See the FAQ)
    * Main must return a value (See the FAQ)
    * - is an operator, you mean
    minus = '-'; and underscore = '_';
    * Your not using anything from the strings.h, so no.
    * Search this site's tutorials and faqs for more advice, http://cprogramming.com

    Have fun.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    151
    I am not sure If I got you right , but based on what I understood , you are trying to input a string with "-" characters and then change them with "_" characters. I hope I am right , If I am not do not get it wrong , because I am not trying to teach you .Anyway , sorry =)

    you can use string.h library. But it is not necessary for that program. Here is a sample :

    ---------------------------
    Code:
    #include <stdio..h>
    
    int main()
    {
    char input[80];
    gets(input);
    int i=0;
    for(i=0;i<80;i++) 
    if(input[i]='-') input[i]='_'; 
    printf("This is replaced string: %s",input);
    return 0;
    }
    ----------------------
    These will replace - with _ .
    In case you don't want it to be like this there is a function in string.h named strchr(char *string,int char) .

    This function looks for char character in string , if it finds the char , the strchr function returns the adress of the char character from the string.For example"

    char s[80]="This is my string";

    s is adressed for the character "T".

    if we :
    &s[0]=strchr(s,m);

    the s will be adressed for "m".

    ----------------
    I hope I could help , sorry if I couldnt grasp your explaination.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    @ozumsafa, Don't use gets() see the FAQ. Consider not hardcoding the size of input, your also going out of bounds of valid data (ie overstepping the end of the string).

    consider:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char input[BUFSIZ];
        size_t i = 0;
    
        fgets(input, sizeof(input), stdin);
        while(input[i] != '\0')
        {
            if(input[i] == '-')
                input[i] = '_';
    
            i++;
        }
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jul 2007
    Posts
    3
    Thanks, guys! I'll try it!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing strings to functions and modifying them
    By diddy02 in forum C Programming
    Replies: 6
    Last Post: 08-11-2008, 01:07 AM
  2. passing strings from functions as the "return" part
    By fraktal in forum C Programming
    Replies: 8
    Last Post: 12-13-2005, 01:38 AM
  3. Creating Functions that use Format control Strings
    By tzuchan in forum C Programming
    Replies: 6
    Last Post: 03-29-2004, 12:50 PM
  4. Functions that return strings? (help)
    By Nuke in forum C++ Programming
    Replies: 2
    Last Post: 05-18-2003, 09:04 PM
  5. Using Strings with Functions
    By DJH in forum C Programming
    Replies: 10
    Last Post: 10-19-2001, 04:40 PM