0% found this document useful (0 votes)
26 views14 pages

CP 19 Pointers and Strings

This document discusses pointers and strings in C. It explains that a string is an array of characters ending with a null character. A string name or character array name can be a character pointer pointing to the base address. It provides examples of declaring and initializing strings, using scanf and printf with strings, and how strings are stored in memory with character pointers.

Uploaded by

BRAHMJOT SINGH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views14 pages

CP 19 Pointers and Strings

This document discusses pointers and strings in C. It explains that a string is an array of characters ending with a null character. A string name or character array name can be a character pointer pointing to the base address. It provides examples of declaring and initializing strings, using scanf and printf with strings, and how strings are stored in memory with character pointers.

Uploaded by

BRAHMJOT SINGH
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

Pointers and strings

String: is a collection of characters and it ends with \0 character. Or character


array
ex. char str[6]= {‘H’, ’e’, ’l’, ’l’, ‘\0’};

A string name or a character array name can be a character pointer pointing to base address .

char str[6]=“hello”;
str=1000; // str is a character pointer
Each character in the string str takes 1 byte of memory space
Pointers and strings

int main()
{
char str[50];
No need for ‘&’
scanf ("%s",str);
printf ("%s",str); Why??

return 0;
}
Pointers and strings

int main()
{ Input:
char str[30]; hello
scanf(“%s”, str);
printf(“%s”, str);
return 0; Output:
} hello
Pointers and strings

‘b’
‘b’ 2000
1000
int main() { ‘r’
‘r’
1001 2001
char abcd[ ] = "bro!"; ‘o’
‘o
1002 2002
printf("%s",abcd); ’‘!’
‘!’ 2003
1003
return 0; ‘\0’
‘\0’
1004 2004
} .
Output:
bro!
Pointers and Character Array
‘b’
‘b’ 2000
1000
int main() { ‘r’
‘r’
1001 2001
char abcd[8] = "bro!"; ‘o’
1002 2002
printf("%s",abcd); ‘!’
‘!’
1003 2003
return 0; ‘\0’
‘\0’
1004 2004
} . 2005 0
Output: 0
2006
bro! 2007
0
Character Pointer

int main()
1000 0 1 1 0 0 0 0 1
{
1001 0 1 1 0 0 0 1 0
char *ptr = “abc”; 0 1 1 0 0 0 1 1
1002
printf(“%s”, ptr); 1003 0 0 0 0 0 0 0 0

}
Character Pointer

int main()
{ ptr 1000 97

1001 98
char *ptr = “abc”; 1000
1002 99
printf(“%s”, ptr); 3000 1003 0
}

Output:
abc
Character Pointer

int main()
{ ptr 1000 97

1001 98
char *ptr = “abc”; 1001
1000
1002 99
ptr = ptr + 1; 3000 - 3001 1003 0
printf(“%s”, ptr);
} Output:
bc
Character Pointer

int main()
999 GV
{ ptr
1000 97
char *ptr = “abc”; 999
1000 1001 98

ptr = ptr - 1; 1002 99


3000 - 3001
1003 0
printf(“%s”, ptr);
Output:
} i) If GV = 0
ii) If GV != 0
Q) Write a c program to Calculate the length of the string
#include <stdio.h>
int calculateLength(char* ch) // ch = base address of array str1
int calculateLength(char*);
( &str1[0] )
{
void main()
int ctr = 0;
{
while (*ch != '\0')
char str1[25];
{
int l;
ctr++;
printf("\n\n Pointer : Calculate the length of the
ch++;
string :\n");
}
printf("----------------------------------------------\n");
return ctr;
}
printf(" Input a string : ");
fgets(str1, sizeof str1, stdin);

l = calculateLength(str1);
printf(" The length of the given string %s is : %d ", str1, l-
1);
printf("\n\n");

}
Q) Write a program in C to print a string in reverse using a pointer.

Test Data :
Input a string : NAVEEN
Expected Output :

Pointer : Print a string in reverse order :


------------------------------------------------
Input a string : NAVEEN

Reverse of the string is : NEEVAN


Q) Write a program in C to print a string in reverse using a pointer.
#include <stdio.h>
int main()
{ while(*stptr)
{
char str1[50]; stptr++;
char revstr[50]; i++;
char *stptr = str1; }
while(i>=0)
char *rvptr = revstr; {
int i=-1; stptr--;
printf("\n Print a string in reverse order :\n"); *rvptr = *stptr;
rvptr++;
printf("------------------------------------------------\n");
--i;
printf(" Input a string : "); }
scanf("%s",str1); *rvptr='\0';
printf(" Reverse of the string is : %s\n\n",revstr);
return 0;
}
Q) Write a program in C to count the number of vowels and consonants in a string
using a pointer.

Test Data :
Input a string: string
Expected Output :

Number of vowels : 1

Number of constant : 5
//program in C to count the number of vowels and consonants in a string using a pointer.
#include <stdio.h> ctrV=ctrC=0;
int main() while(*pt!='\0')
{
{ if(*pt=='A' ||*pt=='E' ||*pt=='I' ||*pt=='O' ||*pt=='U'
char str1[50]; ||*pt=='a' ||*pt=='e' ||*pt=='i' ||*pt=='o' ||*pt=='u')
char *pt; ctrV++;
else
int ctrV,ctrC; ctrC++;
printf("\nCount the number of vowels and pt++; //ptr is increasing for searching the next character
consonants :\n"); }
printf("-----------------------------\n");
printf(" Number of vowels : %d\n Number of consonants :
printf(" Input a string: "); %d\n",ctrV,ctrC-1);
fgets(str1, sizeof str1, stdin);
return 0;
}
//assign address of str1 to pt
pt=str1;

You might also like