1 Attachment(s)
How to duplicating the Array & copy tokenPtr to an array
Dear fellows, I have few questions here.
(1) Duplicating the Array
This is part of my code for duplicating the array from
name[j] to secondname[x]
Code:
// Read from a file entered by user while(fgets( name[j], 255, fpPtr )!=NULL){
printf( "Name %s\n", name[j] ); // Verfication
strcpy( name[j], secondname[x] );
printf( "Second name %s\n", secondname[x] ); // Verification
j++;
x++;
}
When I run the program, how comethe string for secondname[x] does not contain any value inside?
(2) Copy tokenPtr to an array
In the followings function prototype, I want to copy the string contain inside the token pointer to an array named tempsort[x], but this process was not success when I use the strcpy method, kindly show me the correct way of dealing with token pointer in the strtok.
Code:
void namesort ( char name[LEN][SIZE], int last ){ int i, j, x; // indices of array
char *tokenPtr; // create char pointer;
char tempsort[LEN][SIZE];
for (i = last ; i > 0 ; i--){
for (j = 0 ; j <= last ; j++){
printf("sorting %s",name[j]); // For checking
for (x = 0 ; x <= last ; x++){
tokenPtr = strtok (name[j], ".");
while (tokenPtr != NULL){
printf("tokenPtr: %s\n", tokenPtr) ; // For checking
strcpy( tokenPtr,tempsort[x] ); // assint tokenPtr to tempsort
printf("tempsort: %s\n", tempsort[x]) ; // For checking
tokenPtr = strtok ( NULL, "."); // get next token
} // end while
// printf("%s",name[j]) ; // For checking
}
}
}
} // End function prototype
Thanks in advance :redface:
error: invalid initializer with char s[] = name[j];
Code:
#include <stdio.h>
#include <string.h>
#define SIZE 255
#define LEN 255
void reverseWords(char *s);
void reverse(char *begin, char *end);
void namesort ( char name[LEN][SIZE], int last );
void showSortingResult( char name[LEN][SIZE], int last );
// Function main begins program execution
int main( int argc, char *argv[])
{
char name[LEN][SIZE];
char copyofname[LEN][SIZE];
int i,j=0, x=0; // indices of array
int f; // file counter
int last; // index of last item in array
FILE *fpPtr; // fpPtr is pointer
// if there is a filename from commandline
if ( argc > 1 ) {
for (f=1; f<=argc-1; f++){
// fopen opens file, Read text from a filename entered by user
fpPtr = fopen ( argv[f], "r" );
// Read from a file entered by user
while(fgets( name[j], SIZE, fpPtr )!=NULL){
// printf( "name array = %s", name[j] ); // Verified
strcpy( copyofname[x], name[j] ); // Copy string name -> secondname
// printf( "secondname = %s\n", secondname[x] ); // Verified secondname == name
j++; // array increment
x++; // array increment
}
fclose( fpPtr ); // Closes the file
} // end for
// Sent to namesorting Function Prototype to sort by symbol, A-Z. a-z
printf( "\n---- Sorting Process ---- \n" ); //Verification
// last = j-1;
// for (i = last ; i > 0 ; i--){
// for (j = 1 ; j <= i ; j++){
// char s[] = "i.like.this.program.very.much";
char s[] = name[j]; // this part contain error!!
char *temp = s;
printf("Before reverse: %s\n", s);
reverseWords(s);
printf("1st time reverse: %s\n", s);
reverseWords(s);
printf("2nd time reverse: %s\n", s);
getchar();
// } // end j loop
// } // end i loop
// Output result using Function prototype
printf( "\n---- Display the result ---- \n" ); //Verification
showSortingResult( name, j-1 );
printf( "\n---- End of file input part ---- \n" );//Verification
} // end if
else {
// Get manually entered data from user
fpPtr = stdin;
for(j = 0 ; !feof( fpPtr ) ; j++ ){
// Read next record
fgets( name[j], 255, fpPtr );
strcpy( copyofname[x], name[j] );
x++;
}
// Sent to namesorting Function Prototype to sort by symbol, A-Z. a-z
printf("\n---- Sorting Process ---- \n"); //Verification
// reverseWords( name, j-1 ); // Reverse words in the domain
// namesort ( name, j-1 ); // Sort by a to z from the back
// reverseWords( name, j-1 ); // Reverse back the domain (original state)
// Output result using Function prototype
printf("\n---- Display the result ---- \n"); //Verification
showSortingResult( name, j );
printf("\n---- End of manually input part ---- \n"); //Verification
} // end if else
return 0;
} // End Main
// Function to reverse words in the web domain address
void reverseWords(char *s)
{
char *word_begin = s;
char *temp = s; /* temp is for word boundry */
// STEP 1 of the above algorithm
while( *temp )
{
temp++;
if (*temp == '\0')
{
reverse(word_begin, temp-1);
}
else if(*temp == '.')
{
reverse(word_begin, temp-1);
word_begin = temp+1;
}
} /* End of while */
// STEP 2 of the above algorithm
reverse(s, temp-1);
} // End function prototype
// Function to reverse any sequence starting with
// pointer begin and ending with pointer end
void reverse(char *begin, char *end)
{
char temp;
while (begin < end)
{
temp = *begin;
*begin++ = *end;
*end-- = temp;
}
} // End function prototype
// Function Prototype to Sorting by symbol, A-Z, a-z THIS IS CASE SENSITIVE!!! CORRECT IT!!!
void namesort ( char name[LEN][SIZE], int last ){
int i,j; // indices of array
char hold[LEN];
for (i = last ; i > 0 ; i--){
for (j = 1 ; j <= i ; j++){
if (strcmp(name[j],name[j - 1]) < 0){
strcpy(hold,name[j]) ;
strcpy(name[j],name[j - 1]) ;
strcpy(name[j - 1],hold) ;
}
}
}
} // End function prototype
// Function to Printf result
void showSortingResult( char name[LEN][SIZE], int last )
{
int i; // indices of array
// Write array to output
for (i = 0 ; i <= last ; i++)
printf("%s",name[i]) ; // For checking
} // End function prototype
Dear all, my I know what's wrong with this error? I want to copy the value from name[j] to the pointer s.