Allright, my assignment was to read in user data in the form name,age,sport. I then had to reverse the letters in the name, and then print all of the inputted data out last-first in the order sport age reversedname. For some reason my code to reverse the name string isn't doing anything. I used a multidimensional string array for each object (name, age, and sport) and I think an error with that might be why.
Please tell me what I'm doing wrong...I've spent a I am sillyI am sillyI am sillyI am silly-ton of time on this proggy and I'm sick of it.
Here's the section of code that should reverse the name. The full program is below it in case you want to compile it, or if I've messed up somewhere else. This does compile, btw. Thanks for any help you can give me.
Code:
/*Reversing the name by the use of two temp strings*/
for(x=1; x>=i; x++){
char tempStr1[strlen(name[x])];
char tempStr2[strlen(name[x])];
for(j; j<strlen(name[x]); j++)
tempStr1[j] = name[x][j];
for(y=0; y<strlen(tempStr1); y++){
tempStr2[y] = tempStr1[strlen(tempStr1)-y];
}
for(k; k<strlen(name[x]); k++)
name[x][j] = tempStr2[j];
}
Code:
#include <stdio.h>
#include <string.h>
int main()
{
char input[50];
char name[10][50];
char age[10][50];
char sport[10][50];
int i=1, j=1,k=1;
int x, y, z, w;
/*Speaking to the user*/
printf("Enter up to ten people The format is name,age,sport.");
printf("\nInvalid input will be ignored.\n");
/*Loop for input from the user*/
fgets(input, sizeof(input), stdin);
while(strlen(input) !=1 && i<10){
input[strlen(input)-1] = '\0';
sscanf(input, "%s,%s,%s", &name[i], &age[i], &sport[i]);
/*Handling of lack of entry. If they skip something, it doesn't
increment the counter so it is written over*/
if(name[i] != 0 && age[i] != 0
&& sport[i] != 0 ) i++;
fgets(input, sizeof(input), stdin);
}
printf("Thanks\n\n");
/*Reversing the name by the use of two temp strings*/
for(x=1; x>=i; x++){
char tempStr1[strlen(name[x])];
char tempStr2[strlen(name[x])];
for(j; j<strlen(name[x]); j++)
tempStr1[j] = name[x][j];
for(y=0; y<strlen(tempStr1); y++){
tempStr2[y] = tempStr1[strlen(tempStr1)-y];
}
for(k; k<strlen(name[x]); k++)
name[x][j] = tempStr2[j];
}
/*Print out the info in format sport age reverseName*/
for (i; i>=1; i--){
printf("%s %s %s\n", &sport[i], &age[i], &name[i]);
}
/*End the program*/
return (0);
}