हिंदी माध्यम नोट्स
Categories: C Language in hindi
String : Advance Examples (Part 2) in c language in hindi , some string example in advance level
some string example in advance level , String : Advance Examples (Part 2) in c language in hindi :-
String : Advance Examples (Part 1) मे हमने string related कुछ advance उदाहरन को देखा था |अब इस article मे , string के कुछ और advance उदाहरण को देखेगे जिससे की string के concept को अच्छी तरह से समजा सके |
उदाहरण 1:
Write a program to copy a string into other string (Without strcmp () function ) .
इस उदहारण मे , एक string को किसी और string मे copy करने करना है लेकिन string के standard function strcmp() का use नहीं करना है |
Explanation
1.सबसे पहले , एक string variable ‘statement’ को declare करगे | और यूजर द्वारा दिए गये input को इस string assign करेगे |
2. उसके बाद string की length को strlen() function से calculate करते हैऔर इसे length मे assign करते है |
3.उसके बाद एक दूसरी string ‘output’ को declare करते है जिसमे string को copy करना है |
3.उसके बाद , copy() को call करगे जिसमे statement और output को pass करते है |इसमें function को call by reference से भुलाया जाता है |
copy() ,
1.सबसे पहले , strlen() से string ‘statement’ की size को calculate कर लेते है |
2.looping होती है |loop तब तक चलता है जब तक ‘statement’ मे null pointer नहीं आ जाता है |
loop मे ,
2.1- ‘statement[i] की value को output [i] पर assign कर देते है |
3. जब loop terminate हो जाता है तब string output के length+1 position पर null pointer assign कर देते है |
Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void copy (char * , char *);
void main ()
{
char statement[100];
printf(“Enter String “);
scanf(“%s”, statement);
char output [100];
copy(statement , output );
printf(“Copied String Value \n”)
printf(“%s”, output );
getch();
}
void copy(char *s , char *c)
{
int length= strlen(s);
int i;
for(i=0;i<length;i++)
{
c[i]=s[i];
}
c[length+1]=’\0′;
}
आउटपुट होगा :
Enter String parth
Copied String Value
parth
उदाहरण 2:
Write a program to calculate number of alphabets , digit or special character in string .
इस उदहारण मे , कसी string मे से character , डिजिट और special character के number को find आउट करना होता है |
Explanation
1.सबसे पहले , एक string variable ‘statement’ को declare करगे | और यूजर द्वारा दिए गये input को इस string assign करेगे |
2. उसके बाद string की length को strlen() function से calculate करते हैऔर इसे length मे assign करते है |
3. loop चलाया जाता है |loop तब तक चलता है जब तक की string मे null pointer नहीं आ जाता है |
3.i. condition check की जाती है |
अगर charterer ‘alphabets’ है तब alpha मे increment होगा |
अगर character ‘digit ‘ है तब count मे increment होगा |
और character ‘special character ‘ होता है तब ‘s’ मे increment होगा|
Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char statement[100];
printf(“Enter String “);
gets(statement);
int alpha =0;
int s=0;
int count=0;
int length = strlen(statement);
for(i=0;i<length;i++)
{
if(statement[i]=’/0′ && statement[i]== ‘ ‘ )
{
s++;
}
elseif(statement[i] >=’0′ && statement[i] <= ‘9’)
{
count++;
}
elseif((statement[i] >=’a’ && statement[i] <= ‘z’)|| (statement[i] >=’A’ && statement[i] <= ‘Z’))
{
alpha++;
alpha++;
}
else
else
{
;
}
printf(“Number of Character = %d \n”, alpha ) ;
printf(“Number of Digit = %d\n “, digit ) ;
printf(“Number of Special Character = %d \n “, s ) ;
getch();
}
आउटपुट होगा :
Enter String parth4546@
Number of Character = 5
Number of Digit = 4
Number of Special Character = 1
उदाहरण 3:
Write a program to eliminate leading blank space in string
इस उदहारण मे , किसी string के पहले आने वाले वाले blank space को eliminate करना है |
Explanation
1.सबसे पहले , एक string variable ‘statement’ को declare करगे | और यूजर द्वारा दिए गये input को इस string assign करेगे |
2. उसके बाद string की length को strlen() function से calculate करते हैऔर इसे length मे assign करते है |
3.सबसे पहले string मे स्थित white space को calculate किया जाता है |
4 loop मे , condition check की जाती है |
अगर string का first character ‘space’ है तब string के सभी character left side मूव हो जाते है |
4. ये condition तब तक चलती है जब तक string के first character blank space नहीं हो जाता है |
Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char statement[100];
printf(“Enter String : “);
gets(statement);
int i,j,check;
int length = strlen(statement);
check=0;
while (statement[check]=’ ‘ && statement[check] == ‘\t’ && statement[check] == ‘\n’ )
{
check++;
}
if(check!=0)
if(check!=0)
{
i=0;
i=0;
while (statement[i+check]==’\n’)
{
statement[i]=statement[i+check];
statement[i]=statement[i+check];
i++;
}
statement[i]=’/0′;
}
printf(“string without forward space”);
puts(statement);
getch();
}
आउटपुट होगा :
Enter String : parth
string without forward space :parth
उदाहरण 4:
Write a program to eliminate trailing blank space in string
इस उदहारण मे , किसी string के बाद आने वाले वाले blank space को eliminate करना है |
Explanation
1.सबसे पहले , एक string variable ‘statement’ को declare करगे | और यूजर द्वारा दिए गये input को इस string assign करेगे |
2. उसके बाद string की length को strlen() function से calculate करते हैऔर इसे length मे assign करते है |
3.इस प्रोग्राम मे , null pointer के position को set करनी है
Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char statement[100];
printf(“Enter String : “);
gets(statement);
int i,j,check;
int length = strlen(statement);
i=0;
check=-1;
while(statement[i]=’ ‘ && statement[i] == ‘\t’ && statement[i] == ‘\n’)
{
check=i;
}
i++;
i++;
}
statement[check+1] = ‘\0’;
printf(“string without backward space”);
puts(statement);
getch();
}
आउटपुट होगा :
Enter String :parth /0
string without backward space :parth/0
उदाहरण 5:
Write a program to calculate number of consonant और vowel character in string .
इस उदहारण मे , कसी string मे से consonant और vowel character के number को find आउट करना होता है |
Explanation
1.सबसे पहले , एक string variable ‘statement’ को declare करगे | और यूजर द्वारा दिए गये input को इस string assign करेगे |
2. उसके बाद string की length को strlen() function से calculate करते हैऔर इसे length मे assign करते है |
3. loop चलाया जाता है |loop तब तक चलता है जब तक की string मे null pointer नहीं आ जाता है |
3.i. condition check की जाती है |
अगर charterer ‘consonant’ है तब ‘c’ मे increment होगा |
अगर character ‘vowel ‘ है तब ‘v’ मे increment होगा |
Source Code
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main ()
{
char statement[100];
printf(“Enter String “);
gets(statement);
int c =0;
int v=0;
int length = strlen(statement);
for(i=0;i<length;i++)
{
if( statement[i] >=’a’ || statement[i] >=’e’ || statement[i] >=’i’ || statement[i] >=’o’ || statement[i] >=’u’
statement[i] >=’A’ || statement[i] >=’E’ || statement[i] >=’O’ || statement[i] >=’U’ || statement[i] >=’I’ );
{
v++;
}
else
{
c++;
}
}
printf(“Number of Consonant = %d \n”, c ) ;
printf(“Number of Vowels = %d\n “, v ) ;
getch();
}
आउटपुट होगा :
Enter String parthpatel
Number of Consonant = 3
Number of Vowels = 7
Recent Posts
मालकाना का युद्ध malkhana ka yudh kab hua tha in hindi
malkhana ka yudh kab hua tha in hindi मालकाना का युद्ध ? मालकाना के युद्ध…
4 weeks ago
कान्हड़देव तथा अलाउद्दीन खिलजी के संबंधों पर प्रकाश डालिए
राणा रतन सिंह चित्तौड़ ( 1302 ई. - 1303 ) राजस्थान के इतिहास में गुहिलवंशी…
4 weeks ago
हम्मीर देव चौहान का इतिहास क्या है ? hammir dev chauhan history in hindi explained
hammir dev chauhan history in hindi explained हम्मीर देव चौहान का इतिहास क्या है ?…
4 weeks ago
तराइन का प्रथम युद्ध कब और किसके बीच हुआ द्वितीय युद्ध Tarain battle in hindi first and second
Tarain battle in hindi first and second तराइन का प्रथम युद्ध कब और किसके बीच…
4 weeks ago
चौहानों की उत्पत्ति कैसे हुई थी ? chahamana dynasty ki utpatti kahan se hui in hindi
chahamana dynasty ki utpatti kahan se hui in hindi चौहानों की उत्पत्ति कैसे हुई थी…
1 month ago
भारत पर पहला तुर्क आक्रमण किसने किया कब हुआ first turk invaders who attacked india in hindi
first turk invaders who attacked india in hindi भारत पर पहला तुर्क आक्रमण किसने किया…
1 month ago