Ir para conteúdo
Fórum Script Brasil
  • 0

tem como alguém corrigir? ou dizer se ta certo?


Felipe Xavier

Pergunta

Exercícios – vetores e strings

Escreva programa para testar as seguintes funções e descreva o que elas fazem. Faça seu programa principal imprimir o que retorna de cada função.

1- Protótipo: int mystrlen (char s[]);

int mystrlen( char s[])

{ int i;

for (i=0; s[i] != ‘’; i++);

return (i);
}
2- Protótipo: int mystrcmp (char s1[], char s2[]);
int mystrcmp (char s1[], char s2[])

{ int i = 0;

while ( s1[i] == s2[i])

if (s1[i++] == '') return (0);

return (s1[i] - s2[i]);

}
3- Protótipo: void swap (int a, int B);
void (int a, int b)

{ int temp;

temp = a;

a = b;

b = temp;

}
4- Protótipo: void swapv (int a[], int b[], int n);
void (int a[], int b[], int n)

{ int i, temp;

for (i=0; i

{

temp = a[i];

a[i] = b[i];

b[i] = temp;

}

}
5- Protótipo: void mystrcpy (char s[], char t[]);
void mysrtcpy(char s[], char t[])

{ int i=0;

while((s[i]=t[i])!= '')

i++;

}
6- Protótipo: void append (char s1[], char s2[]);
void append (char s1[], char s2[])

{ int i = j = 0;

while (s1[i] != '')

i++;

while ((s1[i++] = s2[j++])!='');

}
7- Protótipo: void print_string (char s[]);
void print_string(char s)

{

int i=0;

while(s[i]!=NULL)

putchar(s[i++]);

}
Eu fiz isso aqui, eu preciso saber se ta certo e se não tiver alguém pode me ajudar?
#include <stdio.h>
#include <stdlib.h>

int mystrlen (char s[]);
int mystrcmp (char s1[], char s2[]);
void swap (int a, int b);
void swapv (int a[], int b[], int n);
void mystrcpy (char s[], char t[]);
void print_string (char s[]);
void append (char s1[], char s2[]);

int a = 3;
int b = 7;
int i1[] = { 1,2,3,4,5,6,7,8,9,10 };
int i2[] = { 10,9,8,7,6,5,4,3,2,1 };

int main(int argc, char *argv[])
{
  char s[] = { "fernando" }; //Cria uma string
  char s2[] = { "fernando" };

  int i , j; // cria uma variavel int para receber o retorno da função
  i = mystrlen(s); // Chama a função para determinar o tamanho da string e colocar o valor em i
  printf("Tamanho da string = %i",i); //Imprime o tamanho da string
  printf("\n"); // pula uma linha
  j = mystrcmp(s,s2); // Chama a função comparar as string e colocar o valor em j
  printf("0 se as string forem iguais  = %i",j); //Imprime j
  printf("\n"); // pula uma linha

  swap(a,b);

  swapv(i1,i2,10);

  mystrcpy(s,s2);

  append(s,s2);

  print_string(s);

  system("PAUSE");    //pausa o sistema
  return 0;
}
/*Função para determinar o tamanho de uma string
recebe uma string de parametro
retorna um int com o tamanho da string
*/
int mystrlen( char s[])             
{
    int i;                                    
    for (i=0; s[i] != ''; i++);  
    return (i);                             
}                                             

/*Funçao para comparar string
retorna 0 se as string forem iguais
*/

int mystrcmp (char s1[], char s2[])       
{
    int i = 0;                                        
    while ( s1[i] == s2[i])                    
      if (s1[i++] == '')
        return (0);
    return (s1[i] - s2[i]);
}
/* recebe dois inteiros e troca entre eles */
void swap(int a, int b)
{
   int temp;
   temp = a;
   a = b;
   b = temp;
}

/* função para trocar dois vetores
Recebe como parametros dois vetores e o tamanho deles
não retorna nada*/
void swapv(int a[], int b[], int n)    
{    int i, temp;    
     for (i=0; i<n; i++)    
     {    
       temp = a[i];    
       a[i] = b[i];    
       b[i] = temp;
     }
}
/* Copia a string */
void mystrcpy(char s[], char t[])
{    int i=0;
     while((s[i]=t[i])!= '')
     i++;
}

/*escreve a string de tras para frente*/
void  append (char s1[], char s2[])       
{     int i = 0;
      int j = 0;                                   
      while (s1[i] != '')                        
          i++;                                           
       while ((s1[i--] = s2[j++])!='')              
            ;
}

/* imprime uma string na tela*/
void  print_string(char s[])
{
    int i=0;
    while(s[i]!=NULL)
    putchar(s[i++]);
}

Link para o comentário
Compartilhar em outros sites

0 respostass a esta questão

Posts Recomendados

Até agora não há respostas para essa pergunta

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,2k
    • Posts
      652k
×
×
  • Criar Novo...