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...

  • 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;        }    } }

  • 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...

  • using c and pointers only 1. You will read in two strings from a file cp4in_1.txt...

    using c and pointers only 1. You will read in two strings from a file cp4in_1.txt at a time (there will be 2n strings, and you will read them until EOF) and then do the following. You alternately take characters from the two strings and string them together and create a new string which you will store in a new string variable. You may assume that each string is no more than 20 characters long (not including the null terminator),...

  • Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array...

    Write in C code: Define an array f=[23,-12,34,-56,-10,12,19], using call-by-reference (use pointers), to pass the array to a function, named Summation. In main: Define array, pass array, print out the array and the result returned from Summation on screen. In function Summation: 1)take array and size from main 2) sum each element according to the following rules 3) if the value of f(i) is positive, add 2*f(i) to the sum 4)if the value of f(i) is negative, add -3*f(i) to...

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
ADVERTISEMENT