A apresentação está carregando. Por favor, espere

A apresentação está carregando. Por favor, espere

Fundamentos de Programação 1

Apresentações semelhantes


Apresentação em tema: "Fundamentos de Programação 1"— Transcrição da apresentação:

1 Fundamentos de Programação 1
Linguagem C “ ‘String’ por parâmetro” Slides 10. Prof. SIMÃO Jean Marcelo SIMÃO

2 Vetor de Caracteres (“Puro”) por Parâmetro.
#include <stdio.h> #include <stdlib.h> #include <string.h> void invertestring ( char str[] ) { char aux [100]; int i = 0; int k = 0; int j; while ( str[i] != '\0' ) { i = i + 1; } printf ("A string tem %d caracteres. \n", i); k = i - 1; for ( j = 0; j < i; j = j + 1) aux [ j ] = str [ k ]; k = k - 1; } aux [ j ] = '\0'; strcpy ( str, aux ); int main() { char nome[100] = "Jean Marcelo Simao"; printf ( " O inverso de %s eh: \n", nome ); invertestring ( nome ); printf ( " %s \n", nome ); system ( "Pause" ); return 0; }

3 #include <stdio.h>
#include <stdlib.h> #include <string.h> void invertestring ( char str [ ] ) { char aux [ 100 ]; int i = 0; int k = 0; int j; while ( str[ i ] != '\0' ) { i = i + 1; } // pode-se utilizar aqui a função pré-definida strlen()! printf ( "A string tem %d caracteres. \n", i ); k = i - 1; for ( j = 0; j < i; j++) { aux [ j ] = str [ k ]; k = k - 1; } aux [ j ] = '\0'; strcpy ( str, aux );

4 int main ( ) { char nome [ 100 ] = " Jean Marcelo Simao "; printf ( " O inverso de %s eh: \n ", nome ); invertestring ( nome ); printf ( " %s \n", nome ); system ( "Pause" ); return 0; }

5 Vetor de Caracteres por Parâmetro via Ponteiro.
#include <stdio.h> #include <stdlib.h> #include <string.h> void invertestring ( char *str ) { char aux [100]; int i = 0; int k = 0; int j; while ( str[i] != '\0' ) { i = i + 1; } printf ( "A string tem %d caracteres. \n", i ); k = i - 1; for ( j = 0; j < i; j++ ) aux [ j ] = str [ k ]; k = k - 1; } aux [ j ] = '\0'; strcpy (str, aux); int main() { char nome [ 100 ] = "Jean Marcelo Simao"; printf ( " O inverso de %s eh: \n", nome ); invertestring ( nome ); printf ( " %s \n", nome ); system ( "Pause" ); return 0; }

6 #include <stdio.h>
#include <stdlib.h> #include <string.h> void invertestring ( char *str ) { char aux [ 100 ]; int i = 0; int k = 0; int j; while ( str [ i ] != '\0' ) { i++; } printf ( "A string tem %d caracteres. \n", i ); k = i - 1; for ( j = 0; j < i; j++ ) aux [ j ] = str [ k ]; k = k - 1; } aux [ j ] = '\0'; strcpy ( str, aux );

7 Elaborar um exemplo com matriz de caracteres, por exemplo: inverter um conjunto de strings

8 os da apostila, por exemplo.
Fazer exercícios: os da apostila, por exemplo.


Carregar ppt "Fundamentos de Programação 1"

Apresentações semelhantes


Anúncios Google