Skip to main content
deleted 1 character in body
Source Link
qwerty
  • 64
  • 12
#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include <string.h>
void swaps(char*s,char*t)
#include<string.h>
{
    char*a = t;
    strcpy(t,s);
    strcpy(s,a);
}
int main(void)
{
    int n=GetInt();
    char* a[n];
    int c=0;
    for(int i=0;i<n;i++)
    {
        a[i]=GetString();
    }
    puts("");
    for(int j=0;j<n;j++)                                  
    {
        for(int i=0;i<n-1;i++)
        {
            c=strcmp(a[i],a[i+1]);
            if(c>0)
            {
                swaps(a[i],a[i+1]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
         printf("%s\n",a[i]);   
    }
    return 0;
}

I am trying to arrange strings in alphabetic order using pointers.I was able to get desired output using 2d char arrays but not able to get the correct output using pointers.

#include <stdio.h>
#include <string.h>
#include <cs50.h>
void swaps(char*s,char*t)
#include<string.h>
{
    char*a = t;
    strcpy(t,s);
    strcpy(s,a);
}
int main(void)
{
    int n=GetInt();
    char* a[n];
    int c=0;
    for(int i=0;i<n;i++)
    {
        a[i]=GetString();
    }
    puts("");
    for(int j=0;j<n;j++)                                  
    {
        for(int i=0;i<n-1;i++)
        {
            c=strcmp(a[i],a[i+1]);
            if(c>0)
            {
                swaps(a[i],a[i+1]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
         printf("%s\n",a[i]);   
    }
    return 0;
}

I am trying to arrange strings in alphabetic order using pointers.I was able to get desired output using 2d char arrays but not able to get the correct output using pointers.

#include <stdio.h>
#include <string.h>
#include <cs50.h>
#include <string.h>
void swaps(char*s,char*t)
{
    char*a = t;
    strcpy(t,s);
    strcpy(s,a);
}
int main(void)
{
    int n=GetInt();
    char* a[n];
    int c=0;
    for(int i=0;i<n;i++)
    {
        a[i]=GetString();
    }
    puts("");
    for(int j=0;j<n;j++)                                  
    {
        for(int i=0;i<n-1;i++)
        {
            c=strcmp(a[i],a[i+1]);
            if(c>0)
            {
                swaps(a[i],a[i+1]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
         printf("%s\n",a[i]);   
    }
    return 0;
}

I am trying to arrange strings in alphabetic order using pointers.I was able to get desired output using 2d char arrays but not able to get the correct output using pointers.

added 148 characters in body
Source Link
Cliff B
  • 69.3k
  • 4
  • 33
  • 56
#include <stdio.h>
#include <string.h>
#include <cs50.h>
void swaps(char*s,char*t)
#include<string.h>
{
    char*a = t;
    strcpy(t,s);
    strcpy(s,a);
}
int main(void)
{
    int n=GetInt();
    char* a[n];
    int c=0;
    for(int i=0;i<n;i++)
    {
        a[i]=GetString();
    }
    puts("");
    for(int j=0;j<n;j++)                                  
    {
        for(int i=0;i<n-1;i++)
        {
            c=strcmp(a[i],a[i+1]);
            if(c>0)
            {
                swaps(a[i],a[i+1]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
         printf("%s\n",a[i]);   
    }
    return 0;
}

#include <stdio.h> #include <string.h> #include <cs50.h> void swaps(chars,chart) #include<string.h> { chara = t; strcpy(t,s); strcpy(s,a); } int main(void) { int n=GetInt(); char a[n]; int c=0; for(int i=0;i<n;i++) { a[i]=GetString(); } puts(""); for(int j=0;j<n;j++)
{ for(int i=0;i<n-1;i++) { c=strcmp(a[i],a[i+1]); if(c>0) { swaps(a[i],a[i+1]); } } } for(int i=0;i<n;i++) { printf("%s\n",a[i]);
} return 0; } II am trying to arrange strings in alphabetic order using pointers.I was able to get desired output using 2d char arrays but not able to get the correct output using pointers.

#include <stdio.h> #include <string.h> #include <cs50.h> void swaps(chars,chart) #include<string.h> { chara = t; strcpy(t,s); strcpy(s,a); } int main(void) { int n=GetInt(); char a[n]; int c=0; for(int i=0;i<n;i++) { a[i]=GetString(); } puts(""); for(int j=0;j<n;j++)
{ for(int i=0;i<n-1;i++) { c=strcmp(a[i],a[i+1]); if(c>0) { swaps(a[i],a[i+1]); } } } for(int i=0;i<n;i++) { printf("%s\n",a[i]);
} return 0; } I am trying to arrange strings in alphabetic order using pointers.I was able to get desired output using 2d char arrays but not able to get the correct output using pointers.

#include <stdio.h>
#include <string.h>
#include <cs50.h>
void swaps(char*s,char*t)
#include<string.h>
{
    char*a = t;
    strcpy(t,s);
    strcpy(s,a);
}
int main(void)
{
    int n=GetInt();
    char* a[n];
    int c=0;
    for(int i=0;i<n;i++)
    {
        a[i]=GetString();
    }
    puts("");
    for(int j=0;j<n;j++)                                  
    {
        for(int i=0;i<n-1;i++)
        {
            c=strcmp(a[i],a[i+1]);
            if(c>0)
            {
                swaps(a[i],a[i+1]);
            }
        }
    }
    for(int i=0;i<n;i++)
    {
         printf("%s\n",a[i]);   
    }
    return 0;
}

I am trying to arrange strings in alphabetic order using pointers.I was able to get desired output using 2d char arrays but not able to get the correct output using pointers.

Source Link
qwerty
  • 64
  • 12

Arranging Strings using pointers

#include <stdio.h> #include <string.h> #include <cs50.h> void swaps(chars,chart) #include<string.h> { chara = t; strcpy(t,s); strcpy(s,a); } int main(void) { int n=GetInt(); char a[n]; int c=0; for(int i=0;i<n;i++) { a[i]=GetString(); } puts(""); for(int j=0;j<n;j++)
{ for(int i=0;i<n-1;i++) { c=strcmp(a[i],a[i+1]); if(c>0) { swaps(a[i],a[i+1]); } } } for(int i=0;i<n;i++) { printf("%s\n",a[i]);
} return 0; } I am trying to arrange strings in alphabetic order using pointers.I was able to get desired output using 2d char arrays but not able to get the correct output using pointers.