0% found this document useful (0 votes)
19 views

2 String

This C code document demonstrates various string manipulation functions in C including initializing and assigning string values, reading strings from user input, copying and modifying strings, and looping through strings. It includes 10 code demos showing how to: 1) Initialize string variables and assign values 2) Read strings from user input using scanf(), fgets(), and getchar() 3) Copy strings using strcpy() and check string lengths with strlen() 4) Replace and toggle characters in strings using functions like toupper() and tolower() 5) Loop through strings using while and for loops to count characters or modify string values The document provides examples of common string operations in C like initialization, input/output,

Uploaded by

phubase.san
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)
19 views

2 String

This C code document demonstrates various string manipulation functions in C including initializing and assigning string values, reading strings from user input, copying and modifying strings, and looping through strings. It includes 10 code demos showing how to: 1) Initialize string variables and assign values 2) Read strings from user input using scanf(), fgets(), and getchar() 3) Copy strings using strcpy() and check string lengths with strlen() 4) Replace and toggle characters in strings using functions like toupper() and tolower() 5) Loop through strings using while and for loops to count characters or modify string values The document provides examples of common string operations in C like initialization, input/output,

Uploaded by

phubase.san
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/ 5

#include <stdio.

h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define N 5
#define MAX_LEN 15

int main()
{
//-----------demo1----------//

//without size
// char date1[] = "June 14";
// char date2[] = {'M','a','y',' ','1','4','\0'};

//Also, we can initialize a string variable using a pointer:


// Similar to char date1[] = "June 14"
// char *date1 = "June 14"; //| J | u | n | e | | 1 |4 | \0
// // Similar to char date2[] = "May 14"
// char *date2 = "May 14";

////with size
// char date1[MAX_LEN+1] = "June 14"; // +1 for '\0'
// char date2[MAX_LEN+1] = "May 14";
// //printing string by printf, puts
// printf("%s\n", date1);
// printf("%s\n", date2);
// puts(date2);

//-----------demo2----------//

//string = array of char ended with \0


//Initializing a String Variable

// char date1[MAX_LEN+1] = "June 14"; // +1 for '\0' --> 16 slot


// char date2[] = "June 14"; //8slots
// //char *date2 = "June 14";
// strcpy(date1,"January 8"); // date1 = "January 8"
// //date1 = "January 8"; //wrong
// //strcpy(date2,"January 8"); //Abort trap: 6 Error

// printf("%s\n", date1);
// printf("%s\n", date2);

//-----------demo3----------//
//------scanf(), fgets() and getchar() to read string------
//-------1. read word vs. many words
//-------2. If inputs more than max_size -> scanf will prompt error but
//fgets will auto keep the value + \0
//Welcome to ICT, Hello World!, Fundamental of programming, MUICT,
a_very_long_sentence

// char name [15]; //| H | e | l | l | o | \0


// scanf("%s", name); //name [] is Welcome\0 ,
// printf("%s\n",name);
// puts(name);

//it can be space, enter, tab it will stop reading and the null char (\0) will
be added automatically

//fgets: it is a good way to read words

// char name [15];


// fgets(name, 5, stdin); //name [] is Welcome to ICT\0, MUICT\n\0
// printf("%s\n",name);
// puts(name);

//getchar: get each char with loop

// char input_str[MAX_LEN];//15 | t i p\n


// int i = 0; //1
// char c = getchar(); //t
// while((i < MAX_LEN - 1) && (c != '\n')) { //the last slot for \0 i<14, c
// input_str[i++] = c; //| t | i | p | \0
// c = getchar(); //i
// }
// input_str[i] = '\0';
// printf("%s\n", input_str);

// for (i = 0; i < MAX_LEN; ++i){


// if(input_str[i] == '\0') break; //--> have some gabage values
// putchar(input_str[i]); //print each char
// }

// printf("\n");

//-----------demo4----------//
//15
/*
char str1[MAX_LEN] = "Google"; // if no MAX_LEN, we cannot store a longer
string.
char str2[MAX_LEN] = "Microsoft";

//str2 = str1; //Error !! --> Cannot assign the value with ‘=’
//strcpy(str2, str1); // strcpy() is typically used to assign string value
strcpy(str1, str2);
printf("%s %s\n", str1, str2);
*/
//

//-----------demo5----------//

// char str1[] = "Google"; //| G | o | o | g | l | e | \0 |


// char str2[] = "Microsoft";
// char str3[] = "Apple"; //| A | p | p | l | e | \0 |
// strcpy(str2, str1);
// printf("%s %s\n", str1, str2); //

//strcpy(str3, str1); // Error !! --> the size of str3 is smaller than str1
//printf("%s %s\n", str1, str3);

//-----------demo6----------//

// char fname[MAX_LEN], lname[MAX_LEN], fullname[MAX_LEN]; //15 | t|i |p | |


t | h | a |i
// scanf("%s", fname); //tip
// scanf("%s", lname); //thai
// printf("# of chars in fname: %lu\n", strlen(fname));
// printf("# of chars in lname: %lu\n", strlen(lname));

// strcpy(fullname, fname);
// strcat(fullname, " ");
// strcat(fullname, lname);

// printf("Fullname: %s\n", fullname);


// printf("# of chars: %lu\n", strlen(fullname));

//-----------demo7 (while, for loop through string)----------//


//while loop
/*
char str[] = "This is an Alphabet."; // a
int i = 0;
int num_a = 0;
while (str[i] != '\0') { // Check for end-of-string
if (str[i] == 'A' || str[i] == 'a') { //(tolower(str[i]) == 'a') {
num_a++;
}
i++;
}
printf("%d\n", num_a);
*/

//for loop
/*
char str[] = "This is an Alphabet.";
int num_a = 0;
for(int i = 0; i<strlen(str); i++){
if (str[i] != '\0') { // Check for end-of-string
if (str[i] == 'A' || str[i] == 'a') {
num_a++;
}
}else{
break;
}
}
printf("%d\n", num_a);
*/

//-----------demo8 (Copy string)----------//


/*
char str1[] = "Hi";
char str2[MAX_LEN];
int i=0;
// Check for end-of-string and array size
while ((str1[i] != '\0') && (i < MAX_LEN-1)) {
str2[i] = str1[i];
i++;
}
str2[i] = '\0'; // Don’t forget the end-of-string

//strcpy(str2, str1);
printf("%s\n", str2);
*/

//-----------demo9 (Replace Characters)----------//


/*
char str[] = "This_is_a_VEry_Long_stRIng.";
int i = 0;
printf("Before: %s\n", str);
while (str[i] != '\0') {
if (str[i] == '_') {
str[i] = ' ';
}
i++;
}
printf("After: %s\n", str);
*/

//-----------demo10 (Toggle Characters)----------//


/*
char str[] = "This_is_a_VEry_Long_stRIng.";
int i = 0;
printf("Before: %s\n", str);
while (str[i] != '\0') {
if (isalpha(str[i])) { //return 1 if it is character is a letter
if (islower(str[i])) { //isupper(), isdigit()
str[i] = toupper(str[i]);
} else {
str[i] = tolower(str[i]);
}
}
i++;
}
printf("After: %s\n", str);
*/

return 0;
}

You might also like