the last for loop i declared it inside, which doesnt work as a c program its c++ i need to declare it out side how do i do that?

#include <stdio.h>  
#include <string.h>  
#include <ctype.h>  
  
// Our function declaration saying it takes one string pointer and returns one string pointer.  
char * flipstring(char *stringtoflip);  
  
int main() {  
    // Strings to hold our lines (forward and backward)  
    char string[256];  
    char flipped[256];  
  
    // Our file pointers (one in and one out)  
    FILE * infile;  
    FILE * outfile;  
  
    // Open files to valid txt files  
    infile = fopen("input.txt","r");  
    outfile = fopen("output.txt","w");  
  
    // Loop through the input file reading each line up to 256 chars  
    while (fgets(string,256,infile) != NULL) {  
        printf("The value read in is: %s\n", string);  
  
        // Flip the string and copy it to our "flipped" string  
        strcpy(flipped,flipstring(string));  
  
        // Write "flipped" to output  
        printf("The value written is: %s\n",flipped);  
        fputs(flipped,outfile);  
    }  
  
    // Close files  
    fclose(infile);  
    fclose(outfile);  
  
    return 0;  
}  
  
// Flips strings by taking incoming string, loop in reverse  
// and write each char to reverse string. Return reverse string.  
char * flipstring(char *stringtoflip) {  
    char reverse[256];  
  
    int j = 0;  
  
    // Loop through each char of our string in reverse  
	
    for(int i = strlen(stringtoflip)-1; i>=0; i--)  
    {  
        // If it is a newline, just ignore for now.  
        if (stringtoflip[i] != '\n') {  
            reverse[j]=stringtoflip[i];  
            j++;  
        }  
    }  
  
    // Terminate the new reverse string.  
    reverse[j] = '\0';  
  
    // Return reverse string back to main()  
    return reverse;  
}

i need to declare it outside

so pick up your laptop and go declare it outside. just make sure it's not raining, or that you have a good awning at least.

sorry. :P

actually i dont understand your question. what are you trying to declare "outside" ? and why do you think a for loop works one way in C++ but differently in C? what do you think it should be doing, that it's not?

>which doesnt work as a c program its c++
There's nothing wrong with the loop in C99. If you're getting an error about declaring i in the initialization clause, it's likely that you're compiling as C89 rather than C99. In that case you change your loop to this:

int i;
int j = 0;
  
// Loop through each char of our string in reverse  
for(i = strlen(stringtoflip)-1; i>=0; i--)  
{  
    // If it is a newline, just ignore for now.  
    if (stringtoflip[i] != '\n') {  
        reverse[j]=stringtoflip[i];  
        j++;  
    }  
}

However, I take issue with your claim that this code "works" in C++. It's quite broken in both languages as you're returning a pointer to a local variable in the flipstring function. You're accessing memory that no longer belongs to you, and the least that is likely to happen is that you'll get garbage output for the flipped string.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.