Reverse String: Fixing Compiling Warning

  • Thread starter charmedbeauty
  • Start date
  • Tags
    Printing
You would need to put this function definition in a .c file, say tnirp.c, and then you would need to compile and link tnirp.c with the rest of your code.In summary, the conversation is about a compiling warning that the individual is receiving while trying to write code to reverse the order of a string. They are unsure why they are receiving the warning and ask for help in understanding and fixing it. The solution involves writing a function called tnirp() that takes a string as a parameter and prints it in reverse order. This function needs to be defined and placed in a .c file, and then compiled and linked with the rest of the code.
  • #1
charmedbeauty
271
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
  • #2
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.
 
  • #3
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.
 
  • #4
I don't quite understand your question, is tnirp() a function that you are supposed to write? Where is the code for it.
 
  • #5
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;
}
 
  • #6
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.
 

What is a reverse string and why is it useful?

A reverse string is a function that takes a string as an input and outputs the same string but in reverse order. This can be useful for tasks such as data manipulation, encryption, and sorting.

What is a "compiling warning" and how does it relate to reverse strings?

A compiling warning is a message that is displayed by a compiler when it encounters an issue in the code that may cause errors or unexpected behavior. In the context of reverse strings, it could indicate an error in the code that needs to be fixed in order for the function to work correctly.

How can I fix a compiling warning when working with reverse strings?

The best way to fix a compiling warning is to carefully review the code and identify the issue. This could include missing or incorrect syntax, using incorrect data types, or not handling edge cases. Once the issue is identified, it can be fixed by making the necessary changes to the code.

Are there any common errors or mistakes when coding reverse strings?

Yes, some common errors or mistakes when coding reverse strings include using incorrect syntax, not properly handling special characters or spaces, and not accounting for different data types. It is important to thoroughly test the code and handle any potential edge cases to ensure the function works correctly.

Is there a specific coding language that is best for writing reverse strings?

No, there is not a specific coding language that is best for writing reverse strings. Reverse strings can be written in many different programming languages, such as Java, Python, C++, and more. The best language to use will depend on the specific project and the programmer's familiarity with the language.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
941
  • Engineering and Comp Sci Homework Help
Replies
3
Views
878
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
994
  • Engineering and Comp Sci Homework Help
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
668
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
753
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
Back
Top