C pointers and string

本文介绍了一个使用C语言实现的字符串处理程序,包括字符串长度计算、格式化输出、大小写转换等功能,并通过冒泡排序算法对字符串数组进行大小写不敏感的排序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

typedef int (* fptrOperation)(const char *, const char *);

char *returnALiteral(int);
size_t stringLength(const char *str);
char *format(char *, size_t, const char*, size_t, size_t);
int compare(const char*, const char*);
int compareIgnoreCase(const char*, const char*);
char* stringToLower(const char *);
void bubleSort(char* [], size_t, fptrOperation);

int main(int argc, char *argv[])
{

	char simpleArray[] = "simple string";
	char *simplePtr = (char*)malloc(strlen("simple string")+1);
	strcpy(simplePtr, "simple string");

	printf("%d\n",stringLength(simpleArray));
	//printf("%d\n",stringLength(&simpleArray));
	printf("%d\n",stringLength(&simpleArray[0]));

	char *buffer = (char *)malloc(100);
	printf("%s\n", format(buffer, 100, "Alex", 30, 50));

	printf("test for argv\n");

	for (int i = 0; i < argc; i++) {
		printf("argv[%d]: %s\n", i, *argv++);
	}

	char* names[] = {"Bob", "Ted", "Carol", "Alice", "alice"};
	printf("%d\n", sizeof(names));

	bubleSort(names, 5, compareIgnoreCase);
	for (int i = 0; i < 5; i++) {
		printf("%s ", *(names + i));
	}

	return 0;
}

size_t stringLength(const char *str) {
	size_t len = 0;
	while (*str++) 
		len++;
	return len;

}

char *format(char *buffer, size_t size, const char * name, size_t quantity, size_t weight) {
	snprintf(buffer, size, "Item: %s Quantity: %u Weight: %u", name, quantity, weight);
	return buffer;
}
char* returnALiteral(int code) {
	switch(code) {
		case 100:
			return "Boston Processing Center";
		case 200:
			return "Denver Processing Center";
		case 300:
			return "Atlanta Processing Center";
		case 400:
			return "San Jose Processing Center";

	}
}

int compare(const char *s1, const char*s2) {
	return strcmp(s1, s2);
}

int compareIgnoreCase(const char *s1, const char *s2) {
	char *temp1 = stringToLower(s1);
	char *temp2 = stringToLower(s2);
	int result = strcmp(temp1, temp2);

	free(temp1);
	free(temp2);
	return result;
}

char* stringToLower(const char * str) {
	char *temp = (char *)malloc(strlen(str) + 1);
	char *start = temp;
	while (*start++ = tolower(*str++))
		;
	return temp;
}

void bubleSort(char* arr[],size_t size, fptrOperation operation) {
	int swap = 1;
	while (swap) {
		swap = 0;
		for (size_t i = 0; i < size - 1; i++) {
			if (operation(arr[i], arr[i+1]) > 0) {
				swap = 1;
				char *temp = arr[i];
				arr[i] = arr[i+1];
				arr[i+1] = temp; 
			}
		}
	}
}

【基于QT的调色板】是一个使用Qt框架开发的色彩选择工具,类似于Windows操作系统中常见的颜色选取器。Qt是一个跨平台的应用程序开发框架,广泛应用于桌面、移动和嵌入式设备,支持C++和QML语言。这个调色板功能提供了横竖两种渐变模式,用户可以方便地选取所需的颜色值。 在Qt中,调色板(QPalette)是一个关键的类,用于管理应用程序的视觉样式。QPalette包含了一系列的颜色角色,如背景色、前景色、文本色、高亮色等,这些颜色可以根据用户的系统设置或应用程序的需求进行定制。通过自定义QPalette,开发者可以创建具有独特视觉风格的应用程序。 该调色板功能可能使用了QColorDialog,这是一个标准的Qt对话框,允许用户选择颜色。QColorDialog提供了一种简单的方式来获取用户的颜色选择,通常包括一个调色板界面,用户可以通过滑动或点击来选择RGB、HSV或其他色彩模型中的颜色。 横渐变取色可能通过QGradient实现,QGradient允许开发者创建线性或径向的色彩渐变。线性渐变(QLinearGradient)沿直线从一个点到另一个点过渡颜色,而径向渐变(QRadialGradient)则以圆心为中心向外扩散颜色。在调色板中,用户可能可以通过滑动条或鼠标拖动来改变渐变的位置,从而选取不同位置的颜色。 竖渐变取色则可能是通过调整QGradient的方向来实现的,将原本水平的渐变方向改为垂直。这种设计可以提供另一种方式来探索颜色空间,使得选取颜色更为直观和便捷。 在【colorpanelhsb】这个文件名中,我们可以推测这是与HSB(色相、饱和度、亮度)色彩模型相关的代码或资源。HSB模型是另一种常见且直观的颜色表示方式,与RGB或CMYK模型不同,它以人的感知为基础,更容易理解。在这个调色板中,用户可能可以通过调整H、S、B三个参数来选取所需的颜色。 基于QT的调色板是一个利用Qt框架和其提供的色彩管理工具,如QPalette、QColorDialog、QGradient等,构建的交互式颜色选择组件。它不仅提供了横竖渐变的色彩选取方式,还可能支持HSB色彩模型,使得用户在开发图形用户界面时能更加灵活和精准地控制色彩。
### Differences Between `string.h` and `std::string` In C programming, handling strings can be done using two primary methods: through functions provided by the standard library header `<string.h>` or via the C++ Standard Library class `std::string`. #### Functions Provided by `<string.h>` The `<string.h>` header offers a set of low-level operations for manipulating arrays of characters (null-terminated byte strings). These include: - **Copying Strings**: The function `strcpy(dest, src)` copies the string pointed to by `src`, including the terminating null character, into the array pointed to by `dest`. - **Concatenating Strings**: Using `strcat(s1, s2)`, appends a copy of the suffix of `s2` to the end of the string `s1`. Both must be properly null terminated. - **Comparing Strings**: With `strcmp(s1, s2)`, compares lexicographically the null-terminated string `s1` with the string `s2`. These functions operate directly on raw pointers representing character sequences. They require manual management of memory allocation and deallocation when dynamically allocating space for strings[^1]. ```c #include <stdio.h> #include <string.h> int main() { char dest[50]; strcpy(dest, "Hello"); strcat(dest, " World!"); printf("%s\n", dest); } ``` #### Class `std::string` Introduced in C++, `std::string` provides higher level abstractions over traditional C-style strings. This container manages its own storage automatically, allowing more intuitive manipulation without worrying about buffer sizes or overflow issues. Key features include: - **Automatic Memory Management**: Automatically resizes internal buffers as needed. - **Operator Overloading Support**: Enables natural syntax like concatenation (`+`) between objects. - **Rich Set of Member Functions**: Includes search capabilities, insertion/deletion at arbitrary positions within the sequence among others. Using `std::string` leads generally safer code since many common errors associated with pointer arithmetic are avoided[^3]. ```cpp #include <iostream> #include <string> using namespace std; int main(){ string greeting = "Hello"; greeting += " world!"; cout << greeting; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值