PDA

View Full Version : Programming in C help



nikos
10-14-2010, 01:06 AM
This is the assignment and im not quite sure why its not giving the output correctly.. can anyone help? Thank you in advance.

Write a program that in a loop prompts the user to enter a text line and then reverses the line and prints out:

1. The reversed line.
2. The reversed line with:
1. capital letters replaced by by lowercase letters,
2. leading spaces removed,
3. streaks of two or more spaces replaced by single spaces.
3. OPTIONAL: One per line, indented by a tab, the tokens occurring in the line created in 2). [A token is a maximal sequence of non-white characters, i.e. ' ', '\n', '\t']


The loop is terminated when the user enters a line without tokens.

Here is an example of the interactions in a possible run:

Enter line [CR to exit] : Roses are VERY red
der YREV era sesoR
der yrev era sesor
der
yrev
era
sesor
Enter line [CR to exit] : VioLETS ARE Blue
eulB ERA STELoiV
eulb era steloiv
eulb
era
steloiv
Enter line [CR to exit] :





//My Attempt:


#define SIZE 256
#include <stdio.h>
#include <string.h>

//this function will reverse the first and last letters.
void reverseLine (int n, char *a[n]) {
int left, right;
for (left = 0, right = n - 1; left < right; left++, right--) {
int temp;
temp = a[left];
a[left] = a[right];
a[right] = temp;
}
}
int main () {
char buffer[SIZE];
int n = strlen(buffer);

while (1) {
printf("Enter a phrase [Non Token to Quit] :");
fgets (buffer, SIZE, stdin);
reverseLine(n,&buffer);
printf("\n%s\n", buffer);
}
return 0;
}