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...
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.
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; }
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.
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,...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;     ...
*Answer in C program* #include <stdio.h> int main() {      FILE *fp1;      char c;      fp1= fopen ("C:\\myfiles\\newfile.txt", "r");      while(1)      {         c = fgetc(fp1);         if(c==EOF)             break;         else             printf("%c", c);      }      fclose(fp1);      return 0; } In the program above which statement is functioning for opening a file Write the syntax for opening a file What mode that being used in the program. Give the example from the program Referring to...
hey I have this program written in c++ and I have a problem with if statement...
hey I have this program written in c++ and I have a problem with if statement {} brackets and I was wondering if someone can fix it. //Name: Group1_QueueImplementation.cpp //made by ibrahem alrefai //programming project one part 4 //group members: Mohammad Bayat, Seungmin Baek,Ibrahem Alrefai #include <iostream> #include<stdlib.h> using namespace std; struct node { string data; struct node* next; }; struct node* front = NULL; struct node* rear = NULL; struct node* temp; void Insert() {     string val;    ...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT