0% found this document useful (0 votes)
21 views2 pages

String Demo

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)
21 views2 pages

String Demo

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/ 2

#include<stdio.

h>
#include<string.h>

int main () {
printf ("Nhap chuoi ki tu :");
char str[100];
char str1[] = "FPT University";
char str2[] = {'I',' ','l','o','v','e',' ','y','o','u','\0'};

// Hien chuoi
printf ("\nStr1: ");
int i,j;
for ( i = 0 ; i <= strlen(str1) ; i++) {
printf ("%c", str1[i]);
}
printf ("\n%s ", str2);

printf ("\nNhap chuoi khac :");


gets(str); // scanf version 2 : neu muon nhap chuoi ma co dau space
thi hay dung ( VD : Nguyen Thanh Tung )
printf ("\nStr = %s", str);

printf ("Hien chuoi khac cua khac");


puts(str2); // Printf verson 2 : dung trong string

// Sao chep mot chuoi


// Cach 1 : duyet mang de sao chep
char str3[strlen(str+1)];
for ( i = 0 ; i < strlen(str) ; i++) {
str3[i] = str[i];
j++;
}
str3[j] = '\0';
printf ("\nStr3 = %s", str3);
// Cach 2 : su dung ham strcpy
char str4[strlen(str+1)];
strcpy(str4,str);
printf ("\nStr4 = %s", str4);

/* Coi string la ho ten cua minh . VD : Nguyen Van A


In ra ho , ten va ten dem
*/

char HovaTen[100];
printf ("\n");
gets(HovaTen);
// code in ho
char Ho[strlen(HovaTen)+ 1];
j = 0;
for ( i = 0 ; HovaTen[i]!=' ' ; i++) {
Ho[j] = HovaTen[i];
j++;
}
Ho[j] = '\0';
printf ("\n Ho : %s", Ho);

// code in ten
char Ten[strlen(HovaTen)+1];
int end;
for ( i = strlen(HovaTen)-1 ; HovaTen[i] != ' '; i--) {
end = i;
}

j = 0;
for ( i = end ; i < strlen(HovaTen) ; i++ ) {
Ten[j] = HovaTen[i];
j++;
}
Ten[j] = '\0';
printf ("\nTen :%s", Ten);

/* Coi string chua 1 chuoi :


- khong co dau space thua o dau va cuoi
- giua cac tu trong chuoi chi co mot dau space
Kiem tra chuoi co bao nhieu tu ?
Tu nao co chieu dai lon nhat*/

printf ("Nhap chuoi can tinh : ");


char string[50];
gets(string);

// Kiem tra chuoi co bao nhieu tu :


int count = 0;
for ( i = 0 ; i < strlen(string) ; i++) {
if ( string[i] == ' ' || string[i] == '\n' || string[i] == '\t') {
count++;
}
}
printf ("\nCo %d tu trong chuoi ", count);

// Kiem tra tu nao dai nhat


int first_char , last_char , dem , max = 0;
for ( i = 0 ; i < strlen(string) ; i++) {
if (string[i] != ' ') {
dem = 1;
j = i+1;
}
for ( ;j < strlen(string) && string[j] != ' ' ; j++ ) {
dem++;
if ( dem > max ) {
dem = max;
first_char = i;
last_char = j;
}
}
}
printf ("\nKi tu dai nhat trong chuoi :");
for ( i = first_char ; i <= last_char ; i++) {
printf ("%c", string[i]);
}
return 0;

You might also like