O slideshow foi denunciado.
Seu SlideShare está sendo baixado. ×

Do the following problem- For assignment submission- use the format li (2).docx

Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio
Anúncio

Confira estes a seguir

1 de 1 Anúncio

Do the following problem- For assignment submission- use the format li (2).docx

Baixar para ler offline

Do the following problem. For assignment submission, use the format listed in the syllabus. Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the preceding character, and so on, until the entire string is reversed. Write a main program to test your function on various strings of both even and odd length.
Solution
#include #include #include int main(int argc, char* argv[]) { if (argc != 2) { printf(\"please specify exactly one argument, a string to reverse\ \"); exit(-1); } char* str = argv[1]; char a; char* front; char* rear; int len = strlen(str); front = str; rear = str + len - 1; printf(\"we will reverse the string %s in place\ \", str); while (front < rear) { a = *front; *front = *rear; *rear = a; front++; rear--; } printf(\"the reversed string is %s\ \", str); }
.

Do the following problem. For assignment submission, use the format listed in the syllabus. Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the preceding character, and so on, until the entire string is reversed. Write a main program to test your function on various strings of both even and odd length.
Solution
#include #include #include int main(int argc, char* argv[]) { if (argc != 2) { printf(\"please specify exactly one argument, a string to reverse\ \"); exit(-1); } char* str = argv[1]; char a; char* front; char* rear; int len = strlen(str); front = str; rear = str + len - 1; printf(\"we will reverse the string %s in place\ \", str); while (front < rear) { a = *front; *front = *rear; *rear = a; front++; rear--; } printf(\"the reversed string is %s\ \", str); }
.

Anúncio
Anúncio

Mais Conteúdo rRelacionado

Semelhante a Do the following problem- For assignment submission- use the format li (2).docx (20)

Mais de wviola (20)

Anúncio

Mais recentes (20)

Do the following problem- For assignment submission- use the format li (2).docx

  1. 1. Do the following problem. For assignment submission, use the format listed in the syllabus. Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the preceding character, and so on, until the entire string is reversed. Write a main program to test your function on various strings of both even and odd length. Solution #include #include #include int main(int argc, char* argv[]) { if (argc != 2) { printf("please specify exactly one argument, a string to reverse "); exit(-1); } char* str = argv[1]; char a; char* front; char* rear; int len = strlen(str); front = str; rear = str + len - 1; printf("we will reverse the string %s in place ", str); while (front < rear) { a = *front; *front = *rear; *rear = a; front++; rear--; } printf("the reversed string is %s ", str); }

×