Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

sorting strings function with pointers


  • Please log in to reply

#1
flipsb14

flipsb14

    New Member

  • Member
  • Pip
  • 3 posts
I have to write a program that takes a char pointer, scans the strings into the pointer then passes them to a function which then passes them to a string compare function. The string compare function is supposed to work like strcmp in the stings.h library. Here is the code if anybody can figure this please let me know.

#include <stdio.h>
int strcompare(char *str1, char *str2);
void stringsort(char *strings[]);
int main(){

char *strings[20];
int i;
for(i=0; i<20; i++){
scanf("%s", &strings[i]);
}
stringsort(strings);
}
void stringsort(char *strings[]){
char *temp="~~~~";
char order[20][30];
int i, j, m, k;
for(j=0; j<20; j++){
for(i=0; i<20; i++){
if(strcompare(strings[i], temp)<0){
temp = strings[i];
}

else{
break;
}
}
for(m=0; m<30; m++){
order[j][m] = temp;
strings[j][m]='~';
temp[m]='~';
}
}
for(i=0; i<20; i++){
printf("%s\n", order[i]);
}
printf("\n");
}
int strcompare(char *str1, char *str2){
int i, j, k, m, n, best;
if( str1 < str2){
for(j=0;(str2[j] = str1[j])!='\0';j++){
;
}
return -1;
}
if( str1== str2){
return 0;
}
if( str1 > str2){
return 1;
}
}
  • 0

Advertisements







Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP