Wednesday, July 2, 2014

Bài tập về xâu trong ngôn ngữ lập trình C. Đếm số ký tự số, ký tự hoa, ký tự thường, số tự nhiên.

       
  1.      Đếm số kí tự số trong xâu nhập từ bàn phím.

#include <stdio.h>
#include <conio.h>
#include <string.h>
int demktso(chars[])
{
       inti,dem=0;
       for(i=0;i<strlen(s);i++)
        if ( s[i]>='0' && s[i]<='9' )
                     dem++;
       returndem;
}
void main()
{
    chars[100];
    gets(s);
    printf("so ki tu so la  :  %d",demktso(s));
    getch();
}

        2.      Đếm số kí tự hoa trong xâu nhập từ bàn phím.

#include <stdio.h>
#include <conio.h>
#include <string.h>
int demkthoa(chars[])
{
       inti,dem=0;
       for(i=0;i<strlen(s);i++)
        if ( s[i]>='A' && s[i]<='Z' )
                     dem++;
       returndem;
}
void main()
{
    chars[100];
    gets(s);
    printf("so ki tu hoa la  :  %d",demkthoa(s));
    getch();
}

        3.      Đếm số kí tự thường trong xâu nhập từ bàn phím.

#include <stdio.h>
#include <conio.h>
#include <string.h>
int demktthuong(chars[])
{
       inti,dem=0;
       for(i=0;i<strlen(s);i++)
        if ( s[i]>='a' && s[i]<='z' )
                     dem++;
       returndem;
}
void main()
{
    chars[100];
    gets(s);
    printf("so ki tu thuong la  :  %d",demktthuong(s));
    getch();
}

       4.      Đếm các số tự nhiên trong xâu nhập từ bàn phím. Các kí tự số gần nhau ghép thành 1 số tự nhiên.
Ví dụ: a123bc4d56ef
           cho ra đáp án là : 3
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
    chars[100];
    inti=0,j,dem=0;
    gets(s);
    while(i<=strlen(s))
    {
        j=0;
        while ( s[i]>='0' && s[i]<='9' )
        {
             i++;
             j++;
        }
        i++;
        if(j!=0) dem++;
     }
     printf("%d",dem);
     getch();

}
 Link file word

No comments:

Post a Comment