Reverse String: Fixing Compiling Warning

  • Thread starter Thread starter charmedbeauty
  • Start date Start date
  • Tags Tags
    Printing
Click For Summary

Discussion Overview

The discussion revolves around a coding issue related to reversing a string in C programming. Participants are addressing a specific compiler warning regarding an undefined function, as well as providing guidance on how to implement the required functionality.

Discussion Character

  • Homework-related
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant is attempting to reverse a string but encounters a compiler warning due to an implicit declaration of the function 'tnirp'.
  • Another participant points out that 'tnirp' is not defined anywhere in the code, which is causing the warning.
  • A suggestion is made to define the 'tnirp' function before calling it in the main function, but this does not resolve the issue.
  • Some participants express confusion about whether 'tnirp' is a function that the original poster is supposed to implement, asking for clarification on its definition.
  • Participants propose writing a function 'void tnirp(char s[])' that prints the characters of the string in reverse order, emphasizing the need to include a newline character at the end.
  • There is a reiteration that the original poster has provided a declaration for 'tnirp' but has not included its definition, which is necessary for the code to compile successfully.

Areas of Agreement / Disagreement

Participants generally agree that the issue stems from the lack of a definition for the 'tnirp' function. However, there is some uncertainty regarding the original poster's understanding of how to implement this function.

Contextual Notes

There are limitations in the original code, including the absence of the 'tnirp' function definition and potential misunderstandings about its implementation. The discussion does not resolve these issues fully.

Who May Find This Useful

This discussion may be useful for individuals learning C programming, particularly those dealing with string manipulation and function definitions.

charmedbeauty
Messages
266
Reaction score
0

Homework Statement



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 what's wrong with my code...



Homework Equations





The Attempt at a Solution



Code:
#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!
 
Physics news on Phys.org
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.
 
uart said:
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.

Ok so If I have tnirp() at the start of the code ie

Code:
#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.
 
I don't quite understand your question, is tnirp() a function that you are supposed to write? Where is the code for it.
 
uart said:
I don't quite understand your question, is tnirp() a function that you are supposed to write? Where is the code for it.

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;
}
 
charmedbeauty said:
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;
}

You have not written the tnirp function. You have its declaration (or prototype) here
Code:
void tnirp(char s[]);

and you have the invocation (or call) to this function here (in your if block)
Code:
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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
Replies
7
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K