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

C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU...
C++ PROGRAM (Pointers and char arrays) IMPORTANT NOTES: 1. CHAR ARRAY MUST BE USED. 2. YOU MUST USE POINTERS. 3. YOU MUST USE THE SWITCH STATEMENT TO EXECUTE THE PROGRAM. 4. ALL MODIFICATIONS MUST BE DONE IN THE SAME ORIGINAL CHAR ARRAY WITHOUT CREATING A NEW ONE. Write a C++ program that modifies a null teminated char array as follows: Consonants are positioned at the beginning of the string and vowels are moved to the end of the string. Example...
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...
for C program 10 by 10 char array. char 0-9 as rows and char a-j as...
for C program 10 by 10 char array. char 0-9 as rows and char a-j as collumns.
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; }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT