1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Printing string-help.

  1. Sep 14, 2012 #1
    1. The problem statement, all variables and given/known data

    I am trying to write up code to reverse the order of a string i.e.

    input: and
    output: dna
    For some reason I am getting this compiling warning but I don't know whats wrong with my code...



    2. Relevant equations



    3. The attempt at a solution

    Code (Text):


    #include <stdio.h>
    #include <string.h>
    #define MAX_LENGTH 20
    int main(int argc, char * argv[]){


       char s[MAX_LENGTH];

       printf("Enter String\n");
       if( fgets(s,MAX_LENGTH,stdin) != NULL){
          tnirp(s);

       }
     

    // original string
    printf("\n, %s",s);

    // the number of characters in a string
    int i = 0;
    while(s[i] != 0){
    i++;
    }


    // Reverse string
    printf("\n");
    int idx;
    for(idx = 0;idx < i;){
    i--; // I place this here instead of in the for loop,
    // because the for loop will subtract 1 from i
    // before 'string' can even begin to print, thus
    // the last char will not be printed.
    printf("%c", s[i]);
    }

    return 0;


     

    and this is the compiler warning
    warnings being treated as errors
    In function ‘main’:
    warning: implicit declaration of function ‘tnirp’


    any help is great!
     
  2. jcsd
  3. Sep 14, 2012 #2

    uart

    User Avatar
    Science Advisor

    The error message is fairly self explanatory. It's saying you've written " tnirp(s);", which implies that tnirp() is a function, however it is not defined anywhere.
     
  4. Sep 14, 2012 #3
    Ok so If I have tnirp() at the start of the code ie

    Code (Text):


    #include <stdio.h>
    #include <string.h>
    #define MAX_LENGTH 20
    int main(int argc, char * argv[]){
    tnirp()


       char s[MAX_LENGTH];

     

    but that does not fix it ,,, I have to submit this in an hour so I really need help thanks.
     
  5. Sep 14, 2012 #4

    uart

    User Avatar
    Science Advisor

    I don't quite understand your question, is tnirp() a function that you are supposed to write? Where is the code for it.
     
  6. Sep 14, 2012 #5
    Write a function

    void tnirp(char s[]);

    which takes a string s as a parameter and prints the characters of s in reverse order. Only characters up to the first '\0' or the first '\n' should be included, and a newline character '\n' should be printed at the end. Place the code for this function in a file called tnirp.c. Now write a main() function with the following contents.

    #define MAX_LENGTH 20
    int main(int argc, char * argv[]){
    char s[MAX_LENGTH];

    printf("Enter String\n");
    // fgets returns NULL if it gets only a ctrl ^d
    if( fgets(s,MAX_LENGTH,stdin) != NULL ){
    tnirp(s);
    }
    return 0;
    }
     
  7. Sep 14, 2012 #6

    Mark44

    Staff: Mentor

    You have not written the tnirp function. You have its declaration (or prototype) here
    Code (Text):
    void tnirp(char s[]);
     
    and you have the invocation (or call) to this function here (in your if block)
    Code (Text):
    tnirp(s);
     
    but you don't show a definition for this function. The definition would consist of the instructions in C for printing a string in reverse order.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Printing string-help.
  1. C++ strings help (Replies: 2)

  2. Not printing (Replies: 4)

  3. Help with C++ strings (Replies: 3)

Loading...