Question

Hello, I have my piece of C programming code and I have a pointer initialized to...

Hello, I have my piece of C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'. The code is below:

#include <stdio.h>

int main ()

{
  
char array[80];

printf("Enter a string: ");
scanf("%[^\n]", &array);
printf("%s\n", array);

char * p;   

p = array;  
  
  
}

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

Problem : C programming code and I have a pointer initialized to point to my array and I need help to display the contents of my array using a while loop or do-while loop. The stop condition should be when the pointer reaches '\0'.

Program :

#include <stdio.h>
#include <string.h>
int main (){
   char array[80];
   printf("Enter a string: ");   //taking the string from the user
   scanf("%[^\n]", &array);
   printf("printing the string normally : %s\n\n", array); //printing the string normally
   char * p;    //declaring the variable
   p = array; //pointer p stores first element address of the string
  
   int i = 0;
   printf("printing the string using the pointers : ");
   while(*(p + i) != '\0'){
       printf("%c",*(p + i)); //prints the string using pointers
       i++;
   }  
}

OUTPUT

Add a comment
Know the answer?
Add Answer to:
Hello, I have my piece of C programming code and I have a pointer initialized to...
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
  • getchar() redirect to input textfile in ASCI C programming. Hello, underneath is the code I did...

    getchar() redirect to input textfile in ASCI C programming. Hello, underneath is the code I did and it works. However, my professor is asking to use getchar() and have an input text file to redirect it. How would i do that on my code? Thank you for the help. #include <stdio.h> #include <string.h> int main() { char string[100]; int c = 0, Lcase[26] = {0}, x; int Ucase[26] = {0};    printf("Enter a string\n"); gets(string); while (string[c] != '\0') {...

  • 2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics...

    2. Pointer arithmetic: a) Write a printString function that prints a string (char-by-char) using pointer arithmetics and dereferencing. b) Write a function “void computeMinMax(double arr[], int length, double * min, double * max)” that finds the minimum and the maximum values in an array and stores the result in min and max. You are only allowed to use a single for loop in the function (no while loops). Find both the min and the max using the same for loop....

  • C Programming I have this cost estimation program, but I want to add to it more...

    C Programming I have this cost estimation program, but I want to add to it more functions to be more accurate and more useful. I want to add to the program an array and a loop that can help me with the materials that can be use to the building, as ceramic, wood and concrete and extra functions. ########## #include <stdio.h> int main(void) { float wid,len,yrs; int metersq = 2000; int floors,rooms,win,doors,restrooms,umet; char floortype[20]; char ti[20]; int woodfloor = 25;...

  • In Programming language C - How would I convert my words char array into a string...

    In Programming language C - How would I convert my words char array into a string array so I can use the strcmp() function and alphabetically sort the words? #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main(int argc, char*argv[]){ int i =0; int j =0; int count =0; int length = strlen(argv[1]); for(i =0; i < length; i++){ if(isalpha(argv[1][i]) == 0 ||isdigit(argv[1][i] != 0)){ count ++; } printf("%c",argv[1][i]); } char *strings; int wordNum =0; int charNum =0; strings...

  • Programming C....... function code is clear but compile is wrong .. I input 8 and compiled...

    Programming C....... function code is clear but compile is wrong .. I input 8 and compiled 2 0 1 1 2 3 5 8 13.. look first number 2 is wrong. The correct answer 8 is 0 1 1 2 3 5 8 13 21.. Need fix code to compile correctly.. Here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[1000]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for (...

  • Programming in C: I am trying to modify this linked list to be doubly linked list....

    Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...

  • I have a question on an assignment for C++. I have my code available here and...

    I have a question on an assignment for C++. I have my code available here and just want someone to look over it using MS VS 2010 express, for that is what my instructor is using. Write a program that mimics a calculator. The program should take as input two integers and the opreation to be performed. It should then output the numbers, the operator, and the result. (For division, if the denominator is zero, output an appropriate message.) //**This...

  • A)Correct this code and explain what was corrected // C code - For Loop / Array...

    A)Correct this code and explain what was corrected // C code - For Loop / Array Population // This program will populate integers into an array, and then display those integers. // Developer: Johnson, William CMIS102 // Date: Mar 4, 2020 // Global constants #define INTLIMIT 10 #include <stdio.h> // This function will handle printing my name, class/section number, and the date void printStudentData() { char fullName[19] = "William J. Johnson"; char classNumber[9] = "CMIS 102"; char sectionNumber[5] = "4020";...

  • C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure...

    C programming Rewrite the following code replacing the else-if construct with a switch statement. Make sure you test your code. Supplied code #include <stdio.h> int main(void) { char ch; int countA = 0; int countE = 0; int countI = 0; printf("Enter in a letter A, E, or I.\n"); scanf(" %c", &ch); //Replace the following block with your switch if(ch == 'E' || ch == 'e') countE++; else if(ch == 'A' || ch == 'a') countA++; else if(ch == 'I'...

  • URGENT. Need help editing some C code. I have done most of the code it just...

    URGENT. Need help editing some C code. I have done most of the code it just needs the following added: takes an input file name from the command line; opens that file if possible; declares a C struct with three variables; these will have data types that correspond to the data types read from the file (i.e. string, int, float); declares an array of C structs (i.e. the same struct type declared in point c); reads a record from the...

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