Question

In: Computer Science

I have this program in C that takes three char arrays that each have a first...

I have this program in C that takes three char arrays that each have a first and last name. I have two functions that reverese the name and change it to all upper case. I have the program completeed but need to change both functions to use pointers instead of arrays. I will bold the functions I need to use pointers.

#include <stdio.h>

void upper_string(char []);

int main()
{
char name1[100]="John Smith";
char name2[100]="Mary Cohen";
char name3[100]="Carl Williams";
upper_string(name1);// calling upper case function
upper_string(name2);
upper_string(name3);
printf("Names are in upper case \n\"%s\"\n""%s\"\n""%s\"\n", name1,name2,name3);
reverse(name1);// calling reverse string function
reverse(name2);
reverse(name3);
  
getch();
}

void upper_string(char s[]) {
int c = 0;

while (s[c] != '\0') {
if (s[c] >= 'a' && s[c] <= 'z') {
s[c] = s[c] - 32;
}
c++;
}
}
// function to reverse the string
void reverse(char string[]){

char temp;
int i, j = 0;
i = 0;
j = strlen(string) - 1;

while (i < j) {
temp = string[i];
string[i] = string[j];
string[j] = temp;
i++;
j--;
}

printf("\nReverse string is :%s", string);
}

Solutions

Expert Solution

Hi,

I have modified the code and highlighted the code changes below.


#include <stdio.h>
#include <string.h>
void upper_string(char *);
void reverse(char *string);

int main()
{
char name1[100]="John Smith";
char name2[100]="Mary Cohen";
char name3[100]="Carl Williams";
upper_string(name1);// calling upper case function
upper_string(name2);
upper_string(name3);
printf("Names are in upper case \n\"%s\"\n""%s\"\n""%s\"\n", name1,name2,name3);
reverse(name1);// calling reverse string function
reverse(name2);
reverse(name3);
  
}
void upper_string(char *s) {
int c = 0;
while (*(s + c) != '\0') {
if (*(s + c) >= 'a' && *(s + c) <= 'z') {
*(s + c) = *(s + c) - 32;

}
c++;
}
}
// function to reverse the string
void reverse(char *string){
char temp;
int i, j = 0;
i = 0;
j = strlen(string) - 1;
while (i < j) {
temp = *(string + i);
*(string + i) = *(string + j);
*(string + j) = temp;

i++;
j--;
}
printf("Reverse string is :%s\n", string);
}

Output:

sh-4.2$ gcc -o main *.c                                                                                                                                                                                                                                

sh-4.2$ main                                                                                                                                                                                                                                           

Names are in upper case                                                                                                                                                                                                                                

"JOHN SMITH"                                                                                                                                                                                                                                           

MARY COHEN"                                                                                                                                                                                                                                            

CARL WILLIAMS"                                                                                                                                                                                                                                         

Reverse string is :HTIMS NHOJ                                                                                                                                                                                                                          

Reverse string is :NEHOC YRAM                                                                                                                                                                                                                          

Reverse string is :SMAILLIW LRAC


Related Solutions

Write a complete C program to do the following:(i) Define a function Nodeptr CreateDLL(char str[])which takes...
Write a complete C program to do the following:(i) Define a function Nodeptr CreateDLL(char str[])which takes a string as parameter and creates a Doubly Linked List of characters and returns the pointer to the first node. (ii) Define a function int IsPalindrome(Nodeptr first)to check whether the string represented by the above doubly linked list pointed to by first, is a palindrome or not and return 1/0 accordingly. Do not use any additional data structure.Write a main function to read a...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
There are three things wrong with this program. List each. print("This program takes three numbers and...
There are three things wrong with this program. List each. print("This program takes three numbers and returns the sum.") total = 0 for i in range(3):     x = input("Enter a number: ")     total = total + i print("The total is:", x)
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which...
Program Assignment 1 C++ please Instructions This assignment will require the use of three arrays, which will be used in parallel. Create a program that keeps track of the sales of BBQ sauces for a company. The company makes several different types of sauces, Original, Sticky Sweet, Spicy, Sweet Heat, Hickory Bourbon and Smokey Mesquite. One array will contain the names of the different BBQ sauces. This array will be initialized from a text file with the 6 different names....
c++ Write the definition of a function named ‘isLower’ that takes as input a char value...
c++ Write the definition of a function named ‘isLower’ that takes as input a char value and returns true if the character is lowercase; otherwise, it returns false.•Print the message “The character xis lowercase” when returned value above is true, and vice versa.
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1;...
Translate the following C++ program to Pep/9 assembly language. const char chConst = '+'; char ch1; char ch2; int main() { cin.get(ch1); cin.get(ch2);    cout << ch1 << chConst << ch2;    return 0; }
Write a program that takes two integer arrays a and b of size n from the...
Write a program that takes two integer arrays a and b of size n from the user, the use a method product to find the product of a and b and return the results after storing them in an array c, then prints the returned results to the screen. (Note: c[i] = a[i] * b[i], for i = 0, ..., n-1) Sample Output: Enter the size of your arrays: 5 Enter the integer values of the first array a: 1...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and double pointers? The assignment asks to Write a program that displays a 2D multiplication table based on row and column value specified by the user. Perform a data validation to ensure the number of rows and columns given by the user exist on the interval [1, 10]. If possible, protect against inputs that contain symbols that would normally crash the program (i.e. letter, symbols,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT