0% found this document useful (0 votes)
9 views3 pages

String Exercise

Uploaded by

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

String Exercise

Uploaded by

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

#include<stdio.

h>
#include<string.h>
#include<stdlib.h>
char check_Palindrome ( char str[100]) { // Bai 5
int i;
for ( i = 0 ; i < strlen(str) ; i++) {
// dieu kien de check chuoi co doi xung ko
if ( str[i] != str[strlen(str)-1-i] ) {

return 0;
}
}
return 1;
}

void character_to_int ( char str[]) { // Bai 6


int i;
int kitu = 0;
for ( i = 0 ; i < strlen(str) ; i++) {
// dieu kien check chuoi co phai toan ki tu so hay ko ( su dung bang ma
ASCII )
if (str[i] <= 48 && str[i] >= 57 || str[0]==0 ) {
kitu = 1;
break;
}
}
if ( kitu == 0 ) {
printf ("\nSau khi bien doi ki tu thanh so tu nhien :%d", atoi(str));
}
if ( kitu == 1) {
printf ("\n-1");
}

int main () {

// Bai 5
printf ("Bai 5 :\n");
printf ("Nhap chuoi :\n");
char str[100];
gets(str);

// goi ham
if ( check_Palindrome(str)==1) {
printf ("\nDay la chuoi Palindrome");
}
if ( check_Palindrome(str)==0) {
printf ("\nDay khong phai chuoi Palindrome");
}

// Bai 6
printf ("\nBai 6 :\n");
printf ("\nNhap 1 chuoi bat ki :");
char str1[100];
gets(str1);
character_to_int(str1);
// Bai 7

// Bai 8
printf ("\nBai 8 :\n");
printf ("\nNhap chuoi :");
char s[50];
gets(s);
char s2[strlen(s)+1];

int i,j;
for ( i = 0 ; s[i]!=' '; i++) {
s2[j] = s[i];
j++;
}
s2[j] = '\0';
printf ("\nTu dau tien xuat hien trong chuoi : %s", s2);

// Bai 9
printf ("\nBai 9 :\n");
printf ("\nNhap ho va ten :");
char name[100];
gets(name);

// Cat bo nhung dau space thua giua cac tu

for ( i = 0 ; i < strlen(name) ; i++) {


if ( name[i] == ' ' && name[i+1] == ' ') {
// Dung dau & thi se dung duoc ham strcpy voi hai ki tu , vi ham
strcpy ban dau chi copy dc 2 chuoi
strcpy(&name[i],&name[i+1]);
i--; // phai i-- de lui i lai check xem co khoang trang tiep
khong
}
}

// Cat bo dau space thua o dau chuoi


// code nay se phat huy tac dung khi chung ta space xong moi nhap chuoi
if (name[0] == ' ') {
// Dung dau & thi se dung duoc ham strcpy voi hai ki tu , vi ham strcpy
ban dau chi copy dc 2 chuoi
strcpy(&name[0],&name[1]);
}
// Cat bo dau space thua o cuoi chuoi
if (name[strlen(name)-1]== ' ') {
// Dung dau & thi se dung duoc ham strcpy voi hai ki tu , vi ham strcpy
ban dau chi copy dc 2 chuoi
strcpy(&name[strlen(name)-1],&name[strlen(name)]);
}

// Chuan hoa thuong ==> hoa , hoa ==> thuong


if (name[0] != ' ' && name[0] >= 'a' && name[0] <= 'z') {
// Chuyen ki tu dau tien sang ki tu in hoa vi khi chay vong lap
// ben duoi , vong lap se luon duyet tu ki tu thu 2 tro di
name[0]-=32;
}
// Dung vong lap nay de chuyen cac ki tu khong phai ki tu dau moi word tu HOA
==> thuong
for ( j = 1 ; j < strlen(name) ; j++) {
if ( name[j] != ' ' && name[j] >= 'A' && name[j] <= 'Z') {
name[j]+=32;
}
}
// Dung vong lap nay de chuyen cac ki tu dau tien cua moi word tu thuong ==>
in hoa
for ( i = 1 ; i < strlen(name) ; i++) {
// dieu kien de tim ra ki tu dau tien cua moi tu
if (name[i]==' ' && name[i+1]!=' ' &&name[i+1]>='a' && name[i+1]<='z')
{
// neu ki tu phia truoc la dau space va ki tu phia sau la ki tu
thuong
// chuyen no sang ki tu in hoa bang cach su dung ma ASCII
name[i+1]-=32;
}
}

printf ("Sau khi da chuan hoa chuoi :%s", name);


return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy