Hi, I have a program and i cant seem to get it to work. I need to write a program that can take up to 50 strings and then reverse them...

The actual question is:
Write a program that will prompt the user for two strings of characters(each string has maximum 50 characters) , assemble them into a single string and then print the string out reversed.

So far i have this

#include<stdio.h>
#include<iostream.h>
#include<string.h>

void Reverse(string &InputString);
void Reverse(string &InputString2);

void CharacterSwap(char First, char Second);

void main()
	{
	char InputString;
	
	printf("Please enter the First string... ");
	scanf("%s", &InputString);

	printf("Please enter the Second string... ");
	scanf("%s", &InputString2);
	
	printf("You entered: ");
	for(int i = 50; i < InputString.length(); i++)
		printf("InputString[i]");
	
	Reverse(InputString);
		
	printf("The string reversed is : ");
	for(int j = InputString.length(); j > 50 ; j--)
			printf("InputString[j]");	

	
	printf("", endl endl);
	
	}


void Reverse(string &InputString)
	{
	int Begin = 1,
		End = InputString.length();
	
	while(Begin < End)
		{
		CharSwap(InputString[Begin], InputString[End]);
		Begin++;
		End--;	
		}
	}


	void CharSwap(char& First, char& Second)
	{
	char Temp;
	First = Second;
	Temp = First;
	Second = Temp;
	}

<< moderator edit: added [code][/code] tags >>

I also have a nother source that works but i cant get the second one to work
please i need help soon:

#include <stdio.h>
 #include <string.h>

int main ( ) {
    char str[50];
	char string[50];
    int i, add, add1;

    printf("Enter First string   : ");
    scanf("%49[^\n]%*c", str);
	printf("Enter Second string   : ");
    scanf("%49[^\n]%*c", string);

    printf("In reverse order : ");
    add = strlen(str), add1 = strlen(string);
	for (i = (add + add1) - 1; i >= 0; i--)
         putchar(str[i], string[i]);
    putchar('\n');

    return 0;
 }

thanxs

Dani AI

Generated

A compact, safe C solution and a few troubleshooting notes. The goal: read two input lines (each up to 50 chars), concatenate them, then print the concatenated string reversed. posted attempts that exposed common pitfalls (mixing C/C++ I/O, wrong types, off-by-one errors, and incorrect swap/putchar usage). The code below is a concise, portable C approach that avoids those problems and checks buffer lengths.

#include <stdio.h>
#include <string.h>

#define MAX 50

int main(void) {
    char s1[MAX + 2], s2[MAX + 2];          /* room for newline + null */
    char combined[2 * MAX + 1];             /* max 100 chars + null */

    if (!fgets(s1, sizeof s1, stdin)) return 0;
    if (!fgets(s2, sizeof s2, stdin)) return 0;

    /* strip trailing newlines */
    size_t l1 = strlen(s1); if (l1 && s1[l1-1] == '\n') s1[--l1] = '\0';
    size_t l2 = strlen(s2); if (l2 && s2[l2-1] == '\n') s2[--l2] = '\0';

    /* safe concat (snprintf always null-terminates) */
    if (l1 + l2 > 2 * MAX) { /* defensive check */
        /* truncate second string so result fits */
        size_t keep = 2 * MAX - l1;
        s2[keep] = '\0';
        l2 = keep;
    }
    snprintf(combined, sizeof combined, "%s%s", s1, s2);

    /* reverse in place */
    size_t n = strlen(combined);
    for (size_t i = 0; i < n / 2; ++i) {
        char tmp = combined[i];
        combined[i] = combined[n - 1 - i];
        combined[n - 1 - i] = tmp;
    }

    puts(combined);
    return 0;
}

Quick troubleshooting notes:

  • Use fgets to allow spaces; avoid scanf("%s") or gets (unsafe).
  • Always allocate room for the terminating NUL and check combined length to prevent overflow.
  • putchar prints one character at a time; passing two arguments is an error.
  • A correct swap uses a temporary variable; be careful not to overwrite a value before saving it.
  • Test with empty strings, exactly 50-char inputs, and inputs with spaces to confirm behavior.

This pattern is simple, safe, and portable across modern C compilers.

I figured it out so thanxs for anyone that was going to help me

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.