Question

an example of a code using pointers in C.

an example of a code using pointers in C.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<stdio.h>

#include<stdlib.h>

#include<stdbool.h>

// get the string from user and point it by originalString

void getString(char *originalString);

// print the string

void displayString(char *);

// return true if the character is to be converted

bool determineIfConvert(char);

// convert character

void convert (char *);

int main()

{

    // create char array of size 11

    char *originalString = (char *)malloc(11 * sizeof(char));

    char *convertedString = (char *)malloc(11 * sizeof(char));

   

    printf("Enter your string:\n");

   

    // get the string fom user

    getString(originalString);

   

    int i;

    char *ptr1, *ptr2;

   

    for( i = 0 ; *(originalString + i) != '\0' ; i++)

    {

        // store the current char in convertedString

        *(convertedString + i) = *(originalString + i);

       

        // if the character is a small letter

        if(determineIfConvert(*(originalString + i)))

            // convert it into capital letter

            convert((convertedString + i));

    }

   

    printf("\nOriginal string is\n");

    displayString(originalString);

   

    printf("\n\nConverted string is\n");

    displayString(convertedString);

   

    return 0;

}

// get the string from user and point it by originalString

void getString(char *originalString)

{

    // read upto 10 character in the string

    scanf("%10s", originalString);

}

// print the string

void displayString(char *str)

{

    // point ptr to str

    char *ptr = str;

   

    // print string character by character

    for(; *ptr != '\0'; ptr++)

        printf("%c",*ptr);

}

// return true if the character is to be converted

bool determineIfConvert(char ch)

{

    int ascii = (int)ch;

   

    // if ch is a lower case character

    if(ascii >= 97 && ascii <= 122)

        return true;

       

    return false;

}

// convert character

void convert (char *str)

{

    int ascii = (int)(*str);

   

    // set the ascii of capital letter

    ascii -= 32;

   

    *str = (char)ascii;

}


Sample Output

Enter your string: abcdeF Original string is abcdeF Converted string is ABCDEF

Add a comment
Know the answer?
Add Answer to:
an example of a code using pointers in C.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I am trying to write the following code using pointers and traversal by pointers instead of...

    I am trying to write the following code using pointers and traversal by pointers instead of indexing (in C++). The following code is correct: void shift(int arr[], int n) { int temp = arr[n - 1]; int temp1; for (int i = 0; i < n; i++) { temp1 = arr[i]; arr[i] = temp; temp = temp1; } } This is my ATTEMPT at writing it with pointers (and traversal by pointers!) but I know it is wrong. I believe...

  • Code in C #### Array of pointers 1. Define a structure. 2. Allocate a *dynamic* array...

    Code in C #### Array of pointers 1. Define a structure. 2. Allocate a *dynamic* array of **pointers** to structures. What is the type of the array variable? 3. Sketch the array on the worksheet. 3. Assign values in a loop. 4. Free all dynamic memory. Mind the order to avoid leaving dangling pointers!

  • I need to convert this code to using pointers only. No arrays other than to declare...

    I need to convert this code to using pointers only. No arrays other than to declare the pointers. I am having serious difficulty with this. void encryptStrings(char strings[NUM_STRINGS][STRING_LENGTH], int key) {    for (int i = 0; i < NUM_STRINGS; i++)    {        for (int j = 0; j < STRING_LENGTH; j++)        {            strings[i][j] += key;        }    } }

  • Write a function in C to convert a decimal number to binary using recursion and pointers....

    Write a function in C to convert a decimal number to binary using recursion and pointers. For example: convertToBinary(10) -> 1010 long convertToBinary ( int num ) { }

  • Requirements: You must use pointers to process all arrays. Code a C function that accepts pointers...

    Requirements: You must use pointers to process all arrays. Code a C function that accepts pointers to two arrays of integers and their respective sizes. This function should check and count how many matchings entries in both arrays and return the count back to main for display. (important! Matchings entries do not have to be located in the same position and you are required to assume that the two arrays are of different sizes…also assume no duplicate within either arrays)

  • Working with pointers in C++. I need a brief step by step explanation of the code...

    Working with pointers in C++. I need a brief step by step explanation of the code to help me understand why the output is: 5 4 3 2 1 What will be displayed? Explain each step of the code and the related use of pointers. #include <iostream> using namespace std; int* myfun(int*); int main() {    int x[5] = { 1, 2, 3, 4, 5 };    int i, *p;    p = myfun(x);    for (i = 0; i < 5; i++)        ...

  • code has to be in C++... Need a completely documented application that uses classes and POINTERS...

    code has to be in C++... Need a completely documented application that uses classes and POINTERS in conjunction with overload operators..... Write a program that puts out dates in various formats. Dates are commonly printed In several different formats in business correspondence , Two of the more common formats are : 07/21/1955 ; july 21, 1955 ..Write a program that reads a date in the first format and prints that date in the second format. make sure to use pointers...

  • Write a C program to add and subtract any two given integer numbers using pointers

    1- Write a C program to add and subtract any two given integer numbers using pointers.  2- Write a C program to find the factorial using a function and pointers.  3- Write a C program to find the square of an Integer number using a function and pointers.  4- Write a C program to find the area and perimeter of a rectangle using a function and pointers.  5- Write a C program to find the larger of two integers numbers using...

  • Lab 8 CS102 Lab 8 Hands on programming with Pointers The goal of this lab exercise...

    Lab 8 CS102 Lab 8 Hands on programming with Pointers The goal of this lab exercise is to apply the knowledge of pointers and File input output operations. Program 1 Short description: Declare, initialize and use pointers for different data types. Detailed Description: Declare 4 variables of type's int, float, double and char respectively and initialize them to some logically correct values. Declare pointers to all these variables. Initialize the pointers to point to these variables respectively Print the values...

  • // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define...

    // Header code for stack // requesting to create source code using C++ #ifndef Stack_h #define Stack_h #include <stdio.h> #include <string> #include <iostream> using namespace std; class Stack { public: Stack(); ~Stack(); bool empty(); string top(); void push(const string &val); void pop(); void display(ostream &out); private: class Node { public: string word; Node *next; }; Node *tos; }; #endif Header file provided above. PLS create source code for functions declared in the header. If changes are need no make in...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT